py_bilimd.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. result = {
  43. 'list':[]
  44. }
  45. return result
  46. cookies = ''
  47. def getCookie(self):
  48. #在cookies_str中填入会员或大会员cookie,以获得更好的体验
  49. cookies_str = "innersign=0; buvid3=606BE156-AE37-AEA8-7052-9DA0B21766E776404infoc; b_nut=1663302976; i-wanna-go-back=-1; b_ut=7; b_lsid=4106252F6_18344933A90; _uuid=586AAEB7-6B88-A691-F7AC-95C27E57F53C43036infoc; buvid4=B6FF1449-4361-1C76-DEFC-4AFCA1777B7E78304-022091612-PdJr0jKE6N5TamfAEX9uACD1RXvklspbNdlcIQEFLMu0d9wS3G3sdA%3D%3D; buvid_fp=2a9b54d5e06aa54293dc7544e000552d"
  50. cookies_dic = dict([co.strip().split('=') for co in cookies_str.split(';')])
  51. rsp = session()
  52. cookies_jar = utils.cookiejar_from_dict(cookies_dic)
  53. rsp.cookies = cookies_jar
  54. content = self.fetch("http://api.bilibili.com/x/web-interface/nav", cookies=rsp.cookies)
  55. res = json.loads(content.text)
  56. if res["code"] == 0:
  57. self.cookies = rsp.cookies
  58. else:
  59. rsp = self.fetch("https://www.bilibili.com/")
  60. self.cookies = rsp.cookies
  61. return rsp.cookies
  62. def categoryContent(self,tid,pg,filter,extend):
  63. result = {}
  64. 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)
  65. if len(self.cookies) <= 0:
  66. self.getCookie()
  67. rsp = self.fetch(url, cookies=self.cookies)
  68. content = rsp.text
  69. jo = json.loads(content)
  70. videos = []
  71. vodList = jo['data']['list']
  72. for vod in vodList:
  73. aid = str(vod['season_id']).strip()
  74. title = vod['title'].strip()
  75. img = vod['cover'].strip()
  76. remark = vod['index_show'].strip()
  77. videos.append({
  78. "vod_id":aid,
  79. "vod_name":title,
  80. "vod_pic":img,
  81. "vod_remarks":remark
  82. })
  83. result['list'] = videos
  84. result['page'] = pg
  85. result['pagecount'] = 9999
  86. result['limit'] = 90
  87. result['total'] = 999999
  88. return result
  89. def cleanSpace(self,str):
  90. return str.replace('\n','').replace('\t','').replace('\r','').replace(' ','')
  91. def detailContent(self,array):
  92. aid = array[0]
  93. url = "http://api.bilibili.com/pgc/view/web/season?season_id={0}".format(aid)
  94. rsp = self.fetch(url,headers=self.header)
  95. jRoot = json.loads(rsp.text)
  96. jo = jRoot['result']
  97. id = jo['season_id']
  98. title = jo['title']
  99. pic = jo['cover']
  100. areas = jo['areas'][0]['name']
  101. typeName = jo['share_sub_title']
  102. dec = jo['evaluate']
  103. remark = jo['new_ep']['desc']
  104. vod = {
  105. "vod_id":id,
  106. "vod_name":title,
  107. "vod_pic":pic,
  108. "type_name":typeName,
  109. "vod_year":"",
  110. "vod_area":areas,
  111. "vod_remarks":remark,
  112. "vod_actor":"",
  113. "vod_director":"",
  114. "vod_content":dec
  115. }
  116. ja = jo['episodes']
  117. playUrl = ''
  118. for tmpJo in ja:
  119. eid = tmpJo['id']
  120. cid = tmpJo['cid']
  121. part = tmpJo['title'].replace("#", "-")
  122. playUrl = playUrl + '{0}${1}_{2}#'.format(part, eid, cid)
  123. vod['vod_play_from'] = 'B站影视'
  124. vod['vod_play_url'] = playUrl
  125. result = {
  126. 'list':[
  127. vod
  128. ]
  129. }
  130. return result
  131. def searchContent(self,key,quick):
  132. url = 'https://api.bilibili.com/x/web-interface/search/type?search_type=media_bangumi&keyword={0}'.format(key) # 番剧搜索
  133. if len(self.cookies) <= 0:
  134. self.getCookie()
  135. rsp = self.fetch(url, cookies=self.cookies)
  136. content = rsp.text
  137. jo = json.loads(content)
  138. rs = jo['data']
  139. if rs['numResults'] == 0:
  140. url = 'https://api.bilibili.com/x/web-interface/search/type?search_type=media_ft&keyword={0}'.format(key) # 影视搜索
  141. rspRetry = self.fetch(url, cookies=self.cookies)
  142. content = rspRetry.text
  143. jo = json.loads(content)
  144. videos = []
  145. vodList = jo['data']['result']
  146. for vod in vodList:
  147. aid = str(vod['season_id']).strip()
  148. title = vod['title'].strip().replace("<em class=\"keyword\">", "").replace("</em>", "")
  149. img = vod['eps'][0]['cover'].strip()
  150. remark = vod['index_show']
  151. videos.append({
  152. "vod_id": aid,
  153. "vod_name": title,
  154. "vod_pic": img,
  155. "vod_remarks": remark
  156. })
  157. result = {
  158. 'list': videos
  159. }
  160. return result
  161. def playerContent(self,flag,id,vipFlags):
  162. result = {}
  163. ids = id.split("_")
  164. header = {
  165. "Referer": "https://www.bilibili.com",
  166. "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"
  167. }
  168. url = 'https://api.bilibili.com/pgc/player/web/playurl?qn=116&ep_id={0}&cid={1}'.format(ids[0],ids[1])
  169. if len(self.cookies) <= 0:
  170. self.getCookie()
  171. rsp = self.fetch(url,cookies=self.cookies,headers=header)
  172. jRoot = json.loads(rsp.text)
  173. if jRoot['message'] != 'success':
  174. print("需要大会员权限才能观看")
  175. return {}
  176. jo = jRoot['result']
  177. ja = jo['durl']
  178. maxSize = -1
  179. position = -1
  180. for i in range(len(ja)):
  181. tmpJo = ja[i]
  182. if maxSize < int(tmpJo['size']):
  183. maxSize = int(tmpJo['size'])
  184. position = i
  185. url = ''
  186. if len(ja) > 0:
  187. if position == -1:
  188. position = 0
  189. url = ja[position]['url']
  190. result["parse"] = 0
  191. result["playUrl"] = ''
  192. result["url"] = url
  193. result["header"] = {
  194. "Referer":"https://www.bilibili.com",
  195. "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"
  196. }
  197. result["contentType"] = 'video/x-flv'
  198. return result
  199. config = {
  200. "player": {},
  201. "filter": {}
  202. }
  203. header = {}
  204. def localProxy(self,param):
  205. return [200, "video/MP2T", action, ""]