py_bilimd.py 5.2 KB

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