py_bilibili_xx.py 4.7 KB

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