py_bilimd.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. from requests import session, utils
  8. import os
  9. import time
  10. import base64
  11. class Spider(Spider): # 元类 默认的元类 type
  12. def getName(self):
  13. return "B站影视"
  14. def init(self,extend=""):
  15. print("============{0}============".format(extend))
  16. pass
  17. def isVideoFormat(self,url):
  18. pass
  19. def manualVideoCheck(self):
  20. pass
  21. def homeContent(self,filter):
  22. result = {}
  23. cateManual = {
  24. "番剧": "1",
  25. "国创": "4",
  26. "电影": "2",
  27. "综艺": "7",
  28. "电视剧": "5",
  29. "纪录片": "3"
  30. }
  31. classes = []
  32. for k in cateManual:
  33. classes.append({
  34. 'type_name':k,
  35. 'type_id':cateManual[k]
  36. })
  37. result['class'] = classes
  38. if(filter):
  39. result['filters'] = self.config['filter']
  40. return result
  41. def homeVideoContent(self):
  42. tid = self.homeContent(False)['class'][0]['type_id']
  43. return self.categoryContent(tid, 1, False, {})
  44. cookies = ''
  45. def getCookie(self):
  46. try:
  47. cookies_str = self.fetch("buvid3=12A78977-8E04-8DA0-EACA-4280A2CBF70F26536infoc; buvid4=AC82CF17-BB02-2BA5-AEB2-880F381C8E3A15597-022020401-bEJsC6owQKOC964b3kF8aA%3D%3D; CURRENT_BLACKGAP=0; i-wanna-go-back=-1; is-2022-channel=1; b_nut=100; _uuid=C3D57961-F8CA-8BB3-EA87-1B545E435B81013328infoc; header_theme_version=CLOSE; nostalgia_conf=-1; rpdid=|(Y|)|Yukuu0J'uY~~|~|RmY; DedeUserID=3493112737433767; DedeUserID__ckMd5=b48704cc9d5767a2; CURRENT_QUALITY=80; b_ut=5; home_feed_column=5; CURRENT_FNVAL=4048; fingerprint=6072d6d047af63027d42e7b07fdc0e14; buvid_fp_plain=undefined; buvid_fp=6072d6d047af63027d42e7b07fdc0e14; bp_video_offset_3493112737433767=829654181129224199; PVID=1; innersign=0; b_lsid=7197A2E1_18A6572474D; bsource=search_baidu; SESSDATA=a3b66263%2C1709471105%2C827d3%2A92J3ek47bxV2D3NICqYMcLiGLc8j5GPYyNJ5Kany321n1pWi76HJvQF51AUZv0Xpjf5K2lxwAAQwA; bili_jct=8fefab3e2a608c421e3d5fa72de5a823; sid=77j01eei; browser_resolution=1920-398").text
  48. cookies_dic = dict([co.strip().split('=') for co in cookies_str.split(';')])
  49. rsp = session()
  50. cookies_jar = utils.cookiejar_from_dict(cookies_dic)
  51. rsp.cookies = cookies_jar
  52. content = self.fetch("http://api.bilibili.com/x/web-interface/nav", cookies=rsp.cookies)
  53. res = json.loads(content.text)
  54. except:
  55. res = {}
  56. res["code"] = 404
  57. if res["code"] == 0:
  58. self.cookies = rsp.cookies
  59. else:
  60. rsp = self.fetch("https://www.bilibili.com/")
  61. self.cookies = rsp.cookies
  62. return rsp.cookies
  63. def categoryContent(self,tid,pg,filter,extend):
  64. result = {}
  65. 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(tid,pg)
  66. if len(self.cookies) <= 0:
  67. self.getCookie()
  68. rsp = self.fetch(url, cookies=self.cookies)
  69. content = rsp.text
  70. jo = json.loads(content)
  71. videos = []
  72. vodList = jo['data']['list']
  73. for vod in vodList:
  74. aid = str(vod['season_id']).strip()
  75. title = vod['title'].strip()
  76. img = vod['cover'].strip()
  77. remark = vod['index_show'].strip()
  78. videos.append({
  79. "vod_id":aid,
  80. "vod_name":title,
  81. "vod_pic":img,
  82. "vod_remarks":remark
  83. })
  84. result['list'] = videos
  85. result['page'] = pg
  86. result['pagecount'] = 9999
  87. result['limit'] = 90
  88. result['total'] = 999999
  89. return result
  90. def cleanSpace(self,str):
  91. return str.replace('\n','').replace('\t','').replace('\r','').replace(' ','')
  92. def detailContent(self,array):
  93. aid = array[0]
  94. url = "http://api.bilibili.com/pgc/view/web/season?season_id={0}".format(aid)
  95. rsp = self.fetch(url,headers=self.header)
  96. jRoot = json.loads(rsp.text)
  97. jo = jRoot['result']
  98. id = jo['season_id']
  99. title = jo['title']
  100. pic = jo['cover']
  101. areas = jo['areas'][0]['name']
  102. typeName = jo['share_sub_title']
  103. dec = jo['evaluate']
  104. remark = jo['new_ep']['desc']
  105. vod = {
  106. "vod_id":id,
  107. "vod_name":title,
  108. "vod_pic":pic,
  109. "type_name":typeName,
  110. "vod_year":"",
  111. "vod_area":areas,
  112. "vod_remarks":remark,
  113. "vod_actor":"",
  114. "vod_director":"",
  115. "vod_content":dec
  116. }
  117. ja = jo['episodes']
  118. playUrl = ''
  119. for tmpJo in ja:
  120. eid = tmpJo['id']
  121. cid = tmpJo['cid']
  122. part = tmpJo['title'].replace("#", "-")
  123. playUrl = playUrl + '{0}${1}_{2}#'.format(part, eid, cid)
  124. vod['vod_play_from'] = 'B站影视'
  125. vod['vod_play_url'] = playUrl
  126. result = {
  127. 'list':[
  128. vod
  129. ]
  130. }
  131. return result
  132. def searchContent(self,key,quick):
  133. url = 'https://api.bilibili.com/x/web-interface/search/type?search_type=media_bangumi&keyword={0}'.format(key) # 番剧搜索
  134. if len(self.cookies) <= 0:
  135. self.getCookie()
  136. rsp = self.fetch(url, cookies=self.cookies)
  137. content = rsp.text
  138. jo = json.loads(content)
  139. rs = jo['data']
  140. if rs['numResults'] == 0:
  141. url = 'https://api.bilibili.com/x/web-interface/search/type?search_type=media_ft&keyword={0}'.format(key) # 影视搜索
  142. rspRetry = self.fetch(url, cookies=self.cookies)
  143. content = rspRetry.text
  144. jo = json.loads(content)
  145. videos = []
  146. vodList = jo['data']['result']
  147. for vod in vodList:
  148. aid = str(vod['season_id']).strip()
  149. title = vod['title'].strip().replace("<em class=\"keyword\">", "").replace("</em>", "")
  150. img = vod['eps'][0]['cover'].strip()
  151. remark = vod['index_show']
  152. videos.append({
  153. "vod_id": aid,
  154. "vod_name": title,
  155. "vod_pic": img,
  156. "vod_remarks": remark
  157. })
  158. result = {
  159. 'list': videos
  160. }
  161. return result
  162. def playerContent(self,flag,id,vipFlags):
  163. result = {}
  164. ids = id.split("_")
  165. header = {
  166. "Referer": "https://www.bilibili.com",
  167. "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"
  168. }
  169. url = 'https://api.bilibili.com/pgc/player/web/playurl?qn=116&ep_id={0}&cid={1}'.format(ids[0],ids[1])
  170. if len(self.cookies) <= 0:
  171. self.getCookie()
  172. rsp = self.fetch(url,cookies=self.cookies,headers=header)
  173. jRoot = json.loads(rsp.text)
  174. if jRoot['message'] != 'success':
  175. url = ''
  176. else:
  177. jo = jRoot['result']
  178. ja = jo['durl']
  179. maxSize = -1
  180. position = -1
  181. for i in range(len(ja)):
  182. tmpJo = ja[i]
  183. if maxSize < int(tmpJo['size']):
  184. maxSize = int(tmpJo['size'])
  185. position = i
  186. url = ''
  187. if len(ja) > 0:
  188. if position == -1:
  189. position = 0
  190. url = ja[position]['url']
  191. result["parse"] = 0
  192. result["playUrl"] = ''
  193. result["url"] = url
  194. result["header"] = {
  195. "Referer":"https://www.bilibili.com",
  196. "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"
  197. }
  198. result["contentType"] = 'video/x-flv'
  199. return result
  200. config = {
  201. "player": {},
  202. "filter": {}
  203. }
  204. header = {}
  205. def localProxy(self,param):
  206. return [200, "video/MP2T", action, ""]