forked from g497813927/Bilibili-comment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
comment_dynamic.py
202 lines (192 loc) · 9.56 KB
/
comment_dynamic.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import json
import requests
import time
with open('dynamic_types.json', 'r') as load_file:
dynamic_types = json.load(load_file)
class Comment(object):
def __init__(self, dynamic_id):
super().__init__()
self.session = requests.Session()
self.dynamic_detail = self.get_dynamic_details(dynamic_id)
def get_comment_uid_list(self, sort=2, ps=49, counter=1, refresh_interval=5, sleep=1, next_reply=0, remove_repeat=True):
# Technique to avoid 412
if counter % refresh_interval == 0:
time.sleep(sleep)
# Detect reply type & oid
dynamic_type = str(self.dynamic_detail['data']['card']['desc']['type'])
if dynamic_type in dynamic_types['types']:
type_data = dynamic_types['types'][dynamic_type]
type_oid_path = type_data['path']
oid = self.dynamic_detail
for k in type_oid_path:
oid = oid[k]
type_value = type_data['value']
# API - Only Display Main Replies
# comment_url = 'https://api.bilibili.com/x/v2/reply?type={}&oid={}&sort={}&ps={}&pn={}'.format(type_value, oid, sort, ps, pn)
# API - Replies with Part of Sub-replies
comment_url = 'https://api.bilibili.com/x/v2/reply/main?type={}&oid={}&mode={}&ps={}&next={}'.format(type_value, oid, sort, ps, next_reply)
# Obtain reply for current page
while True:
try:
topic_info_response = self.session.get(comment_url)
break
except:
time.sleep(1)
comment_info = json.loads(topic_info_response.content.decode())
if comment_info['code'] != 0:
if comment_info['code'] == -404:
raise ValueError
elif comment_info['code'] == -400:
raise ValueError
else:
raise ValueError
reply_list = comment_info['data']['replies']
# Total Count Approach for API - Only Display Main Replies
# total_replies = comment_info['data']['page']['count']
# Total Count Approach for API - Replies with Part of Sub-replies
# total_replies = comment_info['data']['cursor']['all_count']
uid_list = []
# Avoid error caused by empty replies
if reply_list is None:
return []
# Add UID for each reply
for reply in reply_list:
uid_list.append(reply['mid'])
# Check if there is another page for API - Only Display Main Replies
# if total_replies - ps * counter > 0:
# uid_list.extend(self.get_comment_uid_list(sort, ps, counter + 1))
next_reply = comment_info['data']['cursor']['next']
if next_reply != 1:
uid_list.extend(self.get_comment_uid_list(sort, ps, counter+1, next_reply=next_reply))
if remove_repeat:
uid_list = list(set(uid_list))
return uid_list
def get_dynamic_details(self, dynamic_id):
dynamic_detail_url = 'https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/get_dynamic_detail?dynamic_id={}'.format(dynamic_id)
while True:
try:
dynamic_detail_response = self.session.get(dynamic_detail_url)
break
except:
time.sleep(1)
dynamic_detail = json.loads(dynamic_detail_response.content.decode())
if dynamic_detail['code'] != 0:
if dynamic_detail['code'] == -404:
raise ValueError
elif dynamic_detail['code'] == -400:
raise ValueError
else:
raise ValueError
else:
return dynamic_detail
def get_comment_uname_list(self, sort=2, ps=49, counter=1, refresh_interval=5, sleep=1, next_reply=0, remove_repeat=True):
# Technique to avoid 412
if counter % refresh_interval == 0:
time.sleep(sleep)
# Detect reply type & oid
dynamic_type = str(self.dynamic_detail['data']['card']['desc']['type'])
if dynamic_type in dynamic_types['types']:
type_data = dynamic_types['types'][dynamic_type]
type_oid_path = type_data['path']
oid = self.dynamic_detail
for k in type_oid_path:
oid = oid[k]
type_value = type_data['value']
# API - Only Display Main Replies
# comment_url = 'https://api.bilibili.com/x/v2/reply?type={}&oid={}&sort={}&ps={}&pn={}'.format(type_value, oid, sort, ps, pn)
# API - Replies with Part of Sub-replies
comment_url = 'https://api.bilibili.com/x/v2/reply/main?type={}&oid={}&mode={}&ps={}&next={}'.format(type_value, oid, sort, ps, next_reply)
# Obtain reply for current page
while True:
try:
topic_info_response = self.session.get(comment_url)
break
except:
time.sleep(1)
comment_info = json.loads(topic_info_response.content.decode())
if comment_info['code'] != 0:
if comment_info['code'] == -404:
raise ValueError
elif comment_info['code'] == -400:
raise ValueError
else:
raise ValueError
reply_list = comment_info['data']['replies']
# Total Count Approach for API - Only Display Main Replies
# total_replies = comment_info['data']['page']['count']
# Total Count Approach for API - Replies with Part of Sub-replies
# total_replies = comment_info['data']['cursor']['all_count']
uname_list = []
# Avoid error caused by empty replies
if reply_list is None:
return []
# Add UID for each reply
for reply in reply_list:
uname_list.append(reply['member']['uname'])
# Check if there is another page for API - Only Display Main Replies
# if total_replies - ps * counter > 0:
# uid_list.extend(self.get_comment_uid_list(sort, ps, counter + 1))
next_reply = comment_info['data']['cursor']['next']
if next_reply != 1:
uname_list.extend(self.get_comment_uname_list(sort, ps, counter + 1, next_reply=next_reply))
if remove_repeat:
uname_list = list(set(uname_list))
return uname_list
def get_comment_user_list(self, sort=2, ps=49, counter=1, refresh_interval=5, sleep=1, next_reply=0,
remove_repeat=True):
# Technique to avoid 412
if counter % refresh_interval == 0:
time.sleep(sleep)
# Detect reply type & oid
dynamic_type = str(self.dynamic_detail['data']['card']['desc']['type'])
if dynamic_type in dynamic_types['types']:
type_data = dynamic_types['types'][dynamic_type]
type_oid_path = type_data['path']
oid = self.dynamic_detail
for k in type_oid_path:
oid = oid[k]
type_value = type_data['value']
# API - Only Display Main Replies
# comment_url = 'https://api.bilibili.com/x/v2/reply?type={}&oid={}&sort={}&ps={}&pn={}'.format(type_value, oid, sort, ps, pn)
# API - Replies with Part of Sub-replies
comment_url = 'https://api.bilibili.com/x/v2/reply/main?type={}&oid={}&mode={}&ps={}&next={}'.format(
type_value, oid, sort, ps, next_reply)
# Obtain reply for current page
while True:
try:
topic_info_response = self.session.get(comment_url)
break
except:
time.sleep(1)
comment_info = json.loads(topic_info_response.content.decode())
if comment_info['code'] != 0:
if comment_info['code'] == -404:
raise ValueError
elif comment_info['code'] == -400:
raise ValueError
else:
raise ValueError
reply_list = comment_info['data']['replies']
# Total Count Approach for API - Only Display Main Replies
# total_replies = comment_info['data']['page']['count']
# Total Count Approach for API - Replies with Part of Sub-replies
# total_replies = comment_info['data']['cursor']['all_count']
user_list = ''
# Avoid error caused by empty replies
if reply_list is None:
return ''
# Add UID for each reply
for reply in reply_list:
# user_list += str.format('{{\"username\": \"{}\", \"uid\": {}}}, ', reply['member']['uname'], reply['mid'])
user_list += str.format('\"{}\": \"{}\", ', reply['mid'], reply['member']['uname'])
# Check if there is another page for API - Only Display Main Replies
# if total_replies - ps * counter > 0:
# uid_list.extend(self.get_comment_uid_list(sort, ps, counter + 1))
next_reply = comment_info['data']['cursor']['next']
if next_reply != 1:
user_list += (self.get_comment_user_list(sort, ps, counter + 1, next_reply=next_reply))
if counter == 1:
# user_list = '[' + user_list[:(len(user_list) - 2)] + ']'
user_list = '{' + user_list[:(len(user_list) - 2)] + '}'
user_list = json.loads(user_list)
return user_list