Skip to content

Commit

Permalink
add fetch bing Pictures and send weibo
Browse files Browse the repository at this point in the history
  • Loading branch information
dotos committed Oct 25, 2016
1 parent 0cf4eff commit 4959970
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 267 deletions.
25 changes: 13 additions & 12 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ console.log(new Date().toLocaleString());

function scheduleCancel() {
var counter = 1;
var t = schedule.scheduleJob('* * * * * *', function () {
var t = schedule.scheduleJob('* * * * * *', function() {
console.log('定时器触发次数:' + counter);
counter++;
});
setTimeout(function () {
setTimeout(function() {
console.log('定时器取消!');
t.cancel();
}, 5000);
Expand All @@ -44,10 +44,11 @@ app.use(cookieParser('bing.ioliu.cn'));
app.use(session({
secret: 'bing app', //secret的值建议使用随机字符串
cookie: {
secure: true,
maxAge: 60 * 30 * 1000 // 过期时间(毫秒)
}
, resave: true
, saveUninitiarlized: false
},
resave: false,
saveUninitiarlized: false
}));
app.use(logger('dev'));
// 启用 passport 组件
Expand All @@ -68,7 +69,7 @@ app.use('/static', express.static(path.join(__dirname, 'static')));
app.use('/', index);
app.use('/weibo', weibo);
// catch 404 and forward to error handler
app.use(function (req, res, next) {
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
Expand All @@ -77,21 +78,21 @@ app.use(function (req, res, next) {
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function (err, req, res, next) {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message
, error: err
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function (err, req, res, next) {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message
, error: {}
message: err.message,
error: {}
});
});
module.exports = app;
158 changes: 70 additions & 88 deletions bin/www
Original file line number Diff line number Diff line change
Expand Up @@ -8,102 +8,84 @@
let app = require('../app');
let debug = require('debug')('API:server');
let http = require('http');
const cluster = require('cluster');
const cpus = require('os').cpus().length;
if (cluster.isMaster) {
console.log('[master] ' + "start master...");

for (var i = 0; i < cpus; i++) {
cluster.fork();
/**
* Get port from environment and store in Express.
*/

let port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
* Create HTTP server.
*/

let server = http.createServer(app);

/**
* Listen on provided port, on all network interfaces.
*/

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
* Normalize a port into a number, string, or false.
*/

function normalizePort(val) {
let port = parseInt(val, 10);

if (isNaN(port)) {
// named pipe
return val;
}

cluster.on('listening', function(worker, address) {
console.log('[master] ' + 'listening: worker' + worker.id + ',pid:' + worker.process.pid + ', Address:' + address.address + ":" + address.port);
});
cluster.on('exit', function(worker, code, signal) {
console.log(`[master] pid:${worker.id} is died, restarting...`);
cluster.fork();
});
} else {
console.log('[worker] ' + "start worker ..." + cluster.worker.id);
/**
* Get port from environment and store in Express.
*/

let port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
* Create HTTP server.
*/

let server = http.createServer(app);

/**
* Listen on provided port, on all network interfaces.
*/

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
* Normalize a port into a number, string, or false.
*/

function normalizePort(val) {
let port = parseInt(val, 10);

if (isNaN(port)) {
// named pipe
return val;
}

if (port >= 0) {
// port number
return port;
}

return false;
if (port >= 0) {
// port number
return port;
}

/**
* Event listener for HTTP server "error" event.
*/
return false;
}

/**
* Event listener for HTTP server "error" event.
*/

function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}

function onError(error) {
if (error.syscall !== 'listen') {
let bind = typeof port === 'string' ?
'Pipe ' + port :
'Port ' + port;

// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}

let bind = typeof port === 'string' ?
'Pipe ' + port :
'Port ' + port;

// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
}

/**
* Event listener for HTTP server "listening" event.
*/
/**
* Event listener for HTTP server "listening" event.
*/

function onListening() {
let addr = server.address();
let bind = typeof addr === 'string' ?
'pipe ' + addr :
'port ' + addr.port;
debug('Listening on ' + bind);
}
function onListening() {
let addr = server.address();
let bind = typeof addr === 'string' ?
'pipe ' + addr :
'port ' + addr.port;
debug('Listening on ' + bind);
}
13 changes: 6 additions & 7 deletions configs/config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
module.exports = {
weibo: {
CLIENT_ID: '1833831541'
, CLIENT_SECRET: '204239c8f3e37e59f2d3eb95de2811a9'
, ACCESS_TOKEN: ''
, MASTER_ACCESS_TOKEN: ''
, MASTER_COOKIES: 'SINAGLOBAL=5484186899582.633.1473772704217; _s_tentry=-; Apache=8980261524863.621.1477197913275; ULV=1477197913448:7:6:1:8980261524863.621.1477197913275:1477139933649; login_sid_t=752b624fceb9dc2cec36edd0d52acd85; UOR=,,login.sina.com.cn;SCF=Aq48LQxPt1qE63U0qWHsK8bxbvP8_cVQf74lq_fySeLLzKXgR7mwyGoBhtIa2qiADjaddlKAknPi4EuETiGbCb0.; SUB=_2A251CN2sDeTxGeNG4lEX9S3LyDqIHXVWfEhkrDV8PUNbmtBeLRHWkW8Dew228qirbEm8w5khzGUa21ZcsQ..;SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9W5WEmHY08N.cY1WYIGoLoRU5JpX5K2hUgL.Fo-R1KecSKeNe0q2dJLoIEBLxKMLB.eLBKBLxKMLB.2LBoMLxK-LBKeL1h2LxKMLB.2LBont; SUHB=0d4uo5w4_YKfuy; ALF=1508761979; SSOLoginState=1477225980; [email protected]; wvr=6'
, MASTER_UID: '5893653736'
, USER_UID: ''
CLIENT_ID: '1833831541',
CLIENT_SECRET: '204239c8f3e37e59f2d3eb95de2811a9',
ACCESS_TOKEN: '',
MASTER_ACCESS_TOKEN: '',
MASTER_UID: '5893653736',
USER_UID: ''
}
}
51 changes: 11 additions & 40 deletions routes/weibo.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,49 +32,20 @@ router.get('/callback', passport.authenticate('weibo', {
failureRedirect: '/'
}), function(req, res, next) {
req.session.weibo = weibo;
//console.log(req.cookies);
res.redirect('/weibo/send');
res.redirect('/');
});
router.get('/upload', function(req, res) {
var url = req.query.url;
request.get(url).end(function(err, response) {
if (!err) {
var base64 = new Buffer(response.body, 'binary').toString('base64');
request
.post('http://picupload.service.weibo.com/interface/pic_upload.php?mime=image%2Fjpeg&data=base64&url=0&markpos=1&logo=&nick=0&marks=1&app=miniblog&s=json')
.set({
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36',
'cookie': weibo.MASTER_COOKIES
}).type('form').send({
b64_data: base64
}).end(function(error, resp) {
//res.send(resp)
if (!error && resp.status === 200) {
var text = resp.text.substring(resp.text.indexOf('{'));
res.send(text);
} else {
console.log(error);
}
});
} else {
console.log(err);
}
});
});
router.get('/send', function(req, res) {
var url = 'https://api.weibo.com/2/statuses/upload_url_text.json';
var post = {
access_token: req.session['weibo'].ACCESS_TOKEN,
status: '#必应壁纸# 2016-10-24 / #“蛇行”的国王河# 从上空俯瞰澳大利亚温德姆,这里有一条像蛇一样弯曲着的河流——国王河。“蛇的身体”还伸出许多“触角”,那些是国王河数不清的支流,又仿佛河流的穿行让大地都龟裂了一般。河道两侧都被郁郁葱葱的植物包裹...',
url: 'http://s.cn.bing.net/az/hprichbg/rb/KingRiver_ZH-CN12008036815_1920x1080.jpg'
};
request.post(url).type('form').set({
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36'
}).send(post).end(function(err, response) {
res.json(response);
});
router.get('/send', function(req, res, next) {
if (weibo.ACCESS_TOKEN === '') {
res.redirect('/weibo');
}
weiboUtils.update(req, res, next, weibo);
});
router.get('/login', function(req, res) {
weiboUtils.login();
});
router.get('/shorten', function(req, res, next) {
weiboUtils.shorten('http://bing.ioliu.cn', function(data) {
res.send(data);
});
});
module.exports = router;
Loading

0 comments on commit 4959970

Please sign in to comment.