2
0

py_bilibili.py 4.8 KB

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