py_bilivd.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. "Zard": "Zard",
  26. "玩具汽车": "玩具汽车",
  27. "儿童": "儿童",
  28. "幼儿": "幼儿",
  29. "儿童玩具": "儿童玩具",
  30. "昆虫": "昆虫",
  31. "动物世界": "动物世界",
  32. "纪录片": "纪录片",
  33. "相声小品": "相声小品",
  34. "搞笑": "搞笑",
  35. "假窗-白噪音": "窗+白噪音",
  36. "演唱会": "演唱会"
  37. }
  38. classes = []
  39. for k in cateManual:
  40. classes.append({
  41. 'type_name': k,
  42. 'type_id': cateManual[k]
  43. })
  44. result['class'] = classes
  45. if (filter):
  46. result['filters'] = self.config['filter']
  47. return result
  48. def homeVideoContent(self):
  49. result = {
  50. 'list': []
  51. }
  52. return result
  53. cookies = ''
  54. def getCookie(self):
  55. # 在cookies_str中填入会员或大会员cookie,以获得更好的体验。
  56. cookies_str = "innersign=0; buvid3=606BE156-AE37-AEA8-7052-9DA0B21766E776404infoc; b_nut=1663302976; i-wanna-go-back=-1; b_ut=7; b_lsid=4106252F6_18344933A90; _uuid=586AAEB7-6B88-A691-F7AC-95C27E57F53C43036infoc; buvid4=B6FF1449-4361-1C76-DEFC-4AFCA1777B7E78304-022091612-PdJr0jKE6N5TamfAEX9uACD1RXvklspbNdlcIQEFLMu0d9wS3G3sdA%3D%3D; buvid_fp=2a9b54d5e06aa54293dc7544e000552d"
  57. cookies_dic = dict([co.strip().split('=') for co in cookies_str.split(';')])
  58. rsp = session()
  59. cookies_jar = utils.cookiejar_from_dict(cookies_dic)
  60. rsp.cookies = cookies_jar
  61. content = self.fetch("http://api.bilibili.com/x/web-interface/nav", cookies=rsp.cookies)
  62. res = json.loads(content.text)
  63. if res["code"] == 0:
  64. self.cookies = rsp.cookies
  65. else:
  66. rsp = self.fetch("https://www.bilibili.com/")
  67. self.cookies = rsp.cookies
  68. return rsp.cookies
  69. def categoryContent(self, tid, pg, filter, extend):
  70. result = {}
  71. url = 'https://api.bilibili.com/x/web-interface/search/type?search_type=video&keyword={0}&page={1}'.format(tid, pg)
  72. if len(self.cookies) <= 0:
  73. self.getCookie()
  74. rsp = self.fetch(url, cookies=self.cookies)
  75. content = rsp.text
  76. jo = json.loads(content)
  77. videos = []
  78. vodList = jo['data']['result']
  79. for vod in vodList:
  80. aid = str(vod['aid']).strip()
  81. title = vod['title'].replace("<em class=\"keyword\">", "").replace("</em>", "").replace("&quot;", '"')
  82. img = 'https:' + vod['pic'].strip()
  83. remark = str(vod['duration']).strip()
  84. videos.append({
  85. "vod_id": aid,
  86. "vod_name": title,
  87. "vod_pic": img,
  88. "vod_remarks": remark
  89. })
  90. result['list'] = videos
  91. result['page'] = pg
  92. result['pagecount'] = 9999
  93. result['limit'] = 90
  94. result['total'] = 999999
  95. return result
  96. def cleanSpace(self, str):
  97. return str.replace('\n', '').replace('\t', '').replace('\r', '').replace(' ', '')
  98. def detailContent(self, array):
  99. aid = array[0]
  100. url = "https://api.bilibili.com/x/web-interface/view?aid={0}".format(aid)
  101. rsp = self.fetch(url, headers=self.header)
  102. jRoot = json.loads(rsp.text)
  103. jo = jRoot['data']
  104. title = jo['title'].replace("<em class=\"keyword\">", "").replace("</em>", "")
  105. pic = jo['pic']
  106. desc = jo['desc']
  107. timeStamp = jo['pubdate']
  108. timeArray = time.localtime(timeStamp)
  109. year = str(time.strftime("%Y", timeArray))
  110. dire = jo['owner']['name']
  111. typeName = jo['tname']
  112. remark = str(jo['duration']).strip()
  113. vod = {
  114. "vod_id": aid,
  115. "vod_name": title,
  116. "vod_pic": pic,
  117. "type_name": typeName,
  118. "vod_year": year,
  119. "vod_area": "",
  120. "vod_remarks": remark,
  121. "vod_actor": "",
  122. "vod_director": dire,
  123. "vod_content": desc
  124. }
  125. ja = jo['pages']
  126. playUrl = ''
  127. for tmpJo in ja:
  128. cid = tmpJo['cid']
  129. part = tmpJo['part'].replace("#", "-")
  130. playUrl = playUrl + '{0}${1}_{2}#'.format(part, aid, cid)
  131. vod['vod_play_from'] = 'B站视频'
  132. vod['vod_play_url'] = playUrl
  133. result = {
  134. 'list': [
  135. vod
  136. ]
  137. }
  138. return result
  139. def searchContent(self, key, quick):
  140. header = {
  141. "Referer": "https://www.bilibili.com",
  142. "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"
  143. }
  144. url = 'https://api.bilibili.com/x/web-interface/search/type?search_type=video&keyword={0}'.format(key)
  145. if len(self.cookies) <= 0:
  146. self.getCookie()
  147. rsp = self.fetch(url, cookies=self.cookies,headers=header)
  148. content = rsp.text
  149. jo = json.loads(content)
  150. if jo['code'] != 0:
  151. rspRetry = self.fetch(url, cookies=self.getCookie())
  152. content = rspRetry.text
  153. jo = json.loads(content)
  154. videos = []
  155. vodList = jo['data']['result']
  156. for vod in vodList:
  157. aid = str(vod['aid']).strip()
  158. title = vod['title'].replace("<em class=\"keyword\">", "").replace("</em>", "").replace("&quot;", '"')
  159. img = 'https:' + vod['pic'].strip()
  160. remark = str(vod['duration']).strip()
  161. videos.append({
  162. "vod_id": aid,
  163. "vod_name": title,
  164. "vod_pic": img,
  165. "vod_remarks": remark
  166. })
  167. result = {
  168. 'list': videos
  169. }
  170. return result
  171. def playerContent(self, flag, id, vipFlags):
  172. result = {}
  173. ids = id.split("_")
  174. url = 'https://api.bilibili.com:443/x/player/playurl?avid={0}&cid={1}&qn=116'.format(ids[0], ids[1])
  175. if len(self.cookies) <= 0:
  176. self.getCookie()
  177. rsp = self.fetch(url, cookies=self.cookies)
  178. jRoot = json.loads(rsp.text)
  179. jo = jRoot['data']
  180. ja = jo['durl']
  181. maxSize = -1
  182. position = -1
  183. for i in range(len(ja)):
  184. tmpJo = ja[i]
  185. if maxSize < int(tmpJo['size']):
  186. maxSize = int(tmpJo['size'])
  187. position = i
  188. url = ''
  189. if len(ja) > 0:
  190. if position == -1:
  191. position = 0
  192. url = ja[position]['url']
  193. result["parse"] = 0
  194. result["playUrl"] = ''
  195. result["url"] = url
  196. result["header"] = {
  197. "Referer": "https://www.bilibili.com",
  198. "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"
  199. }
  200. result["contentType"] = 'video/x-flv'
  201. return result
  202. config = {
  203. "player": {},
  204. "filter": {}
  205. }
  206. header = {}
  207. def localProxy(self, param):
  208. return [200, "video/MP2T", action, ""]