Skip to content

Commit

Permalink
add new interface
Browse files Browse the repository at this point in the history
  • Loading branch information
dotos committed Nov 16, 2016
1 parent 8d788a1 commit f5fb02d
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 31 deletions.
19 changes: 4 additions & 15 deletions configs/config.js
Expand Up @@ -7,23 +7,12 @@ module.exports = {
MASTER_UID: '5893653736',
USER_UID: ''
},
// mysql_dev: {
// host: process.env.mysql_host,
// user: process.env.mysql_user,
// password: process.env.mysql_password,
// port: '3306',
// database: process.env.mysql_database,
// connectionLimit: 10,
// supportBigNumbers: true,
// multipleStatements: true,
// insecureAuth: true
// },
mysql_dev: {
host: 'qdm16395144.my3w.com',
user: 'qdm16395144',
password: 'simple123',
host: process.env.mysql_host,
user: process.env.mysql_user,
password: process.env.mysql_password,
port: '3306',
database: 'qdm16395144_db',
database: process.env.mysql_database,
connectionLimit: 10,
supportBigNumbers: true,
multipleStatements: true,
Expand Down
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -15,9 +15,9 @@
},
"keywords": [
"Bing",
"BingPicture",
"BingPicture API",
"必应图片",
"必应壁纸"
"必应壁纸接口"
],
"bugs": {
"url": "https://github.com/xCss/bing/issues"
Expand Down
55 changes: 54 additions & 1 deletion readme.md
@@ -1,2 +1,55 @@

# Coming Soon
# Bing Pictures Interface | 必应壁纸接口
> :hammer: `Bing 壁纸 Api`重装上阵啦 :smile:
## 目前开放的壁纸接口:
- `/v1{d,w,h,callback}` 返回今日的壁纸完整数据(`可选参数{d,w,h}`):

> 若指定参数`{w,h}` ,则直接返回图片
```js
// d:自今日起第d天前的数据
// w:图片宽度
// h:图片高度
// callback:JSONP的回调函数名
```
- `/v1/rand{w,h,type,callback}` 返回随机的壁纸(`可选参数{w,h,type,callback}`):

```js
// w:图片宽度
// h:图片高度
// type:json,返回值为JSON数据
// callback:JSONP的回调函数名
```
- `/v1/blur{d,w,h,r}` 返回高斯模糊壁纸(`可选参数{d,w,h,r}`):

```js
// d:自今日起第d天前的数据
// w:图片宽度
// h:图片高度
// r:模糊半径(1~50)
```

### **:warning:** `高斯模糊`接口目前只支持指定分辨率(`w,h`)的图片,具体分辨率如下:
```js
/**
* 已知分辨率
*/
resolutions: [
'1920x1200',
'1920x1080',
'1366x768',
'1280x768',
'1024x768',
'800x600',
'800x480',
'768x1280',
'720x1280',
'640x480',
'480x800',
'400x240',
'320x240',
'240x320'
]
```

欢迎点评→[issue](https://github.com/xCss/bing/issues)
88 changes: 75 additions & 13 deletions routes/v1.js
Expand Up @@ -18,15 +18,15 @@ router.post('/', function(req, res, next) {
});

var v1 = function(req, res, next) {
var d = req.params.d;
var w = req.params.w;
var h = req.params.h;
var d = req.query.d || req.body.d;
var w = req.query.w || req.body.w;
var h = req.query.h || req.body.h;
var size = w + 'x' + h;
var enddate = 0;
if (!isNaN(d)) {
var date = +Date.now / 1000 / 60 / 24 - +d;
var newDate = new Date(date * 24 * 60 * 1000);
enddate = newDate.getFullYear() + '' + (newDate.getMonth() + 1) > 10 ? (newDate.getMonth() + 1) : '0' + (newDate.getMonth() + 1) + '' + newDate.getDay() > 10 ? newDate.getDay() : '0' + newDate.getDay();
var date = new Date().getTime() - parseInt(d) * 1000 * 60 * 60 * 24;
var newDate = new Date(date);
enddate = newDate.getFullYear() + '' + ((newDate.getMonth() + 1) > 10 ? (newDate.getMonth() + 1) : '0' + (newDate.getMonth() + 1)) + '' + (newDate.getDate() > 10 ? newDate.getDate() : '0' + newDate.getDate());
}
var params = {
page: {
Expand All @@ -40,7 +40,6 @@ var v1 = function(req, res, next) {
enddate: enddate
}
}
console.log(params);
dbUtils.get('bing', params, function(rows) {
if (rows.length > 0) {
var data = rows[0];
Expand All @@ -63,7 +62,7 @@ var v1 = function(req, res, next) {
message: ''
}
};
if (req.method === 'GET' && req.params.callback) {
if (req.method === 'GET' && req.query.callback) {
res.jsonp(output);
} else {
res.json(output);
Expand All @@ -74,7 +73,7 @@ var v1 = function(req, res, next) {
data: {},
status: {
code: -1,
message: '很抱歉,由于未知原因,暂时无法提供服务,请稍后重试!'
message: '很抱歉,可能由于您输入的天数[d]大于壁纸总数,请修改后重试!'
}
});
}
Expand All @@ -92,11 +91,11 @@ router.post('/rand', function(req, res, next) {
});

var random = function(req, res, next) {
var t = req.params.type;
var w = req.params.w || '1920';
var h = req.params.h || '1080';
var t = req.query.type || req.body.type;
var w = req.query.w || req.body.w || '1920';
var h = req.query.h || req.body.h || '1080';
var size = w + 'x' + h;
var callback = req.params.callback;
var callback = req.query.callback || req.body.callback;
dbUtils.getCount('bing', {}, function(rows) {
if (rows.length > 0) {
var sum = Number(rows[0].sum);
Expand Down Expand Up @@ -152,4 +151,67 @@ var random = function(req, res, next) {
});
}


/**
* 获取高斯模糊图片
*/
router.get('/blur', function(req, res, next) {
blur(req, res, next);
});
router.post('/blur', function(req, res, next) {
blur(req, res, next);
});

var blur = function(req, res, next) {
var d = req.query.d || req.body.d;
var w = req.query.w || req.body.w;
var h = req.query.h || req.body.h;
var r = req.query.r || req.body.r;
r = isNaN(r) ? 10 : parseInt(r) > 50 ? 50 : parseInt(r) <= 0 ? 1 : r;
var size = w + 'x' + h;
var enddate = 0;
if (!isNaN(d)) {
var date = new Date().getTime() - parseInt(d) * 1000 * 60 * 60 * 24;
var newDate = new Date(date);
enddate = newDate.getFullYear() + '' + ((newDate.getMonth() + 1) > 10 ? (newDate.getMonth() + 1) : '0' + (newDate.getMonth() + 1)) + '' + (newDate.getDate() > 10 ? newDate.getDate() : '0' + newDate.getDate());
}
var params = {
page: {
no: 1,
size: 1
},
body: {}
};
if (!!enddate) {
params.body = {
enddate: enddate
}
}
dbUtils.get('bing', params, function(rows) {
if (rows.length > 0) {
var data = rows[0];
var base = 'http://images.ioliu.cn/bing/';
if (resolutions.indexOf(size) > -1) {
data['url'] = base + data.qiniu_url + '_' + size + '.jpg';
}
var qiniu_url = /images\.ioliu\.cn/.test(data.url) ? data.url : base + data.qiniu_url + '_1920x1080.jpg';
qiniu_url += '?imageMogr2/blur/' + r + 'x50'
request.get(qiniu_url)
.set(cookie)
.end(function(err, response) {
res.header('content-type', 'image/jpg');
res.send(response.body);
});
} else {
res.json({
data: {},
status: {
code: -1,
message: '很抱歉,可能由于您输入的天数[d]大于壁纸总数,请修改后重试!'
}
});
}
});
}

module.exports = router;

0 comments on commit f5fb02d

Please sign in to comment.