Skip to content

Commit

Permalink
Remove .eslintignore
Browse files Browse the repository at this point in the history
  • Loading branch information
surmon-china committed Mar 28, 2020
1 parent fd1d19b commit bc4260c
Show file tree
Hide file tree
Showing 9 changed files with 9,567 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
extends: './node_modules/@surmon-china/abc-factory/preset/eslintrc/typescript',
rules: {
'@typescript-eslint/explicit-function-return-type': 0,
'@typescript-eslint/no-explicit-any': 0
}
}
6 changes: 6 additions & 0 deletions abc.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
entry: 'src/bing.ts',
fileName: 'bing',
minimize: false,
targets: ['esm', 'cjs']
}
72 changes: 72 additions & 0 deletions dev/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* @file Example of wonderful-bing-wallpaper
* @author Surmon <https://github.com/surmon-china>
*/

const xml2js = require('xml2js')
const WonderfulBingWallpaper = require('../dist/bing.cjs')

const resolutions = WonderfulBingWallpaper.getResolutions()
const wbw = new WonderfulBingWallpaper({
size: 8,
day: 7,
resolution: resolutions[2],
host: 'cn.bing.com',
local: 'zh-cn',
})

console.log('wallPaper', wbw)
console.log('\nwallPaper resolutions', resolutions, resolutions[3])

// default json
wbw.getWallpapers().then(wallpaperJSON => {
console.group('wallpaperJSON-1')
console.log('got wallpaperJSON-1 data', wallpaperJSON)
console.log(`\ngot wallpaperJSON-1 humanizeWallpapers data :url = ${resolutions[2]}\n`, wbw.humanizeWallpapers(wallpaperJSON))
console.log(`\ngot wallpaperJSON-1 humanizeWallpapers data :url = ${resolutions[3]}\n`, wbw.humanizeWallpapers(wallpaperJSON, resolutions[3]))
console.log(`\ngot wallpaperJSON-1[0] humanizeWallpapers data :url = ${resolutions[2]}\n`, wbw.humanizeWallpapers(wallpaperJSON[0]))
console.log(`\ngot wallpaperJSON-1[0] humanizeWallpapers data :url = ${resolutions[5]}\n`, wbw.humanizeWallpapers(wallpaperJSON[0], resolutions[5]))
console.groupEnd()
})

// params json
wbw.getWallpapers({ size: 10, day: 2 }).then(wallpaperJSON => {
console.group('wallpaperJSON-2')
console.log('got wallpaperJSON-2 data', wallpaperJSON)
console.log('got wallpaperJSON-2 humanizeWallpapers data', wbw.humanizeWallpapers(wallpaperJSON))
console.log('got wallpaperJSON-2[0] humanizeWallpapers data\n', wbw.humanizeWallpapers(wallpaperJSON[0]))
console.groupEnd()
})

wbw.setOptions({ ensearch: 1 })

// ensearch
wbw.getWallpapers().then(wallpaperJSON => {
console.group('wallpaperJSON-3')
console.log('got wallpaperJSON-3 data', wallpaperJSON)
console.log('got wallpaperJSON-3 humanizeWallpapers data', wbw.humanizeWallpapers(wallpaperJSON))
console.groupEnd()
})

// xml
wbw.getWallpapers({ format: 'xml' }).then(wallpaperXML => {
console.group('wallpaperXML')
console.debug('got wallpaperXML data\n', wallpaperXML)
xml2js.parseString(wallpaperXML, (err, result) => {
console.log('wallpaperXML', result)
})
console.groupEnd()
})

// setOptions
wbw.setOptions({ size: 3 })

// rss
wbw.getWallpapers({ format: 'rss' }).then(wallpaperRSS => {
console.group('wallpaperRSS')
console.debug('got wallpaperRSS data\n', wallpaperRSS)
xml2js.parseString(wallpaperRSS, (err, result) => {
console.log('wallpaperRSS', result)
})
console.groupEnd()
})
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('@surmon-china/abc-factory/preset/jest/typescript')
163 changes: 163 additions & 0 deletions src/bing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/**
* @file WonderfulBingWallpaper Class
* @author Surmon <https://github.com/surmon-china>
*/

import https from 'https'
import querystring from 'querystring'

// https://www.bing.com/HPImageArchive.aspx?format=js&n=1&pid=hp&ensearch=0
// https://www.bing.com/HPImageArchive.aspx?format=js&n=1&pid=hp&ensearch=1

/**
* Resolutions constant.
*/
const RESOLUTIONS = [
'1920x1200',
'1920x1080',
'1366x768',
'1280x768',
'1024x768',
'800x600',
'800x480',
'768x1280',
'720x1280',
'640x480',
'480x800',
'400x240',
'320x240',
'240x320'
]

/**
* Bing data dormat.
*/
enum DataFormt {
JSON = 'js',
XML = 'xml',
RSS = 'rss',
}

/**
* Default options.
* @return {Object} A default options.
*/
const DEFAULT_OPTIONS = Object.freeze({
size: 1,
day: 0,
format: DataFormt.JSON as string,
ensearch: 0,
local: 'en-US',
host: 'www.bing.com',
wallpaperApi: '/HPImageArchive.aspx',
resolution: RESOLUTIONS[1]
})

type WonderfulBingWallpaperOption = Partial<typeof DEFAULT_OPTIONS>
type GetWallpapersParams = Pick<WonderfulBingWallpaperOption, 'size' | 'day' | 'local' | 'format'>

export default class WonderfulBingWallpaper {

options: WonderfulBingWallpaperOption = DEFAULT_OPTIONS

constructor(options: WonderfulBingWallpaperOption = {}) {
this.setOptions(options)
}

/**
* Set the instance new options.
* @param {options} The WonderfulBingWallpaper options.
*/
setOptions(options: WonderfulBingWallpaperOption) {
this.options = {
...this.options,
...options
}
}

/**
* Get the wallpapers by params.
* @param {params} size - wallpapers size.
* @param {params} day - Before days.
* @param {params} local - The location.
* @param {params} format - The result doc format.
*/
getWallpapers(params?: GetWallpapersParams): any {
return new Promise((resolve, reject) => {
// params
const mergeParmas = {
...this.options,
...params
}
const queryParams = {
n: mergeParmas.size,
idx: mergeParmas.day,
format: mergeParmas.format,
mkt: mergeParmas.local,
ensearch: mergeParmas.ensearch,
pid: 'hp'
}

// options
const requestOptions = {
port: 443,
method: 'GET',
host: this.options.host,
path: this.options.wallpaperApi + '?' + querystring.stringify(queryParams),
}

// request
const request = https.request(requestOptions, response => {
const { statusCode } = response
if (statusCode && (statusCode < 200 || statusCode >= 300)) {
return reject(new Error(`WonderfulBingWallpaper getWallpapers error: statusCode=${statusCode}`))
}
let body: any = []
response.on('data', data => { body.push(data) })
response.on('end', () => {
body = Buffer.concat(body).toString()
if (queryParams.format === DataFormt.JSON) {
body = JSON.parse(body)
}
resolve(body.images || body)
})
})

// request
request.on('error', reject)
request.end()
})
}

/**
* Get the humanize wallpapers by original wallpapers.
* @param {object} wallpaperJson - original wallpapers.
* @param {string} resolution - wallpaper resolution.
*/
humanizeWallpapers(wallpaperJson: any | any[], resolution?: string) {
const targetResolution = resolution || this.options.resolution
const doHumanize = (image: any) => {
const host = 'https://' + this.options.host
const fileFormat = (/\.[^\.]+$/.exec(image.url))
const targetFileFormat = fileFormat?.length ? fileFormat?.[0] : '.jpg'
return {
...image,
humanizedSearchUrl: `${host}${image.quiz}`,
humanizedImageUrl: `${host}${image.url}`,
humanizeResolutionUrl: `${host}${image.urlbase}_${targetResolution}${targetFileFormat}`
}
}

return Array.isArray(wallpaperJson)
? wallpaperJson.map(doHumanize)
: doHumanize(wallpaperJson)
}

/**
* Static function.
* @return {Array} A resolutions array.
*/
static getResolutions() {
return RESOLUTIONS
}
}
93 changes: 93 additions & 0 deletions test/bing.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* @file Test of WonderfulBingWallpaper Class
* @author Surmon <https://github.com/surmon-china>
*/

import WonderfulBingWallpaper from '../src/bing'
import xml2js from 'xml2js'

const resolutions = WonderfulBingWallpaper.getResolutions()
const wbw = new WonderfulBingWallpaper({
size: 8,
day: 7,
resolution: resolutions[2],
host: 'cn.bing.com',
local: 'zh-cn'
})

describe('Test Class WonderfulBingWallpaper', () => {

it('test instance', () => {
expect(wbw).toBeInstanceOf(WonderfulBingWallpaper)
expect(wbw.options.host).toBe('cn.bing.com')
expect(wbw.options.local).toBe('zh-cn')
expect(wbw.options.day).toBe(7)
})

it('test resolutions', () => {
expect(resolutions.length).toBe(14)
expect(resolutions[6]).toBe('800x480')
})

it('test getWallpapers', done => {
wbw.getWallpapers()
.then((data: any) => {
expect(data.length).toBe(8)
expect(data[0].url.indexOf('.jpg') > -1).toBe(true)
done()
})
.catch((error: any) => {
done(error)
})
})

it('test params getWallpapers', done => {
wbw.getWallpapers({ size: 10, day: 2 })
.then((data: any) => {
expect(data.length).toBe(8)
expect(data[0].url.indexOf('.jpg') > -1).toBe(true)
expect(data[0].quiz.indexOf('/search')).toBe(0)
done()
})
.catch((error: any) => {
done(error)
})
})

it('test getWallpapers format', done => {
wbw.getWallpapers({ format: 'xml' })
.then((wallpaperXML: any) => {
xml2js.parseString(wallpaperXML, (_, wallpaperXMLJSON) => {
expect(wallpaperXML).not.toBeUndefined()
expect(wallpaperXMLJSON).not.toBeUndefined()
expect(Object.keys(wallpaperXMLJSON.images).includes('tooltips')).toBe(true)
expect(wallpaperXMLJSON.images.image.length).toBe(8)
expect(wallpaperXMLJSON.images.image[0].copyright[0]).not.toBeUndefined()
done()
})
})
.catch((error: any) => {
done(error)
})
})

it('test setOptions', () => {
wbw.setOptions({ size: 3 })
expect(wbw.options.size).toBe(3)
})

it('test rss format and options merge', done => {
wbw.getWallpapers({ format: 'rss' })
.then((data: any) => {
xml2js.parseString(data, (_, wallpaperXMLJSON) => {
expect(wallpaperXMLJSON.rss.channel.length).toBe(1)
expect(wallpaperXMLJSON.rss.channel[0].link[0]).toBe('http://bing.com/HPImageArchive.aspx?format=rss')
expect(wallpaperXMLJSON.rss.channel[0].item.length).toBe(3)
done()
})
})
.catch((error: any) => {
done(error)
})
})
})
31 changes: 31 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"compilerOptions": {
"target": "es5",
"module": "esnext",
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"importHelpers": true,
"moduleResolution": "node",
"experimentalDecorators": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"declaration": true,
"declarationDir": "types",
"sourceMap": true,
"baseUrl": ".",
"types": [
"node",
"jest"
],
"lib": [
"esnext"
]
},
"exclude": [
"node_modules",
"dev",
"dist",
"test"
]
}
Loading

0 comments on commit bc4260c

Please sign in to comment.