-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetInvalidVideos.py
167 lines (137 loc) · 6.28 KB
/
getInvalidVideos.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
# -*- coding:utf-8 -*-
import sys
import requests
import json
import time
agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
fav_list = []
fav_name_list = []
output_index = 1
# entry point, parse command line args
# def run():
# if len(sys.argv) < 2:
# print_f('Error: Please enter Bilibili user id.')
# return
# user_id = int(sys.argv[1])
# if user_id <= 0:
# print_f('Error: Please enter a valid Bilibili user id.')
# return
# get_fav_videos_from_user(user_id)
# main function flow: get all fav folders
# -> loop through fav folder id
# -> loop through page in that folder id
# -> loop all video info to find invalid ones and record them
# -> write to output file
# -> done
def get_fav_videos_from_user(uid):
global fav_list
global fav_name_list
global output_index
fav_list = []
fav_name_list = []
output_index = 1
get_fav_folder_list(uid) #get all fav folders, id store to fav_list, names store to fav_name_list
output_list = []
if len(fav_list) == 0:
print_f('user mid={user} don\'t have public fav folders. if you own this mid, make your fav folders public.'.format(user=uid))
return output_list
for i in range(0, len(fav_list)): # loop all fav folders
time.sleep(0.5)
s = process_fav_folder(uid, i) # current fav folder jObject, constain all video infos
output_list += s
# write_output(output)
print(output_list)
return output_list
# find all fav folders ids and names.
def get_fav_folder_list(uid):
global fav_list
url = 'https://api.bilibili.com/x/space/fav/nav?mid={userid}&jsonp=jsonp'.format(userid=uid)
resp = get_HTML_text(url, agent)
responed_jobject = json.loads(resp)
archive = responed_jobject['data']['archive']
for i in range(0, len(archive)):
fav_folder_obj = archive[i]
fav_folder_id = fav_folder_obj['fid']
fav_folder_name = fav_folder_obj['name']
fav_list.append(fav_folder_id)
fav_name_list.append(fav_folder_name)
print_f('mid={user} has fav folders: {folder}'.format(user=uid, folder=fav_name_list))
# given a fav folder id, find pages and parse videos info
def process_fav_folder(uid, fav_list_index):
global fav_list
fav_folder_content_list = []
url = 'https://api.bilibili.com/x/space/fav/arc?vmid={userid}&ps=30&fid={favid}&tid=0&keyword=&pn=1&order=fav_time&jsonp=jsonp'.format(userid=uid, favid=fav_list[fav_list_index])
resp = get_HTML_text(url, agent)
# print('API返回结果:', resp)
responed_jobject = json.loads(resp)
# 判断api调用是否正常
if responed_jobject['code'] == 0:
page_count = responed_jobject['data']['pagecount']
else:
return fav_folder_content_list
print_f('{favid} has {page} pages.'.format(id=uid, favid=fav_name_list[fav_list_index], page=page_count))
video_jobject = responed_jobject['data']['archives']
fav_folder_content_list += handle_jobject_per_page(video_jobject, fav_list_index, 1)
for i in range(2, page_count + 1):
url = 'https://api.bilibili.com/x/space/fav/arc?vmid={userid}&ps=30&fid={favid}&tid=0&keyword=&pn={page_index}&order=fav_time&jsonp=jsonp'.format(userid=uid, favid=fav_list[fav_list_index], page_index=i)
resp = get_HTML_text(url, agent)
responed_jobject = json.loads(resp)
video_jobject = responed_jobject['data']['archives']
fav_folder_content_list += handle_jobject_per_page(video_jobject, fav_list_index, i)
return fav_folder_content_list
# each page contains 30 videos, I tried passing 1000, do NOT work. :(
# parse videos in give page, write individual video info to output.
# here you can custom what info you want, and the format you want. For avaliable elements, print 'jObject.Keys'
def handle_jobject_per_page(page_jobjects, fav_list_index, page_index):
global output_index
global fav_name_list
return_list = []
# page_info = ''
valid_count = 0
invalid_count = 0
for i in range(0, len(page_jobjects)):
jObject = page_jobjects[i]
if int(jObject['state']) >= 0:
valid_count += 1
continue
invalid_count += 1
# s = '#{number}{title}\n'.format(number=output_index, title=jObject['title'])
# s += 'AV{vid} 收藏夹:{favFolder},Page:{page},Index:{num}\n'.format(vid=jObject['aid'], favFolder=fav_name_list[fav_list_index], page=page_index, num=i)
# s += 'UP主:{up}<space.bilibili.com/{mid}>\n'.format(up=jObject['owner']['name'], mid=jObject['owner']['mid'])
# s += '注释:\n{desc}\n\n\n'.format(desc=jObject['desc'])
return_dist = {}
return_dist["title"] = jObject['title']
return_dist["av_id"] = jObject['aid']
return_dist["fav_name"] = fav_name_list[fav_list_index]
return_dist["fav_page"] = page_index
return_dist["fav_page_index"] = i
return_dist["up_id"] = jObject['owner']['name']
return_dist["up_url"] = 'https://space.bilibili.com/' + str(jObject['owner']['mid'])
return_dist["description"] = jObject['desc']
return_list.append(return_dist)
# page_info += s
output_index += 1
print_f(' {}:Page {} has {} valid videos and {} invalid videos, index reach to \'{}\'. '.format(fav_name_list[fav_list_index], page_index, valid_count, invalid_count, output_index))
return return_list
# write infos to output file
def write_output(info):
file = open('invalidFavVideos.txt', 'w', encoding='utf-8')
file.write(info)
file.close()
print_f("\nDone! Outputing invalid {} videos. Happy holding and have a nice day~ \[T] /\n".format(output_index))
def get_HTML_text(url, agent):
try:
headers = {'User-Agent': agent}
r = requests.get(url, timeout=30, headers=headers)
r.raise_for_status()
r.encoding = r.apparent_encoding
return r.text
except:
return 'Error: Unable to query Bilibili server!'
def print_f(info):
if sys.getdefaultencoding() == 'ascii':
info.encode('gb2312')
print(info)
else:
info.encode('utf-8')
print(info)