forked from pymyworld/xingji100_danmu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
danmu_longzhu.js
113 lines (104 loc) · 4.5 KB
/
danmu_longzhu.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//
const longzhu_danmu = require('longzhu-danmu');
var mysql = require('mysql');
var mongoclient = require('mongodb').MongoClient;
var time = parseInt(Date.now() / 1000);
var log4js = require('log4js');
var format = require('string-format');
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth()+1;
var day = date.getDate();
var log_date = year + '-' + month + '-' + day;
var danmu_con = require('./danmu_config');
var dbhelper = require("./dbhelper").DBHelper();
// 日志配置
log4js.configure({
appenders: {
// out: {type: 'console'},
app:{type: 'file', filename: format('./log/longzhu_{}.log', log_date)}
},
categories: {
default: {appenders: ['app'], level: 'debug'}
}
});
var logger = log4js.getLogger();
// 创建一个connection
// var connection = mysql.createConnection({
// host: danmu_con.mysql_host,
// user: danmu_con.mysql_user,
// password: danmu_con.mysql_passwd,
// port: danmu_con.mysql_port,
// database: danmu_con.mysql_db,
// });
// 创建一个connection
// connection.connect();
mongoclient.connect(danmu_con.mongo_url, function (err, db) {
if(err) {
logger.debug(`${err}`);
};
var dbo = db.db('danmu');
// 执行查询语句
// connection.query('SELECT id,live_url FROM xj_star WHERE live_url LIKE "%douyu%"', function (err, results) {
dbhelper.Query('SELECT id,live_url FROM xj_star WHERE live_url LIKE "%longzhu%" AND view_num>10000 AND is_publish=1',function(err, results) {
if (err) {
logger.debug('[query] - :' + err);
return;
};
for (let i = 0; i < results.length; i++) {
const element = results[i].live_url;
const id = results[i].id;
const roomid = element.match('^http://star\.longzhu\.com/(.+)$')[1];
// 获得roomid,执行监听房间弹幕,以下均为监听代码
const client = new longzhu_danmu(roomid);
client.on('connect', () => {
logger.debug(`已连接longzhu ${roomid}房间弹幕~`);
});
client.on('message', msg => {
switch (msg.type) {
case 'chat':
// 存入mongodb数据库
var data = {anchor_id: `${id}`, live_url: `${element}`, platform_id: 4, add_time: parseInt(`${time}`), type: 'danmu', content: `${msg.content}`};
dbo.collection('longzhu').insertOne(data, function (err, result) {
if (err) {
logger.debug(`${err}`);
};
// console.log('弹幕数据插入成功');
});
// console.log(`[${msg.from.name}]:${msg.content}`);
break;
case 'gift':
// 存入mongodb数据库
var data = {anchor_id: `${id}`, live_url: `${element}`, platform_id: 4, add_time: parseInt(`${time}`), type: 'gift', content: `${msg.name}`, gift_num: `${msg.count}`};
dbo.collection('longzhu').insertOne(data, function (err, result) {
if(err) {
logger.debug(`${err}`);
};
// console.log('礼物数据插入成功');
});
// console.log(`[${msg.from.name}]->赠送${msg.count}个${msg.name}`);
break;
case 'longdou':
// 存入mongodb数据库
var data = {anchor_id: `${id}`, live_url: `${element}`, platform_id: 4, add_time: parseInt(`${time}`), type: 'gift', content: `${msg.name}`, gift_num: `${msg.count}`};
dbo.collection('longzhu').insertOne(data, function (err, result) {
if (err) {
logger.debug(`${err}`);
};
// console.log('礼物数据插入成功');
});
// console.log(`[${msg.from.name}]->赠送${msg.count}个${msg.name}`);
break;
};
});
client.on('error', e => {
logger.debug(e);
});
client.on('close', () => {
logger.debug('close');
});
client.start();
// 监听代码结束
};
});
});