py_bilibili_cz.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 time
  8. import base64
  9. class Spider(Spider): # 元类 默认的元类 type
  10. def getName(self):
  11. return "哔哩"
  12. def init(self,extend=""):
  13. print("============{0}============".format(extend))
  14. pass
  15. def isVideoFormat(self,url):
  16. pass
  17. def manualVideoCheck(self):
  18. pass
  19. def homeContent(self,filter):
  20. result = {}
  21. cateManual = {
  22. "7年级语文": "7年级语文",
  23. "7年级数学": "7年级数学",
  24. "7年级英语": "7年级英语",
  25. "7年级物理": "7年级物理",
  26. "7年级化学": "7年级化学",
  27. "7年级生物": "7年级生物",
  28. "7年级政治": "7年级政治",
  29. "7年级历史": "7年级历史",
  30. "7年级地理": "7年级地理",
  31. "8年级语文": "8年级语文",
  32. "8年级数学": "8年级数学",
  33. "8年级英语": "8年级英语",
  34. "8年级物理": "8年级物理",
  35. "8年级化学": "8年级化学",
  36. "8年级生物": "8年级生物",
  37. "8年级政治": "8年级政治",
  38. "8年级历史": "8年级历史",
  39. "8年级地理": "8年级地理",
  40. "9年级语文": "9年级语文",
  41. "9年级数学": "9年级数学",
  42. "9年级英语": "9年级英语",
  43. "9年级物理": "9年级物理",
  44. "9年级化学": "9年级化学",
  45. "9年级生物": "9年级生物",
  46. "9年级政治": "9年级政治",
  47. "9年级历史": "9年级历史",
  48. "9年级地理": "9年级地理"
  49. }
  50. classes = []
  51. for k in cateManual:
  52. classes.append({
  53. 'type_name':k,
  54. 'type_id':cateManual[k]
  55. })
  56. result['class'] = classes
  57. if(filter):
  58. result['filters'] = self.config['filter']
  59. return result
  60. def homeVideoContent(self):
  61. result = {
  62. 'list':[]
  63. }
  64. return result
  65. cookies = ''
  66. def getCookie(self):
  67. rsp = self.fetch("https://www.bilibili.com/")
  68. self.cookies = rsp.cookies
  69. return rsp.cookies
  70. def categoryContent(self,tid,pg,filter,extend):
  71. result = {}
  72. url = 'https://api.bilibili.com/x/web-interface/search/type?search_type=video&keyword={0}&duration=4&page={1}'.format(tid,pg)
  73. if len(self.cookies) <= 0:
  74. self.getCookie()
  75. rsp = self.fetch(url,cookies=self.cookies)
  76. content = rsp.text
  77. jo = json.loads(content)
  78. if jo['code'] != 0:
  79. rspRetry = self.fetch(url,cookies=self.getCookie())
  80. content = rspRetry.text
  81. jo = json.loads(content)
  82. videos = []
  83. vodList = jo['data']['result']
  84. for vod in vodList:
  85. aid = str(vod['aid']).strip()
  86. title = vod['title'].strip().replace("<em class=\"keyword\">","").replace("</em>","")
  87. img = 'https:' + vod['pic'].strip()
  88. remark = str(vod['duration']).strip()
  89. videos.append({
  90. "vod_id":aid,
  91. "vod_name":title,
  92. "vod_pic":img,
  93. "vod_remarks":remark
  94. })
  95. result['list'] = videos
  96. result['page'] = pg
  97. result['pagecount'] = 9999
  98. result['limit'] = 90
  99. result['total'] = 999999
  100. return result
  101. def cleanSpace(self,str):
  102. return str.replace('\n','').replace('\t','').replace('\r','').replace(' ','')
  103. def detailContent(self,array):
  104. aid = array[0]
  105. url = "https://api.bilibili.com/x/web-interface/view?aid={0}".format(aid)
  106. rsp = self.fetch(url,headers=self.header)
  107. jRoot = json.loads(rsp.text)
  108. jo = jRoot['data']
  109. title = jo['title'].replace("<em class=\"keyword\">","").replace("</em>","")
  110. pic = jo['pic']
  111. desc = jo['desc']
  112. typeName = jo['tname']
  113. vod = {
  114. "vod_id":aid,
  115. "vod_name":title,
  116. "vod_pic":pic,
  117. "type_name":typeName,
  118. "vod_year":"",
  119. "vod_area":"",
  120. "vod_remarks":"",
  121. "vod_actor":"",
  122. "vod_director":"",
  123. "vod_content":desc
  124. }
  125. ja = jo['pages']
  126. playUrl = ''
  127. for tmpJo in ja:
  128. cid = tmpJo['cid']
  129. part = tmpJo['part']
  130. playUrl = playUrl + '{0}${1}_{2}#'.format(part,aid,cid)
  131. vod['vod_play_from'] = 'B站'
  132. vod['vod_play_url'] = playUrl
  133. result = {
  134. 'list':[
  135. vod
  136. ]
  137. }
  138. return result
  139. def searchContent(self,key,quick):
  140. result = {
  141. 'list':[]
  142. }
  143. return result
  144. def playerContent(self,flag,id,vipFlags):
  145. # https://www.555dianying.cc/vodplay/static/js/playerconfig.js
  146. result = {}
  147. ids = id.split("_")
  148. url = 'https://api.bilibili.com:443/x/player/playurl?avid={0}&cid=%20%20{1}&qn=112'.format(ids[0],ids[1])
  149. rsp = self.fetch(url)
  150. jRoot = json.loads(rsp.text)
  151. jo = jRoot['data']
  152. ja = jo['durl']
  153. maxSize = -1
  154. position = -1
  155. for i in range(len(ja)):
  156. tmpJo = ja[i]
  157. if maxSize < int(tmpJo['size']):
  158. maxSize = int(tmpJo['size'])
  159. position = i
  160. url = ''
  161. if len(ja) > 0:
  162. if position == -1:
  163. position = 0
  164. url = ja[position]['url']
  165. result["parse"] = 0
  166. result["playUrl"] = ''
  167. result["url"] = url
  168. result["header"] = {
  169. "Referer":"https://www.bilibili.com",
  170. "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"
  171. }
  172. result["contentType"] = 'video/x-flv'
  173. return result
  174. config = {
  175. "player": {},
  176. "filter": {}
  177. }
  178. header = {}
  179. def localProxy(self,param):
  180. return [200, "video/MP2T", action, ""]