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