-
Notifications
You must be signed in to change notification settings - Fork 6
/
danmu_douyu_quanmin_zhanqi.py
176 lines (153 loc) · 5.2 KB
/
danmu_douyu_quanmin_zhanqi.py
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# 使用danmu工具包监听斗鱼,全民,战旗平台弹幕礼物信息
import danmu_config
from pymongo import MongoClient
from danmu import DanMuClient
import pymysql
import time
import sys
import threading
douyu_id = 11
panda_id = 10
quanmin_id = 8
zhanqi_id = 9
bilibili_id = 15
huomao_id = 6
sql_str = "SELECT id,live_url FROM xj_star WHERE is_publish=1 AND view_num>10000 AND (live_url LIKE '%douyu%' OR live_url LIKE '%zhanqi%' OR live_url LIKE '%quanmin%')"
def get_star_info(sql):
'''
查询数据,构建关系
:param sql:
:return:
'''
dbparams = dict(
host=danmu_config.MYSQL_HOST,
port=danmu_config.MYSQL_PORT,
db=danmu_config.MYSQL_DB,
user=danmu_config.MYSQL_USER,
passwd=danmu_config.MYSQL_PASS,
charset=danmu_config.MYSQL_CHAR,
)
connect = pymysql.connect(**dbparams)
cursor = connect.cursor()
cursor.execute(sql)
result = cursor.fetchall()
connect.close()
live_list = list()
# live_url与id的关系字典
relation = dict()
for i in result:
# if i[1].find('douyu') > 0:
# live_list.append(i[1])
# relation[i[1]] = i[0]
# if i[1].find('panda'): # 熊猫运行结果无响应
# live_list.append(i[1])
if i[1].find('quanmin') > 0:
live_list.append(i[1])
relation[i[1]] = i[0]
# if i[1].find('huomao'): # 火猫运行结果无响应
# live_list.append(i[1])
if i[1].find('zhanqi') > 0:
live_list.append(i[1])
relation[i[1]] = i[0]
# if i[1].find('bilibili'): # b站运行结果无响应
# live_list.append(i[1])
return live_list, relation
def save_danmu(live_url, info, relation):
data = dict()
# client = MongoClient(host=danmu_config.MONGO_HOST, port=danmu_config.MONGO_PORT)
# db = client[danmu_config.MONGO_DB]
data['anchor_id'] = relation[live_url]
data['live_url'] = live_url
data['add_time'] = int(time.time())
data['type'] = 'danmu'
data['content'] = info['Content']
# if live_url.find('douyu') > 0:
# coll_name = danmu_config.douyu_coll
# data['platform_id'] = douyu_id
# return data, coll_name
if live_url.find('quanmin') > 0:
coll_name = danmu_config.quanmin_coll
data['platform_id'] = quanmin_id
return data, coll_name
if live_url.find('zhanqi') > 0:
coll_name = danmu_config.zhanqi_coll
data['platform_id'] = zhanqi_id
return data, coll_name
else:
return None, None
def save_gift(live_url, info, relation):
data = dict()
data['anchor_id'] = relation[live_url]
data['live_url'] = live_url
data['add_time'] = int(time.time())
data['type'] = 'gift'
# if live_url.find('douyu') > 0:
# coll_name = danmu_config.douyu_coll
# data['platform_id'] = douyu_id
# data['content'] = info['gfid']
# data['gift_num'] = 1
# return data, coll_name
if live_url.find('quanmin') > 0:
coll_name = danmu_config.quanmin_coll
data['platform_id'] = quanmin_id
data['content'] = info['Content']
# data['gift_num'] = info['Gift_num']
data['gift_num'] = 1
return data, coll_name
if live_url.find('zhanqi') > 0:
coll_name = danmu_config.zhanqi_coll
data['platform_id'] = zhanqi_id
data['content'] = info['Content']
# data['gift_num'] = info['Gift_num']
data['gift_num'] = 1
return data, coll_name
else:
return None, None
def start_danmu(live_url, relation):
'''
开始运行弹幕监听
:param live_url:
:return:
'''
client = MongoClient(host=danmu_config.MONGO_HOST, port=danmu_config.MONGO_PORT)
db = client[danmu_config.MONGO_DB]
dmc = DanMuClient(live_url)
if not dmc.isValid():
client.close()
pass
@dmc.danmu
def danmu_fn(msg):
# info = msg.encode(sys.stdin.encoding, 'ignore').decode(sys.stdin.encoding)
data, coll_name = save_danmu(live_url, msg, relation)
if data is not None and coll_name is not None:
coll = db[coll_name]
try:
coll.insert(data)
except Exception:
pass
@dmc.gift
def gift_fn(msg):
# info = msg.encode(sys.stdin.encoding, 'ignore').decode(sys.stdin.encoding)
data, coll_name = save_gift(live_url, msg, relation)
if data is not None and coll_name is not None:
coll = db[coll_name]
try:
coll.insert(data)
except Exception:
pass
dmc.start(blockThread=True)
client.close()
def run():
live_list, relation = get_star_info(sql_str)
for live_url in live_list:
t = threading.Thread(target=start_danmu, args=(live_url, relation))
t.daemon = True
t.start()
flag = True
while flag:
if len(threading.enumerate()) > 0:
flag = True
else:
flag = False
if __name__ == '__main__':
run()