Created
April 11, 2018 15:52
-
-
Save superalsrk/e55a8eefd0bf17555f6f083f7acbce4f to your computer and use it in GitHub Desktop.
火山弹幕
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var log4js = require('log4js'); | |
var fivebeans = require('fivebeans'); | |
var fetch = require('node-fetch'); | |
log4js.configure({ | |
appenders: [ | |
{ type: 'console', category: 'danmuConsole'}, | |
{ type: 'file', filename: 'monitor.log', category: 'danmuConsole'}, | |
{ type: 'file', filename: 'danmu.log', category: 'danmuFile' } | |
] | |
}) | |
var logger = log4js.getLogger('danmuFile') | |
var logger4console = log4js.getLogger('danmuConsole') | |
var beanstalkClient = new fivebeans.client('localhost', 11301) | |
var raven = require('raven'); | |
var redis = require('redis') | |
var redisclient = redis.createClient({ | |
'host' : 'localhost', | |
'port' : 6371, | |
'db' : 7, | |
'password' : 'pwd' | |
}) | |
var sentry = new raven.Client('http://5e4899539af240f690fbbb17aa3dd1af:6e443964568a43eb87c83b20464f26b3@h16.mzhen.cn:9090/8'); | |
var WHOLE_CONNECTIONS = {} | |
var updateOnlineInfo = function() { | |
var ret = [] | |
Object.keys(WHOLE_CONNECTIONS).forEach(function(k) { | |
ret.push({ | |
"room_key" : WHOLE_CONNECTIONS[k].room_key, | |
"detail_url" : k | |
}) | |
}) | |
redisclient.set('huoshan_danmu_keys', JSON.stringify(ret), function(err, res) { | |
if (err) { | |
sentry.captureException(err) | |
} else { | |
logger4console.info('Sync Online Info Success') | |
} | |
}) | |
} | |
//获取直播状态 | |
var fetch_zhibo_state = function(room_key, zhibo_series) { | |
return new Promise((resolve, reject) => { | |
fetch('https://api.huoshan.com/hotsoon/room/' + zhibo_series + '/?iid=8534648360') | |
.then(function(res) { | |
return res.json() | |
}) | |
.then(function(json) { | |
let ret = { | |
"zhibo_series" : zhibo_series, | |
"room_key" : room_key, | |
"online" : 2==(json.data.status) | |
} | |
//logger4console.info(ret) | |
resolve(ret) | |
}) | |
.catch(function(err) { | |
reject(err) | |
}) | |
}) | |
} | |
var CloudSpider = function(room_key, zhibo_series) { | |
this.room_key = room_key; | |
this.zhibo_series = zhibo_series; | |
this.cursor = 0; | |
this.fetch_interval = 10000; | |
this.online = true; | |
this.leaveTagTimestamp = -1; | |
} | |
CloudSpider.prototype.init = function() { | |
this.start(); | |
return this; | |
} | |
CloudSpider.prototype.start = function() { | |
var that = this; | |
var danmu_url = 'https://api.huoshan.com/hotsoon/room/' + that.zhibo_series + '/_fetch_message_polling/?iid=8534648360&os_version=10.1.1&os_api=18&app_name=live_stream_pro&channel=App%20Store&device_platform=iphone&idfa=00000000-0000-0000-0000-000000000000&live_sdk_version=1.7.4&vid=38F1F08D-C828-4B79-8E6E-F2E92C24A8A4&openudid=73bdbcc4b6fcff3667872a2d588c5ee4f8a553be&device_type=iPhone7,2&version_code=1.7.4&ac=WIFI&screen_width=750&device_id=35336346648&aid=1115&cursor=' + that.cursor; | |
fetch_zhibo_state(that.room_key, that.zhibo_series) | |
.then(body => { | |
if (body.online == true) { | |
fetch(danmu_url) | |
.then(function(res) { | |
return res.json() | |
}) | |
.then(function(json) { | |
json.data.forEach((item) => { | |
let log_content = JSON.stringify(body) + '\t' + JSON.stringify(item) | |
logger.info(log_content) | |
}) | |
that.cursor = json.extra.cursor; | |
that.fetch_interval = json.extra.fetch_interval; | |
setTimeout(function(){ | |
that.start() | |
}, 1000) | |
}) | |
.catch(function(err) { | |
sentry.captureException(err) | |
setTimeout(function() { | |
that.start() | |
}, 1000) | |
}) | |
} else { | |
that.online = false | |
if (that.leaveTagTimestamp == -1) { | |
that.leaveTagTimestamp = parseInt((new Date()).getTime()/1000) | |
} | |
} | |
}) | |
.catch(function(err) { | |
sentry.captureException(err) | |
logger4console.error('error at last') | |
setTimeout(function() { | |
that.start() | |
}, 1000) | |
}) | |
} | |
beanstalkClient.on('connect', function() { | |
beanstalkClient.watch('remind_huoshan_danmu_seeds', function(err, response) { | |
logger4console.info('watch:' + response) | |
var coc = function() { | |
beanstalkClient.reserve(function(err, jobid, payload){ | |
logger4console.info(payload.toString('utf8')) | |
let tmpJob = JSON.parse(payload.toString('utf8')) | |
let room_key = tmpJob['room_key']; | |
let zhibo_series = tmpJob['detail_url'].replace("https://www.huoshan.com/share/room/", "").replace("/", "") | |
logger4console.info(zhibo_series) | |
if ((typeof WHOLE_CONNECTIONS[zhibo_series] != 'undefined') && (WHOLE_CONNECTIONS[zhibo_series].online == false)) { | |
delete WHOLE_CONNECTIONS[zhibo_series] | |
WHOLE_CONNECTIONS[zhibo_series] = new CloudSpider(room_key, zhibo_series).init() | |
updateOnlineInfo() | |
} else if (typeof WHOLE_CONNECTIONS[zhibo_series] == 'undefined') { | |
WHOLE_CONNECTIONS[zhibo_series] = new CloudSpider(room_key, zhibo_series).init() | |
updateOnlineInfo() | |
} | |
beanstalkClient.destroy(jobid, function(err) { | |
if (err) logger4console.error('delete job error') | |
coc() | |
}) | |
}) | |
} | |
coc() | |
}) | |
}) | |
beanstalkClient.connect() | |
// if (typeof WHOLE_CONNECTIONS['6407653404893678337'] == 'undefined') { | |
// WHOLE_CONNECTIONS['6407653404893678337'] = new CloudSpider('123456', '6407653404893678337').init() | |
// } | |
setInterval(function(){ | |
logger4console.warn("DICT SIZE: " + Object.keys(WHOLE_CONNECTIONS).length) | |
Object.keys(WHOLE_CONNECTIONS).forEach(function(k) { | |
if (WHOLE_CONNECTIONS[k].online == false) { | |
var nowTimeStamp = parseInt((new Date()).getTime()/1000); | |
if(nowTimeStamp - WHOLE_CONNECTIONS[k].leaveTagTimestamp > 120) { | |
delete WHOLE_CONNECTIONS[k] | |
} | |
} | |
}) | |
}, 5000) | |
setInterval(function() { | |
updateOnlineInfo() | |
}, 20000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment