py_bilibili_hj.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. "粤语MV4k合集": "粤语MV4k合集",
  23. "2022年热门MV4k合集": "2022年热门MV4k合集",
  24. "KTV热门MV4k合集": "KTV热门MV4k合集",
  25. "滚石经典MV4k合集": "滚石经典MV4k合集",
  26. "经典老歌MV4k合集": "经典老歌MV4k合集",
  27. "古风MV4k合集": "古风MV4k合集",
  28. "闽南语MV4k合集": "闽南语MVMV4k合集",
  29. "印度歌舞MV4k合集": "印度歌舞MV4k合集",
  30. "网红翻唱MV4k合集": "网红翻唱MV4k合集",
  31. "韩国女团MV4k合集": "韩国女团MV4k合集",
  32. "A阿黛尔MV4k合集": "A阿黛尔MV4k合集",
  33. "BlackpinkMV4k合集": "BlackpinkMV4k合集",
  34. "BeyondMV4k合集": "BeyondMV4k合集",
  35. "坂井泉水MV4k合集": "坂井泉水MV4k合集",
  36. "宝丽金MV4k合集": "宝丽金MV4k合集",
  37. "布兰妮MV4k合集": "布兰妮MV4k合集",
  38. "崔健MV4k合集": "崔健MV4k合集",
  39. "莫文蔚MV4k合集": "莫文蔚MV4k合集",
  40. "孟庭苇MV4k合集": "孟庭苇MV4k合集",
  41. "M麦当娜MV4k合集": "M麦当娜MV4k合集",
  42. "M迈克杰克逊MV4k合集": "M迈克杰克逊MV4k合集",
  43. "W伍佰MV4k合集": "W伍佰MV4k合集",
  44. "W温兆伦MV4k合集": "W温兆伦MV4k合集",
  45. "W王菲MV4k合集": "W王菲MV4k合集",
  46. "X徐小凤MV4k合集": "X徐小凤MV4k合集",
  47. "X席琳迪翁MV4k合集": "X席琳迪翁MV4k合集",
  48. "X许嵩MV4k合集": "X许嵩MV4k合集",
  49. "X许美静MV4k合集": "X许美静MV4k合集",
  50. "C草蜢MV4k合集": "C草蜢MV4k合集"
  51. }
  52. classes = []
  53. for k in cateManual:
  54. classes.append({
  55. 'type_name':k,
  56. 'type_id':cateManual[k]
  57. })
  58. result['class'] = classes
  59. if(filter):
  60. result['filters'] = self.config['filter']
  61. return result
  62. def homeVideoContent(self):
  63. result = {
  64. 'list':[]
  65. }
  66. return result
  67. cookies = ''
  68. def getCookie(self):
  69. rsp = self.fetch("https://www.bilibili.com/")
  70. self.cookies = rsp.cookies
  71. return rsp.cookies
  72. def categoryContent(self,tid,pg,filter,extend):
  73. result = {}
  74. url = 'https://api.bilibili.com/x/web-interface/search/type?search_type=video&keyword={0}&duration=4&page={1}'.format(tid,pg)
  75. if len(self.cookies) <= 0:
  76. self.getCookie()
  77. rsp = self.fetch(url,cookies=self.cookies)
  78. content = rsp.text
  79. jo = json.loads(content)
  80. if jo['code'] != 0:
  81. rspRetry = self.fetch(url,cookies=self.getCookie())
  82. content = rspRetry.text
  83. jo = json.loads(content)
  84. videos = []
  85. vodList = jo['data']['result']
  86. for vod in vodList:
  87. aid = str(vod['aid']).strip()
  88. title = vod['title'].strip().replace("<em class=\"keyword\">","").replace("</em>","")
  89. img = 'https:' + vod['pic'].strip()
  90. remark = str(vod['duration']).strip()
  91. videos.append({
  92. "vod_id":aid,
  93. "vod_name":title,
  94. "vod_pic":img,
  95. "vod_remarks":remark
  96. })
  97. result['list'] = videos
  98. result['page'] = pg
  99. result['pagecount'] = 9999
  100. result['limit'] = 90
  101. result['total'] = 999999
  102. return result
  103. def cleanSpace(self,str):
  104. return str.replace('\n','').replace('\t','').replace('\r','').replace(' ','')
  105. def detailContent(self,array):
  106. aid = array[0]
  107. url = "https://api.bilibili.com/x/web-interface/view?aid={0}".format(aid)
  108. rsp = self.fetch(url,headers=self.header)
  109. jRoot = json.loads(rsp.text)
  110. jo = jRoot['data']
  111. title = jo['title'].replace("<em class=\"keyword\">","").replace("</em>","")
  112. pic = jo['pic']
  113. desc = jo['desc']
  114. typeName = jo['tname']
  115. vod = {
  116. "vod_id":aid,
  117. "vod_name":title,
  118. "vod_pic":pic,
  119. "type_name":typeName,
  120. "vod_year":"",
  121. "vod_area":"",
  122. "vod_remarks":"",
  123. "vod_actor":"",
  124. "vod_director":"",
  125. "vod_content":desc
  126. }
  127. ja = jo['pages']
  128. playUrl = ''
  129. for tmpJo in ja:
  130. cid = tmpJo['cid']
  131. part = tmpJo['part']
  132. playUrl = playUrl + '{0}${1}_{2}#'.format(part,aid,cid)
  133. vod['vod_play_from'] = 'B站'
  134. vod['vod_play_url'] = playUrl
  135. result = {
  136. 'list':[
  137. vod
  138. ]
  139. }
  140. return result
  141. def searchContent(self,key,quick):
  142. result = {
  143. 'list':[]
  144. }
  145. return result
  146. def playerContent(self,flag,id,vipFlags):
  147. # https://www.555dianying.cc/vodplay/static/js/playerconfig.js
  148. result = {}
  149. ids = id.split("_")
  150. url = 'https://api.bilibili.com:443/x/player/playurl?avid={0}&cid=%20%20{1}&qn=112'.format(ids[0],ids[1])
  151. rsp = self.fetch(url)
  152. jRoot = json.loads(rsp.text)
  153. jo = jRoot['data']
  154. ja = jo['durl']
  155. maxSize = -1
  156. position = -1
  157. for i in range(len(ja)):
  158. tmpJo = ja[i]
  159. if maxSize < int(tmpJo['size']):
  160. maxSize = int(tmpJo['size'])
  161. position = i
  162. url = ''
  163. if len(ja) > 0:
  164. if position == -1:
  165. position = 0
  166. url = ja[position]['url']
  167. result["parse"] = 0
  168. result["playUrl"] = ''
  169. result["url"] = url
  170. result["header"] = {
  171. "Referer":"https://www.bilibili.com",
  172. "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"
  173. }
  174. result["contentType"] = 'video/x-flv'
  175. return result
  176. config = {
  177. "player": {},
  178. "filter": {}
  179. }
  180. header = {}
  181. def localProxy(self,param):
  182. return [200, "video/MP2T", action, ""]