py_bilibili_ys.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. "小剧场": "小剧场",
  23. "番剧": "番剧",
  24. "国创": "国创",
  25. "电影": "电影",
  26. "综艺": "综艺",
  27. "电视剧": "电视剧",
  28. "纪录片": "纪录片"
  29. }
  30. classes = []
  31. for k in cateManual:
  32. classes.append({
  33. 'type_name':k,
  34. 'type_id':cateManual[k]
  35. })
  36. result['class'] = classes
  37. if(filter):
  38. result['filters'] = self.config['filter']
  39. return result
  40. def homeVideoContent(self):
  41. result = {
  42. 'list':[]
  43. }
  44. return result
  45. cookies = ''
  46. def getCookie(self):
  47. rsp = self.fetch("https://www.bilibili.com/")
  48. self.cookies = rsp.cookies
  49. return rsp.cookies
  50. def categoryContent(self,tid,pg,filter,extend):
  51. result = {}
  52. url = 'https://api.bilibili.com/x/web-interface/search/type?search_type=video&keyword={0}&duration=4&page={1}'.format(tid,pg)
  53. if len(self.cookies) <= 0:
  54. self.getCookie()
  55. rsp = self.fetch(url,cookies=self.cookies)
  56. content = rsp.text
  57. jo = json.loads(content)
  58. if jo['code'] != 0:
  59. rspRetry = self.fetch(url,cookies=self.getCookie())
  60. content = rspRetry.text
  61. jo = json.loads(content)
  62. videos = []
  63. vodList = jo['data']['result']
  64. for vod in vodList:
  65. aid = str(vod['aid']).strip()
  66. title = vod['title'].strip().replace("<em class=\"keyword\">","").replace("</em>","")
  67. img = 'https:' + vod['pic'].strip()
  68. remark = str(vod['duration']).strip()
  69. videos.append({
  70. "vod_id":aid,
  71. "vod_name":title,
  72. "vod_pic":img,
  73. "vod_remarks":remark
  74. })
  75. result['list'] = videos
  76. result['page'] = pg
  77. result['pagecount'] = 9999
  78. result['limit'] = 90
  79. result['total'] = 999999
  80. return result
  81. def cleanSpace(self,str):
  82. return str.replace('\n','').replace('\t','').replace('\r','').replace(' ','')
  83. def detailContent(self,array):
  84. aid = array[0]
  85. url = "https://api.bilibili.com/x/web-interface/view?aid={0}".format(aid)
  86. rsp = self.fetch(url,headers=self.header)
  87. jRoot = json.loads(rsp.text)
  88. jo = jRoot['data']
  89. title = jo['title'].replace("<em class=\"keyword\">","").replace("</em>","")
  90. pic = jo['pic']
  91. desc = jo['desc']
  92. typeName = jo['tname']
  93. vod = {
  94. "vod_id":aid,
  95. "vod_name":title,
  96. "vod_pic":pic,
  97. "type_name":typeName,
  98. "vod_year":"",
  99. "vod_area":"",
  100. "vod_remarks":"",
  101. "vod_actor":"",
  102. "vod_director":"",
  103. "vod_content":desc
  104. }
  105. ja = jo['pages']
  106. playUrl = ''
  107. for tmpJo in ja:
  108. cid = tmpJo['cid']
  109. part = tmpJo['part']
  110. playUrl = playUrl + '{0}${1}_{2}#'.format(part,aid,cid)
  111. vod['vod_play_from'] = 'B站'
  112. vod['vod_play_url'] = playUrl
  113. result = {
  114. 'list':[
  115. vod
  116. ]
  117. }
  118. return result
  119. def searchContent(self,key,quick):
  120. result = {
  121. 'list':[]
  122. }
  123. return result
  124. def playerContent(self,flag,id,vipFlags):
  125. # https://www.555dianying.cc/vodplay/static/js/playerconfig.js
  126. result = {}
  127. ids = id.split("_")
  128. url = 'https://api.bilibili.com:443/x/player/playurl?avid={0}&cid=%20%20{1}&qn=112'.format(ids[0],ids[1])
  129. rsp = self.fetch(url)
  130. jRoot = json.loads(rsp.text)
  131. jo = jRoot['data']
  132. ja = jo['durl']
  133. maxSize = -1
  134. position = -1
  135. for i in range(len(ja)):
  136. tmpJo = ja[i]
  137. if maxSize < int(tmpJo['size']):
  138. maxSize = int(tmpJo['size'])
  139. position = i
  140. url = ''
  141. if len(ja) > 0:
  142. if position == -1:
  143. position = 0
  144. url = ja[position]['url']
  145. result["parse"] = 0
  146. result["playUrl"] = ''
  147. result["url"] = url
  148. result["header"] = {
  149. "Referer":"https://www.bilibili.com",
  150. "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"
  151. }
  152. result["contentType"] = 'video/x-flv'
  153. return result
  154. config = {
  155. "player": {},
  156. "filter": {}
  157. }
  158. header = {}
  159. def localProxy(self,param):
  160. return [200, "video/MP2T", action, ""]