py_bilivd.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 requests
  8. from requests import session, utils
  9. import os
  10. import time
  11. import base64
  12. class Spider(Spider): # 元类 默认的元类 type
  13. def getName(self):
  14. return "哔哩"
  15. def init(self, extend=""):
  16. print("============{0}============".format(extend))
  17. pass
  18. def isVideoFormat(self, url):
  19. pass
  20. def manualVideoCheck(self):
  21. pass
  22. def homeContent(self, filter):
  23. result = {}
  24. cateManual = {
  25. "搞笑": "搞笑",
  26. "美食": "美食",
  27. "科普": "科普",
  28. "橙子": "半个橙子",
  29. "纪录片": "纪录片",
  30. "演唱会": "演唱会",
  31. "动物世界": "动物世界",
  32. "相声小品": "相声小品",
  33. "假窗-白噪音": "窗+白噪音"
  34. }
  35. classes = []
  36. for k in cateManual:
  37. classes.append({
  38. 'type_name': k,
  39. 'type_id': cateManual[k]
  40. })
  41. result['class'] = classes
  42. if (filter):
  43. result['filters'] = self.config['filter']
  44. return result
  45. def homeVideoContent(self):
  46. result = {
  47. 'list': []
  48. }
  49. return result
  50. cookies = ''
  51. def getCookie(self):
  52. cookies_str = "" # 填B站Cookies
  53. cookies_dic = dict([co.strip().split('=') for co in cookies_str.split(';')])
  54. rsp = session()
  55. cookies_jar = utils.cookiejar_from_dict(cookies_dic)
  56. rsp.cookies = cookies_jar
  57. content = self.fetch("http://api.bilibili.com/x/web-interface/nav", cookies=rsp.cookies)
  58. res = json.loads(content.text)
  59. if res["code"] == 0:
  60. self.cookies = rsp.cookies
  61. else:
  62. rsp = self.fetch("https://www.bilibili.com/")
  63. self.cookies = rsp.cookies
  64. return rsp.cookies
  65. def categoryContent(self, tid, pg, filter, extend):
  66. result = {}
  67. url = 'https://api.bilibili.com/x/web-interface/search/type?search_type=video&keyword={0}&page={1}'.format(tid, pg)
  68. if len(self.cookies) <= 0:
  69. self.getCookie()
  70. rsp = self.fetch(url, cookies=self.cookies)
  71. content = rsp.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. timeStamp = jo['pubdate']
  104. timeArray = time.localtime(timeStamp)
  105. year = str(time.strftime("%Y", timeArray))
  106. dire = jo['owner']['name']
  107. typeName = jo['tname']
  108. remark = str(jo['duration']).strip()
  109. vod = {
  110. "vod_id": aid,
  111. "vod_name": title,
  112. "vod_pic": pic,
  113. "type_name": typeName,
  114. "vod_year": year,
  115. "vod_area": "",
  116. "vod_remarks": remark,
  117. "vod_actor": "",
  118. "vod_director": dire,
  119. "vod_content": ""
  120. }
  121. ja = jo['pages']
  122. playUrl = ''
  123. for tmpJo in ja:
  124. cid = tmpJo['cid']
  125. part = tmpJo['part'].replace("#", "-")
  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. header = {
  137. "Referer": "https://www.bilibili.com",
  138. "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"
  139. }
  140. url = 'https://api.bilibili.com/x/web-interface/search/type?search_type=video&keyword={0}'.format(key)
  141. if len(self.cookies) <= 0:
  142. self.getCookie()
  143. rsp = self.fetch(url, cookies=self.cookies,headers=header)
  144. content = rsp.text
  145. jo = json.loads(content)
  146. if jo['code'] != 0:
  147. rspRetry = self.fetch(url, cookies=self.getCookie())
  148. content = rspRetry.text
  149. jo = json.loads(content)
  150. videos = []
  151. vodList = jo['data']['result']
  152. for vod in vodList:
  153. aid = str(vod['aid']).strip()
  154. title = vod['title'].strip().replace("<em class=\"keyword\">", "").replace("</em>", "")
  155. img = 'https:' + vod['pic'].strip()
  156. remark = str(vod['duration']).strip()
  157. videos.append({
  158. "vod_id": aid,
  159. "vod_name": title,
  160. "vod_pic": img,
  161. "vod_remarks": remark
  162. })
  163. result = {
  164. 'list': videos
  165. }
  166. return result
  167. def playerContent(self, flag, id, vipFlags):
  168. result = {}
  169. ids = id.split("_")
  170. url = 'https://api.bilibili.com:443/x/player/playurl?avid={0}&cid={1}&qn=116'.format(ids[0], ids[1])
  171. if len(self.cookies) <= 0:
  172. self.getCookie()
  173. rsp = self.fetch(url, cookies=self.cookies)
  174. jRoot = json.loads(rsp.text)
  175. jo = jRoot['data']
  176. ja = jo['durl']
  177. maxSize = -1
  178. position = -1
  179. for i in range(len(ja)):
  180. tmpJo = ja[i]
  181. if maxSize < int(tmpJo['size']):
  182. maxSize = int(tmpJo['size'])
  183. position = i
  184. url = ''
  185. if len(ja) > 0:
  186. if position == -1:
  187. position = 0
  188. url = ja[position]['url']
  189. result["parse"] = 0
  190. result["playUrl"] = ''
  191. result["url"] = url
  192. result["header"] = {
  193. "Referer": "https://www.bilibili.com",
  194. "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"
  195. }
  196. result["contentType"] = 'video/x-flv'
  197. return result
  198. config = {
  199. "player": {},
  200. "filter": {}
  201. }
  202. header = {}
  203. def localProxy(self, param):
  204. return [200, "video/MP2T", action, ""]