b站个人中心.py 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827
  1. # coding=utf-8
  2. # !/usr/bin/python
  3. import sys
  4. sys.path.append('..')
  5. from base.spider import Spider
  6. import json
  7. import requests
  8. from requests import session, utils
  9. import os
  10. import time
  11. import base64
  12. from time import strftime
  13. from time import gmtime
  14. import time
  15. time_stamp = str(int(time.time()))
  16. import re
  17. from urllib import request, parse
  18. import urllib.request
  19. class Spider(Spider): # 元类 默认的元类 type
  20. box_video_type = ''
  21. vod_area=''
  22. def getName(self):
  23. return "B站个人中心"#自己增加关注pu主和pu主直播(以关注的)
  24. def __init__(self):
  25. self.getCookie()
  26. url = 'http://api.bilibili.com/x/v3/fav/folder/created/list-all?up_mid=%s&jsonp=jsonp' % (self.userid)
  27. rsp = self.fetch(url, cookies=self.cookies)
  28. content = rsp.text
  29. jo = json.loads(content)
  30. fav_list=[]
  31. if jo['code'] == 0:
  32. for fav in jo['data'].get('list'):
  33. fav_dict = {'n':fav['title'].replace("<em class=\"keyword\">", "").replace("</em>", "").replace("&quot;", '"').strip() ,'v':fav['id']}
  34. fav_list.append(fav_dict)
  35. if self.config["filter"].get('收藏夹'):
  36. for i in self.config["filter"].get('收藏夹'):
  37. if i['key']=='mlid':
  38. i['value']=fav_list
  39. def init(self, extend=""):
  40. print("============{0}============".format(extend))
  41. pass
  42. def isVideoFormat(self, url):
  43. pass
  44. def second_to_time(self,a):
  45. #将秒数转化为 时分秒的格式
  46. if a < 3600:
  47. return time.strftime("%M:%S", time.gmtime(a))
  48. else:
  49. return time.strftime("%H:%M:%S", time.gmtime(a))
  50. def manualVideoCheck(self):
  51. pass
  52. #用户userid
  53. userid=''
  54. def get_live_userInfo(self,uid):
  55. url = 'https://api.live.bilibili.com/live_user/v1/Master/info?uid=%s'%uid
  56. rsp = self.fetch(url, cookies=self.cookies)
  57. content = rsp.text
  58. jo = json.loads(content)
  59. if jo['code'] == 0:
  60. return jo['data']["info"]["uname"]
  61. def homeContent(self, filter):
  62. result = {}
  63. cateManual = {
  64. "动态": "动态",
  65. #"关注的pu主":'关注的pu主',
  66. "直播中":'直播中',
  67. "收藏夹": '收藏夹',
  68. "历史记录": '历史记录',
  69. "稍后再看": '稍后再看',
  70. '生活直播':'生活直播',
  71. "热门": "热门",
  72. "排行榜": "排行榜",
  73. "频道": "频道",
  74. "直播": "直播",
  75. "舞蹈": "舞蹈",
  76. "宅舞": "宅舞",
  77. "少女": "少女",
  78. 'cosplay':'cosplay',
  79. 'mmd':'mmd',
  80. "鬼畜": "鬼畜",
  81. "狗狗": "汪星人",
  82. '科技': '科技',
  83. "音声": "音声",
  84. "演唱会": "演唱会",
  85. "番剧": "1",
  86. "国创": "4",
  87. "电影": "2",
  88. "综艺": "7",
  89. "电视剧": "5",
  90. "纪录片": "3",
  91. }
  92. classes = []
  93. for k in cateManual:
  94. classes.append({
  95. 'type_name': k,
  96. 'type_id': cateManual[k]
  97. })
  98. result['class'] = classes
  99. if (filter):
  100. result['filters'] = self.config['filter']
  101. return result
  102. def homeVideoContent(self):
  103. self.box_video_type = '热门'
  104. return self.get_hot(pg='1')
  105. cookies = ''
  106. def getCookie(self):
  107. #在下方cookies_str 后面 双引号里面放置你的cookies
  108. cookies_str = "buvid3=8905B4AA-C4CE-4976-853F-57A11EEA856018556infoc; LIVE_BUVID=AUTO8016374005231370; blackside_state=0; buvid_fp_plain=undefined; CURRENT_BLACKGAP=0; fingerprint3=d7577ba1214002a3b3e99ef5a54c11cd; b_nut=100; _uuid=2BD33E68-A3C6-35104-F58A-4491764A7ED772232infoc; buvid4=A698BE3F-38C2-EAC2-179C-A1E59A412ADB47072-022012415-5ZCNRwNsIx2%2Biu7YVq0ofQ%3D%3D; rpdid=|(u))ul)|YJ~0J'uY~|~RYu|~; fingerprint=c3e80ad851e53d02c0d45180af39c827; buvid_fp=d459047df2c1bc4ebd104bb95c3e6c89; hit-new-style-dyn=1; CURRENT_PID=b7f74190-d1f9-11ed-b9f0-fd43ad325ff3; DedeUserID=671023938; DedeUserID__ckMd5=48c152aca085bcce; hit-dyn-v2=1; PVID=1; CURRENT_FNVAL=4048; nostalgia_conf=-1; bp_video_offset_671023938=798457776625942500; SESSDATA=f1c51afc%2C1700472321%2C82dd5%2A51; bili_jct=73f21a5f7e21ff2ed166608e9d327ac3; sid=gil05bhj"
  109. if cookies_str:
  110. cookies = dict([co.strip().split('=') for co in cookies_str.split(';')])
  111. bili_jct = cookies['bili_jct']
  112. SESSDATA = cookies['SESSDATA']
  113. DedeUserID = cookies['DedeUserID']
  114. cookies_jar={"bili_jct":bili_jct,
  115. 'SESSDATA': SESSDATA,
  116. 'DedeUserID':DedeUserID
  117. }
  118. rsp = session()
  119. rsp.cookies = cookies_jar
  120. content = self.fetch("http://api.bilibili.com/x/web-interface/nav", cookies=rsp.cookies)
  121. res = json.loads(content.text)
  122. if res["code"] == 0:
  123. self.cookies = rsp.cookies
  124. self.userid = res["data"].get('mid')
  125. return rsp.cookies
  126. rsp = self.fetch("https://www.bilibili.com/")
  127. self.cookies = rsp.cookies
  128. return rsp.cookies
  129. def get_hot(self, pg):
  130. self.box_video_type = '热门'
  131. result = {}
  132. url = 'https://api.bilibili.com/x/web-interface/popular?ps=20&pn={0}'.format(pg)
  133. rsp = self.fetch(url, cookies=self.cookies)
  134. content = rsp.text
  135. jo = json.loads(content)
  136. if jo['code'] == 0:
  137. videos = []
  138. vodList = jo['data']['list']
  139. for vod in vodList:
  140. aid = str(vod['aid']).strip()
  141. title = vod['title'].strip().replace("<em class=\"keyword\">", "").replace("</em>", "")
  142. img = vod['pic'].strip()
  143. remark = str(self.second_to_time(vod['duration'])).strip()
  144. videos.append({
  145. "vod_id": aid+'&hot',
  146. "vod_name": title,
  147. "vod_pic": img,
  148. "vod_remarks": remark
  149. })
  150. result['list'] = videos
  151. result['page'] = pg
  152. result['pagecount'] = 9999
  153. result['limit'] = 90
  154. result['total'] = 999999
  155. return result
  156. def str2sec(self,x):
  157. '''
  158. 字符串时分秒转换成秒
  159. '''
  160. x=str(x)
  161. try:
  162. h, m, s = x.strip().split(':') #.split()函数将其通过':'分隔开,.strip()函数用来除去空格
  163. return int(h)*3600 + int(m)*60 + int(s) #int()函数转换成整数运算
  164. except:
  165. m, s = x.strip().split(':') #.split()函数将其通过':'分隔开,.strip()函数用来除去空格
  166. return int(m)*60 + int(s) #int()函数转换成整数运算
  167. def get_rank(self):
  168. self.box_video_type = '排行榜'
  169. result = {}
  170. url = 'https://api.bilibili.com/x/web-interface/ranking/v2?rid=0&type=all'
  171. rsp = self.fetch(url, cookies=self.cookies)
  172. content = rsp.text
  173. jo = json.loads(content)
  174. if jo['code'] == 0:
  175. videos = []
  176. vodList = jo['data']['list']
  177. for vod in vodList:
  178. aid = str(vod['aid']).strip()
  179. title = vod['title'].strip().replace("<em class=\"keyword\">", "").replace("</em>", "")
  180. img = vod['pic'].strip()
  181. remark = str(self.second_to_time(vod['duration'])).strip()
  182. videos.append({
  183. "vod_id": aid+'&rank',
  184. "vod_name": title,
  185. "vod_pic": img,
  186. "vod_remarks": remark
  187. })
  188. result['list'] = videos
  189. result['page'] = 1
  190. result['pagecount'] = 1
  191. result['limit'] = 90
  192. result['total'] = 999999
  193. return result
  194. def filter_duration(self, vodlist, key):
  195. # 按时间过滤
  196. if key == '0':
  197. return vodlist
  198. else:
  199. vod_list_new = [i for i in vodlist if self.time_diff1[key][0] <= self.str2sec(str(i["vod_remarks"])) < self.time_diff1[key][1]]
  200. return vod_list_new
  201. chanel_offset=''
  202. def get_channel(self, pg, cid,extend,order,duration_diff):
  203. result = {}
  204. self.box_video_type = '频道'
  205. url = 'https://api.bilibili.com/x/web-interface/search/type?search_type=video&keyword={0}&page={1}&duration={2}&order={3}'.format(
  206. cid, pg,duration_diff,order)
  207. rsp = self.fetch(url, cookies=self.cookies)
  208. content = rsp.text
  209. jo = json.loads(content)
  210. if jo.get('code') == 0:
  211. videos = []
  212. vodList = jo['data']['result']
  213. for vod in vodList:
  214. aid = str(vod['aid']).strip()
  215. title = vod['title'].replace("<em class=\"keyword\">", "").replace("</em>", "").replace("&quot;", '"')
  216. img = 'https:' + vod['pic'].strip()
  217. remark = str( self.second_to_time(self.str2sec(vod['duration']))).strip()
  218. videos.append({
  219. "vod_id": aid+'&channale',
  220. "vod_name": title,
  221. "vod_pic": img,
  222. "vod_remarks": remark
  223. })
  224. #videos=self.filter_duration(videos, duration_diff)
  225. result['list'] = videos
  226. result['page'] = pg
  227. result['pagecount'] = 9999
  228. result['limit'] = 90
  229. result['total'] = 999999
  230. return result
  231. dynamic_offset = ''
  232. def get_dynamic(self, pg):
  233. self.box_video_type = '动态'
  234. result = {}
  235. if str(pg) == '1':
  236. url = 'https://api.bilibili.com/x/polymer/web-dynamic/v1/feed/all?timezone_offset=-480&type=all&page=%s' % pg
  237. else:
  238. # print('偏移',self.dynamic_offset)
  239. url = 'https://api.bilibili.com/x/polymer/web-dynamic/v1/feed/all?timezone_offset=-480&type=all&offset=%s&page=%s' % (
  240. self.dynamic_offset, pg)
  241. rsp = self.fetch(url, cookies=self.cookies)
  242. content = rsp.text
  243. jo = json.loads(content)
  244. if jo['code'] == 0:
  245. self.dynamic_offset = jo['data'].get('offset')
  246. videos = []
  247. vodList = jo['data']['items']
  248. for vod in vodList:
  249. if vod['type'] == 'DYNAMIC_TYPE_AV':
  250. #up=vod['modules']["module_author"]['name']
  251. ivod = vod['modules']['module_dynamic']['major']['archive']
  252. aid = str(ivod['aid']).strip()
  253. title = ivod['title'].strip().replace("<em class=\"keyword\">", "").replace("</em>", "")
  254. img = ivod['cover'].strip()
  255. #remark = str(ivod['duration_text']).strip()
  256. remark =str( self.second_to_time(self.str2sec(ivod['duration_text']))).strip()
  257. videos.append({
  258. "vod_id": aid+'&dynamic',
  259. "vod_name": title,
  260. "vod_pic": img,
  261. "vod_remarks": remark
  262. })
  263. result['list'] = videos
  264. result['page'] = pg
  265. result['pagecount'] = 9999
  266. result['limit'] = 90
  267. result['total'] = 999999
  268. return result
  269. def get_pu(self,pg):
  270. result = {}
  271. if int(pg) > 50:
  272. return result
  273. videos = []
  274. vmid=self.userid
  275. url= 'https://api.bilibili.com/x/relation/followings?vmid={1}&pn={0}&ps=20&order=desc&order_type=attention'.format(pg,vmid)
  276. rsp = self.fetch(url, cookies=self.cookies)
  277. content =rsp.text
  278. jo = json.loads(content)
  279. if jo['code'] == 0:
  280. vodList = jo['data']['list']
  281. for vod in vodList:
  282. aid = str(vod['mid']).strip()
  283. title = vod['uname'].strip()
  284. img = vod['face'].strip()
  285. remark = str(vod['sign']).strip()
  286. desc=str(vod['official_verify']['desc']).strip()
  287. videos.append({
  288. "vod_id":title+"###"+aid+"###"+img+"###"+desc+"###"+remark+"&pu",
  289. "vod_name":title,
  290. "vod_pic":img,
  291. "vod_remarks":remark
  292. })
  293. numvL = len(videos)
  294. result['list'] = videos
  295. result['page'] = pg
  296. result['pagecount'] = int(pg)+1 if numvL>19 else pg
  297. result['limit'] = numvL
  298. result['total'] = numvL
  299. return result
  300. time_diff1={'1':[0,300],
  301. '2':[300,900],'3':[900,1800],'4':[1800,3600],
  302. '5':[3600,99999999999999999999999999999999]
  303. }
  304. time_diff='0'
  305. def get_fav_detail(self,pg,mlid,order):
  306. result = {}
  307. self.box_video_type = '收藏夹'
  308. url = 'http://api.bilibili.com/x/v3/fav/resource/list?media_id=%s&order=%s&pn=%s&ps=20&platform=web&type=0'%(mlid,order,pg)
  309. rsp = self.fetch(url, cookies=self.cookies)
  310. content = rsp.text
  311. jo = json.loads(content)
  312. if jo['code'] == 0:
  313. videos = []
  314. vodList = jo['data']['medias']
  315. for vod in vodList:
  316. #print(vod)
  317. #只展示类型为 视频的条目
  318. #过滤去掉收藏夹中的 已失效视频;如果不喜欢可以去掉这个 if条件
  319. if vod.get('type') in [2] and vod.get('title') != '已失效视频':
  320. aid = str(vod['id']).strip()
  321. title = vod['title'].replace("<em class=\"keyword\">", "").replace("</em>", "").replace("&quot;", '"')
  322. img = vod['cover'].strip()
  323. remark = str( self.second_to_time(vod['duration'])).strip()
  324. videos.append({
  325. "vod_id": aid+'&fav',
  326. "vod_name": title,
  327. "vod_pic": img,
  328. "vod_remarks": remark
  329. })
  330. #videos=self.filter_duration(videos, duration_diff)
  331. result['list'] = videos
  332. result['page'] = pg
  333. result['pagecount'] = 9999
  334. result['limit'] = 90
  335. result['total'] = 999999
  336. return result
  337. def get_fav(self,pg,order,extend):
  338. self.box_video_type = '收藏夹'
  339. #获取自己的up_mid(也就是用户uid)
  340. mlid=''
  341. fav_config=self.config["filter"].get('收藏夹')
  342. #默认显示第一个收藏夹内容
  343. if fav_config:
  344. for i in fav_config:
  345. if i['key']=='mlid':
  346. if len(i['value'])>0:
  347. mlid=i['value'][0]['v']
  348. #print(self.config["filter"].get('收藏夹'))
  349. if 'mlid' in extend:
  350. mlid = extend['mlid']
  351. if mlid:
  352. return self.get_fav_detail(pg=pg,mlid=mlid,order=order)
  353. else:
  354. return {}
  355. def get_history(self,pg):
  356. result = {}
  357. self.box_video_type = '历史记录'
  358. url = 'http://api.bilibili.com/x/v2/history?pn=%s&t=time_stamp' % pg
  359. rsp = self.fetch(url,cookies=self.cookies)
  360. content = rsp.text
  361. jo = json.loads(content) #解析api接口,转化成json数据对象
  362. if jo['code'] == 0:
  363. videos = []
  364. vodList = jo['data']
  365. for vod in vodList:
  366. if vod['duration'] > 0: #筛选掉非视频的历史记录
  367. aid = str(vod["aid"]).strip() #获取 aid
  368. #获取标题
  369. title = vod["title"].replace("<em class=\"keyword\">", "").replace("</em>", "").replace("&quot;",
  370. '"')
  371. #封面图片
  372. img = vod["pic"].strip()
  373. #获取已观看时间
  374. if str(vod['progress'])=='-1':
  375. process=str(self.second_to_time(vod['duration'])).strip()
  376. else:
  377. process = str(self.second_to_time(vod['progress'])).strip()
  378. #获取视频总时长
  379. total_time= str(self.second_to_time(vod['duration'])).strip()
  380. #组合 已观看时间 / 总时长 ,赋值给 remark
  381. remark = process+' / '+total_time
  382. videos.append({
  383. "vod_id":aid+'&history',
  384. "vod_name": title,
  385. "vod_pic": img,
  386. "vod_remarks": remark
  387. })
  388. result['list'] = videos
  389. result['page'] = pg
  390. result['pagecount'] = 9999
  391. result['limit'] = 90
  392. result['total'] = 999999
  393. return result
  394. def get_toview(self,pg):
  395. result = {}
  396. self.box_video_type = '稍后再看'
  397. url = 'http://api.bilibili.com/x/v2/history/toview'
  398. rsp = self.fetch(url,cookies=self.cookies)
  399. content = rsp.text
  400. jo = json.loads(content) #解析api接口,转化成json数据对象
  401. if jo['code'] == 0:
  402. videos = []
  403. vodList = jo['data'].get('list')
  404. for vod in vodList:
  405. if vod['duration'] > 0: #筛选掉非视频的历史记录
  406. aid = str(vod["aid"]).strip() #获取 aid
  407. #获取标题
  408. title = vod["title"].replace("<em class=\"keyword\">", "").replace("</em>", "").replace("&quot;",
  409. '"')
  410. #封面图片
  411. img = vod["pic"].strip()
  412. #获取已观看时间
  413. if str(vod['progress'])=='-1':
  414. process=str(self.second_to_time(vod['duration'])).strip()
  415. else:
  416. process = str(self.second_to_time(vod['progress'])).strip()
  417. #获取视频总时长
  418. total_time= str(self.second_to_time(vod['duration'])).strip()
  419. #组合 已观看时间 / 总时长 ,赋值给 remark
  420. remark = process+' / '+total_time
  421. videos.append({
  422. "vod_id":aid+'&toview',
  423. "vod_name": title,
  424. "vod_pic": img,
  425. "vod_remarks": remark
  426. })
  427. result['list'] = videos
  428. result['page'] = pg
  429. result['pagecount'] = pg
  430. result['limit'] = len(videos)
  431. result['total'] = len(videos)
  432. return result
  433. def get_live(self,pg,parent_area_id,area_id):
  434. result = {}
  435. self.box_video_type = '直播'
  436. url = 'https://api.live.bilibili.com/xlive/web-interface/v1/second/getList?platform=web&parent_area_id=%s&area_id=%s&sort_type=online&page=%s'%(parent_area_id,area_id,pg)
  437. rsp = self.fetch(url, cookies=self.cookies)
  438. content = rsp.text
  439. jo = json.loads(content)
  440. if jo['code'] == 0:
  441. videos = []
  442. vodList = jo['data']['list']
  443. for vod in vodList:
  444. aid = str(vod['roomid']).strip()
  445. title = vod['title'].replace("<em class=\"keyword\">", "").replace("</em>", "").replace("&quot;", '"')
  446. img = vod.get('cover').strip()
  447. remark = '直播间人数:'+str( vod['online']).strip()
  448. videos.append({
  449. "vod_id": aid+'&live',
  450. "vod_name": title,
  451. "vod_pic": img,
  452. "vod_remarks": remark
  453. })
  454. #videos=self.filter_duration(videos, duration_diff)
  455. result['list'] = videos
  456. result['page'] = pg
  457. result['pagecount'] = 9999
  458. result['limit'] = 90
  459. result['total'] = 999999
  460. return result
  461. def get_live_goodFor(self,pg):
  462. result = {}
  463. self.box_video_type = '直播'
  464. ts=str(int(time.time()*1000))
  465. url = 'https://api.live.bilibili.com/xlive/web-ucenter/v1/xfetter/GetWebList?page={0}&page_size=10&_={1}'.format(pg,ts)
  466. rsp = self.fetch(url, cookies=self.cookies)
  467. content = rsp.text
  468. jo = json.loads(content)
  469. if jo['code'] == 0:
  470. videos = []
  471. vodList = jo['data']['list']
  472. for vod in vodList:
  473. aid = str(vod['room_id']).strip()
  474. title = vod['title'].replace("<em class=\"keyword\">", "").replace("</em>", "").replace("&quot;", '"')
  475. img = vod.get('keyframe').strip()
  476. remark = '直播间人数:'+str( vod['online']).strip()
  477. videos.append({
  478. "vod_id": aid+'&live',
  479. "vod_name": title,
  480. "vod_pic": img,
  481. "vod_remarks": remark
  482. })
  483. result['list'] = videos
  484. result['page'] = pg
  485. result['pagecount'] = int(pg)+1 if len(videos)==10 else int(pg)
  486. result['limit'] = 90
  487. result['total'] = 999999
  488. return result
  489. def categoryContent(self, tid, pg, filter, extend):
  490. result = {}
  491. if len(self.cookies) <= 0:
  492. self.getCookie()
  493. if tid == "热门":
  494. self.box_video_type = '热门'
  495. return self.get_hot(pg=pg)
  496. elif tid == "关注的pu主":
  497. self.box_video_type = '关注的pu主'
  498. return self.get_pu(pg=pg)
  499. elif tid == "排行榜":
  500. self.box_video_type = '排行榜'
  501. return self.get_rank()
  502. elif tid== '稍后再看':
  503. self.box_video_type = '稍后再看'
  504. return self.get_toview(pg)
  505. elif tid == "收藏夹":
  506. self.box_video_type = '收藏夹'
  507. order = 'mtime'
  508. if 'order' in extend:
  509. order = extend['order']
  510. return self.get_fav(pg=pg, order=order,extend=extend)
  511. elif tid == '生活直播':
  512. self.box_video_type = '直播'
  513. parent_area_id = '10'
  514. area_id=''
  515. if 'area_id' in extend:
  516. area_id = extend['area_id']
  517. return self.get_live(pg=pg,parent_area_id=parent_area_id,area_id=area_id)
  518. elif tid == '直播':
  519. self.box_video_type = '直播'
  520. parent_area_id = '1'
  521. if 'parent_area_id' in extend:
  522. parent_area_id = extend['parent_area_id']
  523. return self.get_live(pg=pg,parent_area_id=parent_area_id,area_id='')
  524. elif tid == '直播中':
  525. self.box_video_type = '直播'
  526. return self.get_live_goodFor(pg=pg)
  527. elif tid == '频道':
  528. self.box_video_type = '频道'
  529. cid = '搞笑'
  530. if 'cid' in extend:
  531. cid = extend['cid']
  532. duration_diff='0'
  533. if 'duration' in extend:
  534. duration_diff = extend['duration']
  535. order = 'totalrank'
  536. if 'order' in extend:
  537. order = extend['order']
  538. return self.get_channel(pg=pg, cid=cid,extend=extend,order=order,duration_diff=duration_diff)
  539. elif tid == '动态':
  540. self.box_video_type = '动态'
  541. return self.get_dynamic(pg=pg)
  542. elif tid == '历史记录':
  543. self.box_video_type = '历史记录'
  544. return self.get_history(pg=pg)
  545. elif tid.isdigit():
  546. self.box_video_type = '影视'
  547. url = 'https://api.bilibili.com/pgc/season/index/result?order=2&season_status=-1&style_id=-1&sort=0&area=-1&pagesize=20&type=1&st={0}&season_type={0}&page={1}'.format(
  548. tid, pg)
  549. rsp = self.fetch(url, cookies=self.cookies)
  550. content = rsp.text
  551. jo = json.loads(content)
  552. videos = []
  553. vodList = jo['data']['list']
  554. for vod in vodList:
  555. aid = str(vod['season_id']).strip()
  556. title = vod['title'].strip()
  557. img = vod['cover'].strip()
  558. remark = vod['index_show'].strip()
  559. videos.append({
  560. "vod_id": aid+'&movie',
  561. "vod_name": title,
  562. "vod_pic": img,
  563. "vod_remarks": remark # 视频part数量
  564. })
  565. result['list'] = videos
  566. result['page'] = pg
  567. result['pagecount'] = 9999
  568. result['limit'] = 90
  569. result['total'] = 999999
  570. else:
  571. duration_diff='0'
  572. if 'duration' in extend:
  573. duration_diff = extend['duration']
  574. order = 'totalrank'
  575. if 'order' in extend:
  576. order = extend['order']
  577. self.box_video_type = '其他'
  578. url = 'https://api.bilibili.com/x/web-interface/search/type?search_type=video&keyword={0}&page={1}&duration={2}&order={3}'.format(
  579. tid, pg,duration_diff,order)
  580. rsp = self.fetch(url, cookies=self.cookies)
  581. content = rsp.text
  582. jo = json.loads(content)
  583. if jo.get('code') == 0:
  584. videos = []
  585. vodList = jo['data']['result']
  586. for vod in vodList:
  587. aid = str(vod['aid']).strip()
  588. title = vod['title'].replace("<em class=\"keyword\">", "").replace("</em>", "").replace("&quot;", '"')
  589. img = 'https:' + vod['pic'].strip()
  590. #remark = str(vod['duration']).strip()
  591. remark =str( self.second_to_time(self.str2sec(vod['duration']))).strip()
  592. videos.append({
  593. "vod_id": aid+'&other',
  594. "vod_name": title,
  595. "vod_pic": img,
  596. "vod_remarks": remark
  597. })
  598. #videos=self.filter_duration(videos, duration_diff)
  599. result['list'] = videos
  600. result['page'] = pg
  601. result['pagecount'] = 9999
  602. result['limit'] = 90
  603. result['total'] = 999999
  604. return result
  605. def cleanSpace(self, str):
  606. return str.replace('\n', '').replace('\t', '').replace('\r', '').replace(' ', '')
  607. def detailContent(self, array):
  608. # if int(array[0])< 1000000:
  609. result={}
  610. arrays = array[0].split("&")
  611. if arrays[-1] == 'movie':
  612. self.box_video_type='影视'
  613. aid = arrays[0]
  614. url = "http://api.bilibili.com/pgc/view/web/season?season_id={0}".format(aid)
  615. rsp = self.fetch(url, headers=self.header)
  616. jRoot = json.loads(rsp.text)
  617. if jRoot['code'] == 0:
  618. jo = jRoot['result']
  619. id = jo['season_id']
  620. title = jo['title']
  621. pic = jo['cover']
  622. areas = jo['areas'][0]['name']
  623. typeName = jo['share_sub_title']
  624. dec = jo['evaluate']
  625. remark = jo['new_ep']['desc']
  626. vod = {
  627. "vod_id": array[0],
  628. "vod_name": title,
  629. "vod_pic": pic,
  630. "type_name": typeName,
  631. "vod_year": "",
  632. # "vod_area":areas,
  633. "vod_area": self.vod_area, #弹幕是否显示的开关
  634. "vod_remarks": remark,
  635. "vod_actor": "",
  636. "vod_director": "",
  637. "vod_content": dec
  638. }
  639. ja = jo['episodes']
  640. playUrl = ''
  641. for tmpJo in ja:
  642. eid = tmpJo['id']
  643. cid = tmpJo['cid']
  644. part = tmpJo['title'].replace("#", "-")
  645. playUrl = playUrl + '{0}${1}_{2}#'.format(part, eid, cid)
  646. vod['vod_play_from'] = 'B站'
  647. vod['vod_play_url'] = playUrl
  648. result = {
  649. 'list': [
  650. vod
  651. ]
  652. }
  653. elif arrays[-1] == 'live':
  654. self.box_video_type='直播'
  655. aid = arrays[0]
  656. url = "https://api.live.bilibili.com/room/v1/Room/get_info?room_id=%s"%aid
  657. rsp = self.fetch(url, headers=self.header,cookies=self.cookies)
  658. jRoot = json.loads(rsp.text)
  659. if jRoot.get('code')==0:
  660. jo = jRoot['data']
  661. title = jo['title'].replace("<em class=\"keyword\">", "").replace("</em>", "")
  662. pic = jo.get("user_cover")
  663. desc = jo.get('description')
  664. dire = self.get_live_userInfo(jo["uid"])
  665. typeName = jo.get("area_name")
  666. remark = '在线人数:'+str(jo['online']).strip()
  667. vod = {
  668. "vod_id": aid+"&live",
  669. "vod_name": '(' + dire + ")" + title,
  670. "vod_pic": pic,
  671. "type_name": typeName,
  672. "vod_area": self.vod_area,
  673. #"vod_area":"",
  674. "vod_remarks": remark,
  675. "vod_actor": "直播间id-"+aid,
  676. "vod_director": dire,
  677. "vod_content": desc + '\n主播:' + dire,
  678. 'vod_play_from':'B站',
  679. 'vod_play_url':'flv线路原画$platform=web&quality=4_'+aid+'#flv线路高清$platform=web&quality=3_'+aid+'#h5线路原画$platform=h5&quality=4_'+aid+'#h5线路高清$platform=h5&quality=3_'+aid
  680. # 'vod_play_url':aid
  681. }
  682. result = {
  683. 'list': [
  684. vod
  685. ]
  686. }
  687. elif arrays[-1] == 'pu':
  688. self.box_video_type='pu'
  689. aidList=arrays[0].split('###')
  690. title = aidList[0]
  691. mid=aidList[1]
  692. pic = aidList[2]
  693. videoList=[]
  694. m=2
  695. if mid=='72270557':
  696. m=8
  697. for i in range(1, m):
  698. url = "https://api.bilibili.com/x/space/wbi/arc/search?mid={0}&ps=30&tid=0&pn={1}&keyword=&order=pubdate&platform=web&web_location=1550101&order_avoided=true&w_rid=1bab351d584ef97b4d0bb3024acce02d&wts=1684745027".format(mid,i)
  699. rsp = self.fetch(url,headers=self.header)
  700. htmlTxt= rsp.text
  701. jRoot = json.loads(htmlTxt)
  702. jo = jRoot['data']
  703. ja = jo['list']
  704. videos=ja['vlist']
  705. if len(videos)<1:
  706. break
  707. for tmpJo in videos:
  708. vodTitle = tmpJo['title']
  709. bvid = tmpJo['bvid']
  710. videoList.append(vodTitle+"$"+'bvid:'+bvid)
  711. typeName = aidList[3]
  712. remark = aidList[4].replace("&pu","")
  713. vod = {
  714. "vod_id": title+"###"+mid+"###"+pic+"###"+typeName+"###"+remark+"&pu",
  715. "vod_name": title,
  716. "vod_pic": pic,
  717. "type_name": typeName,
  718. "vod_year": '',
  719. "vod_area": "",
  720. "vod_remarks": '',
  721. "vod_actor": "",
  722. "vod_director": '',
  723. "vod_content": remark
  724. }
  725. playUrl="#"
  726. if len(videoList)>0:
  727. playUrl="#".join(videoList)
  728. vod['vod_play_from'] = 'B站视频'
  729. vod['vod_play_url'] = playUrl
  730. result = {
  731. 'list': [
  732. vod
  733. ]
  734. }
  735. else :
  736. self.box_video_type='其他'
  737. aid = arrays[0]
  738. url = "https://api.bilibili.com/x/web-interface/view?aid={0}".format(aid)
  739. rsp = self.fetch(url, headers=self.header)
  740. jRoot = json.loads(rsp.text)
  741. if jRoot['code'] == 0:
  742. jo = jRoot['data']
  743. title = jo['title'].replace("<em class=\"keyword\">", "").replace("</em>", "")
  744. pic = jo['pic']
  745. desc = jo['desc']
  746. timeStamp = jo['pubdate']
  747. timeArray = time.localtime(timeStamp)
  748. year = str(time.strftime("%Y", timeArray))
  749. dire = jo['owner']['name']
  750. typeName = jo['tname']
  751. remark = str(jo['duration']).strip()
  752. vod = {
  753. "vod_id": aid,
  754. "vod_name": '(' + dire + ")" + title,
  755. "vod_pic": pic,
  756. "type_name": typeName,
  757. "vod_year": year,
  758. "vod_area": self.vod_area,
  759. # "vod_area":"",
  760. "vod_remarks": remark,
  761. 'vod_tags':'mv',
  762. "vod_director": dire,
  763. "vod_content": desc + '\nup主:' + dire
  764. }
  765. ja = jo['pages']
  766. playUrl = ''
  767. for tmpJo in ja:
  768. cid = tmpJo['cid']
  769. part = tmpJo['part'].replace("#", "-")
  770. playUrl = playUrl + '{0}${1}_{2}#'.format(part, aid, cid)
  771. vod['vod_play_from'] = 'B站'
  772. vod['vod_play_url'] = playUrl
  773. result = {
  774. 'list': [
  775. vod
  776. ]
  777. }
  778. return result
  779. def searchContent(self, key, quick):
  780. videos=[]
  781. isPU=False
  782. if len(key)>3 and key.find('-PU')>1:
  783. key=key[0:len(key)-3]
  784. isPU=True
  785. videos = self.get_search(key=key)
  786. temporary=self.get_search_Movies(key=key)
  787. for vod in temporary:
  788. videos.append(vod)
  789. temporary=self.get_search_Fanju(key=key)
  790. for vod in temporary:
  791. videos.append(vod)
  792. #if isPU=True:
  793. temporary=self.get_search_PU(key=key)
  794. for vod in temporary:
  795. videos.append(vod)
  796. result = {
  797. 'list': videos
  798. }
  799. return result
  800. #番剧搜索
  801. def get_search_Fanju(self, key):
  802. self.box_video_type = '搜索'
  803. header = {
  804. "Referer": "https://www.bilibili.com",
  805. "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
  806. }
  807. url = 'https://api.bilibili.com/x/web-interface/search/type?keyword={0}&page=1&search_type=media_bangumi&order=totalrank&pagesize=20'.format(urllib.parse.quote(key))
  808. rsp = self.fetch(url, cookies=self.cookies, headers=header)
  809. content = rsp.text
  810. jo = json.loads(content)
  811. if jo['code'] != 0:
  812. return []
  813. jo = json.loads(content)
  814. videos = []
  815. vodList = jo['data']['result']
  816. for vod in vodList:
  817. aid = str(vod['season_id']).strip()
  818. title = vod['title'].replace("<em class=\"keyword\">", "").replace("</em>", "").replace("&quot;", '"')
  819. img = vod['cover'].strip()
  820. remark = str(vod['desc']).strip()
  821. videos.append({
  822. "vod_id": aid+'&movie',
  823. "vod_name": title,
  824. "vod_pic": img,
  825. "vod_remarks": remark
  826. })
  827. return videos
  828. #PU搜索
  829. def get_search_PU(self, key):
  830. self.box_video_type = '搜索'
  831. header = {
  832. "Referer": "https://www.bilibili.com",
  833. "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
  834. }
  835. url = 'https://api.bilibili.com/x/web-interface/search/type?keyword={0}&page=1&search_type=bili_user&order=totalrank&pagesize=20'.format(urllib.parse.quote(key))
  836. rsp = self.fetch(url, cookies=self.cookies, headers=header)
  837. content = rsp.text
  838. jo = json.loads(content)
  839. if jo['code'] != 0:
  840. return []
  841. jo = json.loads(content)
  842. videos = []
  843. vodList = jo['data']['result']
  844. for vod in vodList:
  845. aid = str(vod['mid']).strip()
  846. title = vod['uname'].replace("<em class=\"keyword\">", "").replace("</em>", "").replace("&quot;", '"')
  847. img = 'https://'+vod['upic'].strip()
  848. remark = str(vod['usign']).strip()
  849. videos.append({
  850. "vod_id": aid+'&pu',
  851. "vod_name": title,
  852. "vod_pic": img,
  853. "vod_remarks": remark
  854. })
  855. return videos
  856. #视频搜索
  857. def get_search(self, key):
  858. self.box_video_type = '搜索'
  859. header = {
  860. "Referer": "https://www.bilibili.com",
  861. "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
  862. }
  863. url = 'https://api.bilibili.com/x/web-interface/search/type?search_type=video&keyword={0}&page=1'.format(key)
  864. rsp = self.fetch(url, cookies=self.cookies, headers=header)
  865. content = rsp.text
  866. jo = json.loads(content)
  867. if jo['code'] != 0:
  868. rspRetry = self.fetch(url, cookies=self.cookies, headers=header)
  869. content = rspRetry.text
  870. jo = json.loads(content)
  871. videos = []
  872. vodList = jo['data']['result']
  873. for vod in vodList:
  874. aid = str(vod['aid']).strip()
  875. title = vod['title'].replace("<em class=\"keyword\">", "").replace("</em>", "").replace("&quot;", '"')
  876. img = 'https:' + vod['pic'].strip()
  877. remark = str(vod['duration']).strip()
  878. videos.append({
  879. "vod_id": aid+'&search',
  880. "vod_name": title,
  881. "vod_pic": img,
  882. "vod_remarks": remark
  883. })
  884. return videos
  885. #影视搜索
  886. def get_search_Movies(self, key):
  887. self.box_video_type = '搜索'
  888. header = {
  889. "Referer": "https://www.bilibili.com",
  890. "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
  891. }
  892. url = 'https://api.bilibili.com/x/web-interface/search/type?keyword={0}&page=1&search_type=media_ft&order=totalrank&pagesize=20'.format(urllib.parse.quote(key))
  893. rsp = self.fetch(url, cookies=self.cookies, headers=header)
  894. content = rsp.text
  895. jo = json.loads(content)
  896. if jo['code'] != 0:
  897. return []
  898. jo = json.loads(content)
  899. videos = []
  900. vodList = jo['data']['result']
  901. for vod in vodList:
  902. aid = str(vod['season_id']).strip()
  903. title = vod['title'].replace("<em class=\"keyword\">", "").replace("</em>", "").replace("&quot;", '"')
  904. img = vod['cover'].strip()
  905. remark = str(vod['desc']).strip()
  906. videos.append({
  907. "vod_id": aid+'&movie',
  908. "vod_name": title,
  909. "vod_pic": img,
  910. "vod_remarks": remark
  911. })
  912. return videos
  913. def playerContent(self, flag, id, vipFlags):
  914. result = {}
  915. avId=''
  916. try:
  917. if self.box_video_type == '影视':
  918. ids = id.split("_")
  919. avId="av"+ids[0]
  920. #return self.Get_vip(ep=ids[0])#直接调用vip解析,如果是vip账号请删除
  921. header = {
  922. "Referer": "https://www.bilibili.com",
  923. "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
  924. }
  925. url = 'https://api.bilibili.com/pgc/player/web/playurl?qn=116&ep_id={0}&cid={1}'.format(ids[0], ids[1])
  926. if len(self.cookies) <= 0:
  927. self.getCookie()
  928. rsp = self.fetch(url, cookies=self.cookies, headers=header)
  929. jRoot = json.loads(rsp.text)
  930. if jRoot['message'] != 'success':
  931. print("需要大会员权限才能观看")
  932. return result
  933. jo = jRoot['result']
  934. ja = jo['durl']
  935. maxSize = -1
  936. position = -1
  937. for i in range(len(ja)):
  938. tmpJo = ja[i]
  939. if maxSize < int(tmpJo['size']):
  940. maxSize = int(tmpJo['size'])
  941. position = i
  942. url = ''
  943. if len(ja) > 0:
  944. if position == -1:
  945. position = 0
  946. url = ja[position]['url']
  947. result["parse"] = 0
  948. result["playUrl"] = ''
  949. result["url"] = url
  950. result["header"] = {
  951. "Referer": "https://www.bilibili.com",
  952. "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
  953. }
  954. result["contentType"] = 'video/x-flv'
  955. elif self.box_video_type == '直播':
  956. ids = id.split("_")
  957. url = 'https://api.live.bilibili.com/room/v1/Room/playUrl?cid=%s&%s'%(ids[1],ids[0])
  958. #raise Exception(url)
  959. if len(self.cookies) <= 0:
  960. self.getCookie()
  961. rsp = self.fetch(url, cookies=self.cookies)
  962. jRoot = json.loads(rsp.text)
  963. if jRoot['code'] == 0:
  964. jo = jRoot['data']
  965. ja = jo['durl']
  966. url = ''
  967. if len(ja) > 0:
  968. url = ja[0]['url']
  969. result["parse"] = 0
  970. # result['type'] ="m3u8"
  971. result["playUrl"] = ''
  972. result["url"] = url
  973. result["header"] = {
  974. "Referer": "https://live.bilibili.com",
  975. "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
  976. }
  977. if "h5" in ids[0]:
  978. result["contentType"] = ''
  979. else:
  980. result["contentType"] = 'video/x-flv'
  981. elif self.box_video_type == 'pu':
  982. avId=mark=id.split(":")[1]
  983. result = self.get_Url_pu(idTxt=id)
  984. else:
  985. ids = id.split("_")
  986. avId="av"+ids[0]
  987. url = 'https://api.bilibili.com:443/x/player/playurl?avid={0}&cid={1}&qn=116'.format(ids[0], ids[1])
  988. if len(self.cookies) <= 0:
  989. self.getCookie()
  990. rsp = self.fetch(url, cookies=self.cookies)
  991. jRoot = json.loads(rsp.text)
  992. jo = jRoot['data']
  993. ja = jo['durl']
  994. maxSize = -1
  995. position = -1
  996. for i in range(len(ja)):
  997. tmpJo = ja[i]
  998. if maxSize < int(tmpJo['size']):
  999. maxSize = int(tmpJo['size'])
  1000. position = i
  1001. url = ''
  1002. if len(ja) > 0:
  1003. if position == -1:
  1004. position = 0
  1005. url = ja[position]['url']
  1006. result["parse"] = 0
  1007. result["playUrl"] = ''
  1008. result["url"] = url
  1009. result["header"] = {
  1010. "Referer": "https://www.bilibili.com",
  1011. "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
  1012. }
  1013. result["contentType"] = 'video/x-flv'
  1014. except Exception as e:
  1015. print(e)
  1016. result={}
  1017. if result=={} and self.box_video_type.find('直播')<0:
  1018. result= self.get_mp4(av=avId)
  1019. return result
  1020. def Get_vip(self,ep):
  1021. url='https://www.bilibili.com/bangumi/play/ep{0}'.format(ep)
  1022. result={}
  1023. result["parse"] = 1
  1024. result['jx'] = 1
  1025. result["playUrl"] = ''
  1026. result["url"] = url
  1027. result["header"] = ''
  1028. return result
  1029. def get_mp4(self,av):
  1030. result={}
  1031. url='https://m.bilibili.com/video/{0}'.format(av)
  1032. header1= {
  1033. "Referer": "https://www.bilibili.com",
  1034. "User-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3947.100 Mobile Safari/537.36"
  1035. }
  1036. rsp=self.fetch(url,headers=header1)
  1037. html=rsp.text
  1038. url=self.get_RegexGetText(Text=html,RegexText=r'"readyVideoUrl":"(.+?)","readyDuration":',Index=1)
  1039. result["parse"] = 0
  1040. result["playUrl"] = ''
  1041. result["url"] = url
  1042. result["header"] = header1
  1043. return result
  1044. def get_RegexGetText(self,Text,RegexText,Index):
  1045. returnTxt=""
  1046. Regex=re.search(RegexText, Text, re.M|re.S)
  1047. if Regex is not None:
  1048. returnTxt=Regex.group(Index)
  1049. return returnTxt
  1050. def get_Url_pu(self,idTxt):
  1051. result = {}
  1052. ids = idTxt.split(":")
  1053. header = {
  1054. "Referer": "https://www.bilibili.com",
  1055. "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36"
  1056. }
  1057. url = 'https://api.bilibili.com/x/web-interface/view?bvid={0}'.format(ids[1])
  1058. rsp =self.fetch(url, headers=header)
  1059. jRoot = json.loads(rsp.text)
  1060. jo = jRoot['data']
  1061. aid=jo['aid']
  1062. cid=jo['cid']
  1063. url = 'https://api.bilibili.com:443/x/player/playurl?avid={0}&cid={1}&qn=120&fnval=0&128=128&fourk=1'.format(aid,cid)
  1064. rsp = self.fetch(url, headers=header)
  1065. jRoot = json.loads(rsp.text)
  1066. jo = jRoot['data']
  1067. if jRoot['code']!=0:
  1068. return result
  1069. ja = jo['durl']
  1070. maxSize = -1
  1071. position = -1
  1072. for i in range(len(ja)):
  1073. tmpJo = ja[i]
  1074. if maxSize < int(tmpJo['size']):
  1075. maxSize = int(tmpJo['size'])
  1076. position = i
  1077. url = ''
  1078. if len(ja) > 0:
  1079. if position == -1:
  1080. position = 0
  1081. url = ja[position]['url']
  1082. result["parse"] = 0
  1083. result["playUrl"] = ''
  1084. result["url"] = url
  1085. result["contentType"] = 'video/x-flv'
  1086. result["header"] = {
  1087. "Referer": "https://www.bilibili.com",
  1088. "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36"
  1089. }
  1090. return result
  1091. config = {
  1092. "player": {},
  1093. "filter": {
  1094. "舞蹈": [{
  1095. "key": "order",
  1096. "name": "排序",
  1097. "value": [
  1098. {
  1099. "n": "综合排序",
  1100. "v": "totalrank"
  1101. },
  1102. {
  1103. "n": "最新发布",
  1104. "v": "pubdate"
  1105. },
  1106. {
  1107. "n": "最多点击",
  1108. "v": "click"
  1109. },
  1110. {
  1111. "n": "最多收藏",
  1112. "v": "stow"
  1113. },
  1114. {
  1115. "n": "最多弹幕",
  1116. "v": "dm"
  1117. },
  1118. ]
  1119. },
  1120. {
  1121. "key": "duration",
  1122. "name": "时长",
  1123. "value": [{
  1124. "n": "全部",
  1125. "v": "0"
  1126. },
  1127. {
  1128. "n": "60分钟以上",
  1129. "v": "4"
  1130. },
  1131. {
  1132. "n": "30~60分钟",
  1133. "v": "3"
  1134. },
  1135. {
  1136. "n": "5~30分钟",
  1137. "v": "2"
  1138. },
  1139. {
  1140. "n": "5分钟以下",
  1141. "v": "1"
  1142. }
  1143. ]
  1144. }],
  1145. "少女": [{
  1146. "key": "order",
  1147. "name": "排序",
  1148. "value": [
  1149. {
  1150. "n": "综合排序",
  1151. "v": "totalrank"
  1152. },
  1153. {
  1154. "n": "最新发布",
  1155. "v": "pubdate"
  1156. },
  1157. {
  1158. "n": "最多点击",
  1159. "v": "click"
  1160. },
  1161. {
  1162. "n": "最多收藏",
  1163. "v": "stow"
  1164. },
  1165. {
  1166. "n": "最多弹幕",
  1167. "v": "dm"
  1168. },
  1169. ]
  1170. },
  1171. {
  1172. "key": "duration",
  1173. "name": "时长",
  1174. "value": [{
  1175. "n": "全部",
  1176. "v": "0"
  1177. },
  1178. {
  1179. "n": "60分钟以上",
  1180. "v": "4"
  1181. },
  1182. {
  1183. "n": "30~60分钟",
  1184. "v": "3"
  1185. },
  1186. {
  1187. "n": "5~30分钟",
  1188. "v": "2"
  1189. },
  1190. {
  1191. "n": "5分钟以下",
  1192. "v": "1"
  1193. }
  1194. ]
  1195. }],
  1196. "mmd": [{
  1197. "key": "order",
  1198. "name": "排序",
  1199. "value": [
  1200. {
  1201. "n": "综合排序",
  1202. "v": "totalrank"
  1203. },
  1204. {
  1205. "n": "最新发布",
  1206. "v": "pubdate"
  1207. },
  1208. {
  1209. "n": "最多点击",
  1210. "v": "click"
  1211. },
  1212. {
  1213. "n": "最多收藏",
  1214. "v": "stow"
  1215. },
  1216. {
  1217. "n": "最多弹幕",
  1218. "v": "dm"
  1219. },
  1220. ]
  1221. },
  1222. {
  1223. "key": "duration",
  1224. "name": "时长",
  1225. "value": [{
  1226. "n": "全部",
  1227. "v": "0"
  1228. },
  1229. {
  1230. "n": "60分钟以上",
  1231. "v": "4"
  1232. },
  1233. {
  1234. "n": "30~60分钟",
  1235. "v": "3"
  1236. },
  1237. {
  1238. "n": "5~30分钟",
  1239. "v": "2"
  1240. },
  1241. {
  1242. "n": "5分钟以下",
  1243. "v": "1"
  1244. }
  1245. ]
  1246. }],
  1247. "直播": [{
  1248. "key": "parent_area_id",
  1249. "name": "直播分区",
  1250. "value": [
  1251. {
  1252. "n": "娱乐",
  1253. "v": "1"
  1254. },
  1255. {
  1256. "n": "电台",
  1257. "v": "5"
  1258. },
  1259. {
  1260. "n": "网游",
  1261. "v": "2"
  1262. },
  1263. {
  1264. "n": "手游",
  1265. "v": "3"
  1266. },
  1267. {
  1268. "n": "单机游戏",
  1269. "v": "6"
  1270. },
  1271. {
  1272. "n": "虚拟主播",
  1273. "v": "9"
  1274. },{'n': '生活', 'v': 10},
  1275. {'n': '知识', 'v': 11},
  1276. {'n': '赛事', 'v': 13}
  1277. ]
  1278. },
  1279. ],
  1280. "生活直播":[{"key":"area_id","name":"小分区","value":[{"n":"生活分享","v":"646"},{"n":"运动","v":"628"},{"n":"搞笑","v":"624"},{"n":"手工绘画","v":"627"},{"n":"萌宠","v":"369"},{"n":"美食","v":"367"},{"n":"时尚","v":"378"},{"n":"影音馆","v":"33"}] }],
  1281. "音声": [{
  1282. "key": "order",
  1283. "name": "排序",
  1284. "value": [
  1285. {
  1286. "n": "综合排序",
  1287. "v": "totalrank"
  1288. },
  1289. {
  1290. "n": "最新发布",
  1291. "v": "pubdate"
  1292. },
  1293. {
  1294. "n": "最多点击",
  1295. "v": "click"
  1296. },
  1297. {
  1298. "n": "最多收藏",
  1299. "v": "stow"
  1300. },
  1301. {
  1302. "n": "最多弹幕",
  1303. "v": "dm"
  1304. },
  1305. ]
  1306. },
  1307. {
  1308. "key": "duration",
  1309. "name": "时长",
  1310. "value": [{
  1311. "n": "全部",
  1312. "v": "0"
  1313. },
  1314. {
  1315. "n": "60分钟以上",
  1316. "v": "4"
  1317. },
  1318. {
  1319. "n": "30~60分钟",
  1320. "v": "3"
  1321. },
  1322. {
  1323. "n": "5~30分钟",
  1324. "v": "2"
  1325. },
  1326. {
  1327. "n": "5分钟以下",
  1328. "v": "1"
  1329. }
  1330. ]
  1331. }],
  1332. "收藏夹": [{
  1333. "key": "order",
  1334. "name": "排序",
  1335. "value": [
  1336. {
  1337. "n": "收藏时间",
  1338. "v": "mtime"
  1339. },
  1340. {
  1341. "n": "播放量",
  1342. "v": "view"
  1343. },
  1344. {
  1345. "n": "投稿时间",
  1346. "v": "pubtime"
  1347. }
  1348. ]
  1349. },
  1350. {
  1351. "key": "mlid",
  1352. "name": "收藏夹分区",
  1353. "value": [
  1354. ]
  1355. }],
  1356. "cosplay": [{
  1357. "key": "order",
  1358. "name": "排序",
  1359. "value": [
  1360. {
  1361. "n": "综合排序",
  1362. "v": "totalrank"
  1363. },
  1364. {
  1365. "n": "最新发布",
  1366. "v": "pubdate"
  1367. },
  1368. {
  1369. "n": "最多点击",
  1370. "v": "click"
  1371. },
  1372. {
  1373. "n": "最多收藏",
  1374. "v": "stow"
  1375. },
  1376. {
  1377. "n": "最多弹幕",
  1378. "v": "dm"
  1379. },
  1380. ]
  1381. },
  1382. {
  1383. "key": "duration",
  1384. "name": "时长",
  1385. "value": [{
  1386. "n": "全部",
  1387. "v": "0"
  1388. },
  1389. {
  1390. "n": "60分钟以上",
  1391. "v": "4"
  1392. },
  1393. {
  1394. "n": "30~60分钟",
  1395. "v": "3"
  1396. },
  1397. {
  1398. "n": "5~30分钟",
  1399. "v": "2"
  1400. },
  1401. {
  1402. "n": "5分钟以下",
  1403. "v": "1"
  1404. }
  1405. ]
  1406. }],
  1407. "频道": [{
  1408. "key": "order",
  1409. "name": "排序",
  1410. "value": [
  1411. {
  1412. "n": "综合排序",
  1413. "v": "totalrank"
  1414. },
  1415. {
  1416. "n": "最新发布",
  1417. "v": "pubdate"
  1418. },
  1419. {
  1420. "n": "最多点击",
  1421. "v": "click"
  1422. },
  1423. {
  1424. "n": "最多收藏",
  1425. "v": "stow"
  1426. },
  1427. {
  1428. "n": "最多弹幕",
  1429. "v": "dm"
  1430. },
  1431. ]
  1432. },
  1433. {
  1434. "key": "duration",
  1435. "name": "时长",
  1436. "value": [{
  1437. "n": "全部",
  1438. "v": "0"
  1439. },
  1440. {
  1441. "n": "60分钟以上",
  1442. "v": "4"
  1443. },
  1444. {
  1445. "n": "30~60分钟",
  1446. "v": "3"
  1447. },
  1448. {
  1449. "n": "5~30分钟",
  1450. "v": "2"
  1451. },
  1452. {
  1453. "n": "5分钟以下",
  1454. "v": "1"
  1455. }
  1456. ]
  1457. }, {"key": "cid", "name": "分类",
  1458. "value":[{'n': '搞笑', 'v': '搞笑'}, {'n': '美食', 'v': '美食'}, {'n': '鬼畜', 'v': '鬼畜'}, {'n': '美妆', 'v': '美妆'}, {'n': 'mmd', 'v': 'mmd'}, {'n': '科普', 'v': '科普'}, {'n': 'COSPLAY', 'v': 'COSPLAY'}, {'n': '漫展', 'v': '漫展'}, {'n': 'MAD', 'v': 'MAD'}, {'n': '手书', 'v': '手书'}, {'n': '穿搭', 'v': '穿搭'}, {'n': '发型', 'v': '发型'}, {'n': '化妆教程', 'v': '化妆教程'}, {'n': '电音', 'v': '电音'}, {'n': '欧美音乐', 'v': '欧美音乐'}, {'n': '中文翻唱', 'v': '中文翻唱'}, {'n': '洛天依', 'v': '洛天依'}, {'n': '翻唱', 'v': '翻唱'}, {'n': '日文翻唱', 'v': '日文翻唱'}, {'n': '科普', 'v': '科普'}, {'n': '技术宅', 'v': '技术宅'}, {'n': '历史', 'v': '历史'}, {'n': '科学', 'v': '科学'}, {'n': '人文', 'v': '人文'}, {'n': '科幻', 'v': '科幻'}, {'n': '手机', 'v': '手机'}, {'n': '手机评测', 'v': '手机评测'}, {'n': '电脑', 'v': '电脑'}, {'n': '摄影', 'v': '摄影'}, {'n': '笔记本', 'v': '笔记本'}, {'n': '装机', 'v': '装机'}, {'n': '课堂教育', 'v': '课堂教育'}, {'n': '公开课', 'v': '公开课'}, {'n': '演讲', 'v': '演讲'}, {'n': 'PS教程', 'v': 'PS教程'}, {'n': '编程', 'v': '编程'}, {'n': '英语学习', 'v': '英语学习'}, {'n': '喵星人', 'v': '喵星人'}, {'n': '萌宠', 'v': '萌宠'}, {'n': '汪星人', 'v': '汪星人'}, {'n': '大熊猫', 'v': '大熊猫'}, {'n': '柴犬', 'v': '柴犬'},{'n': '田园犬', 'v': '田园犬'}, {'n': '吱星人', 'v': '吱星人'}, {'n': '美食', 'v': '美食'}, {'n': '甜点', 'v': '甜点'}, {'n': '吃货', 'v': '吃货'}, {'n': '厨艺', 'v': '厨艺'}, {'n': '烘焙', 'v': '烘焙'}, {'n': '街头美食', 'v': '街头美食'}, {'n': 'A.I.Channel', 'v': 'A.I.Channel'}, {'n': '虚拟UP主', 'v': '虚拟UP主'}, {'n': '神楽めあ', 'v': '神楽めあ'}, {'n': '白上吹雪', 'v': '白上吹雪'}, {'n': '婺源', 'v': '婺源'}, {'n': 'hololive', 'v': 'hololive'}, {'n': 'EXO', 'v': 'EXO'}, {'n': '防弹少年团', 'v': '防弹少年团'}, {'n': '肖战', 'v': '肖战'}, {'n': '王一博', 'v': '王一博'}, {'n': '易烊千玺', 'v': '易烊千玺'}, {'n': '赵今麦', 'v': '赵今麦'}, {'n': '宅舞', 'v': '宅舞'}, {'n': '街舞', 'v': '街舞'}, {'n': '舞蹈教学', 'v': '舞蹈教学'}, {'n': '明星舞蹈', 'v': '明星舞蹈'}, {'n': '韩舞', 'v': '韩舞'}, {'n': '古典舞', 'v': '古典舞'}, {'n': '旅游', 'v': '旅游'}, {'n': '绘画', 'v': '绘画'}, {'n': '手工', 'v': '手工'}, {'n': 'vlog', 'v': 'vlog'}, {'n': 'DIY', 'v': 'DIY'}, {'n': '手绘', 'v': '手绘'}, {'n': '综艺', 'v': '综艺'}, {'n': '国家宝藏', 'v': '国家宝藏'}, {'n': '脱口秀', 'v': '脱口秀'}, {'n': '日本综艺', 'v': '日本综艺'}, {'n': '国内综艺', 'v': '国内综艺'}, {'n': '人类观察', 'v': '人类观察'}, {'n': '影评', 'v': '影评'}, {'n': '电影解说', 'v': '电影解说'}, {'n': '影视混剪', 'v': '影视混剪'}, {'n': '影视剪辑', 'v': '影视剪辑'}, {'n': '漫威', 'v': '漫威'}, {'n': '超级英雄', 'v': '超级英雄'}, {'n': '影视混剪', 'v': '影视混剪'}, {'n': '影视剪辑', 'v': '影视剪辑'},
  1459. {'n': '诸葛亮', 'v': '诸葛亮'}, {'n': '韩剧', 'v': '韩剧'}, {'n': '王司徒', 'v': '王司徒'}, {'n': '泰剧', 'v': '泰剧'},
  1460. {'n': '郭德纲', 'v': '郭德纲'}, {'n': '相声', 'v': '相声'}, {'n': '张云雷', 'v': '张云雷'}, {'n': '秦霄贤', 'v': '秦霄贤'}, {'n': '孟鹤堂', 'v': '孟鹤堂'}, {'n': '岳云鹏', 'v': '岳云鹏'}, {'n': '假面骑士', 'v': '假面骑士'}, {'n': '特摄', 'v': '特摄'}, {'n': '奥特曼', 'v': '奥特曼'}, {'n': '迪迦奥特曼', 'v': '迪迦奥特曼'}, {'n': '超级战队', 'v': '超级战队'}, {'n': '铠甲勇士', 'v': '铠甲勇士'}, {'n': '健身', 'v': '健身'}, {'n': '篮球', 'v': '篮球'}, {'n': '体育', 'v': '体育'}, {'n': '帕梅拉', 'v': '帕梅拉'}, {'n': '极限运动', 'v': '极限运动'}, {'n': '足球', 'v': '足球'}, {'n': '星海', 'v': '星海'}, {'n': '张召忠', 'v': '张召忠'}, {'n': '航母', 'v': '航母'}, {'n': '航天', 'v': '航天'}, {'n': '导弹', 'v': '导弹'}, {'n': '战斗机', 'v': '战斗机'}]
  1461. }
  1462. ],
  1463. }
  1464. }
  1465. header = {
  1466. "Referer": "https://www.bilibili.com",
  1467. "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
  1468. }
  1469. def localProxy(self, param):
  1470. return [200, "video/MP2T", action, ""]
  1471. if __name__ == '__main__':
  1472. a=Spider()
  1473. print(time_stamp)
  1474. print(a.categoryContent('5','1',filter={},extend='1'))
  1475. #print(a.get_live(pg=1,parent_area_id='0'))
  1476. a.box_video_type='直播'
  1477. print(a.get_hot(pg=1))
  1478. print(a.detailContent(['43000&movie']))
  1479. print(a.get_toview(1))
  1480. #print(a.get_fav(pg='1',order='mtime',extend={}))