py_bilivd.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. "演唱会": "演唱会4K",
  27. "韩国女团": "韩国女团4K",
  28. "日本女团": "日本女团4K",
  29. "MV": "MV 4K",
  30. "舞曲": "DJ舞曲 4K",
  31. "纯音乐": "纯音乐 轻音乐",
  32. "假窗-白噪音": "窗+白噪音",
  33. "纪录片": "纪录片",
  34. "昆虫": "昆虫",
  35. "玩具汽车": "玩具汽车",
  36. "儿童": "儿童",
  37. "幼儿": "幼儿",
  38. "儿童玩具": "儿童玩具"
  39. }
  40. classes = []
  41. for k in cateManual:
  42. classes.append({
  43. 'type_name': k,
  44. 'type_id': cateManual[k]
  45. })
  46. result['class'] = classes
  47. if (filter):
  48. result['filters'] = self.config['filter']
  49. return result
  50. def homeVideoContent(self):
  51. result = {
  52. 'list': []
  53. }
  54. return result
  55. cookies = ''
  56. def getCookie(self):
  57. # 在cookies_str中填入会员或大会员cookie,以获得更好的体验。
  58. cookies_str = "innersign=0; buvid3=0BE6DC00-7EC8-F14C-5022-273B6A46C4C146692infoc; i-wanna-go-back=-1; _uuid=9876F5E7-A2610-17F3-A821-453368B2A13B46427infoc; buvid4=909DE9E8-B2F3-0737-BE05-CBCE52B8F1DF50069-022082212-KrOZh+8iydjdQTLhzp96QF38tYbYJ8K8cXQog40LGu3fOfuINFkXpw%3D%3D; LIVE_BUVID=AUTO5316611442538552; b_timer=%7B%22ffp%22%3A%7B%22333.1007.fp.risk_0BE6DC00%22%3A%22182C3E9AD8B%22%2C%22333.42.fp.risk_0BE6DC00%22%3A%22182C3E9D59E%22%7D%7D; buvid_fp_plain=undefined; SESSDATA=ddac727d%2C1676696334%2C25fac%2A81; bili_jct=cfeaf9060dc34924f82d015212a108c5; DedeUserID=389957880; DedeUserID__ckMd5=42e393d5b4adaf41; sid=8sdohh2u; fingerprint=7564ac47b00541a5b904d49f13e9989a; fingerprint3=4b65d5467158b4fcf6747d29562dd032; b_ut=5; buvid_fp=6393a802b9740ddf1b1443b39e2e9b50; b_lsid=2F7F5E96_18312CBD612"
  59. cookies_dic = dict([co.strip().split('=') for co in cookies_str.split(';')])
  60. rsp = session()
  61. cookies_jar = utils.cookiejar_from_dict(cookies_dic)
  62. rsp.cookies = cookies_jar
  63. content = self.fetch("http://api.bilibili.com/x/web-interface/nav", cookies=rsp.cookies)
  64. res = json.loads(content.text)
  65. if res["code"] == 0:
  66. self.cookies = rsp.cookies
  67. else:
  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}&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. videos = []
  80. vodList = jo['data']['result']
  81. for vod in vodList:
  82. aid = str(vod['aid']).strip()
  83. title = vod['title'].replace("<em class=\"keyword\">", "").replace("</em>", "").replace("&quot;", '"')
  84. img = 'https:' + vod['pic'].strip()
  85. remark = str(vod['duration']).strip()
  86. videos.append({
  87. "vod_id": aid,
  88. "vod_name": title,
  89. "vod_pic": img,
  90. "vod_remarks": remark
  91. })
  92. result['list'] = videos
  93. result['page'] = pg
  94. result['pagecount'] = 9999
  95. result['limit'] = 90
  96. result['total'] = 999999
  97. return result
  98. def cleanSpace(self, str):
  99. return str.replace('\n', '').replace('\t', '').replace('\r', '').replace(' ', '')
  100. def detailContent(self, array):
  101. aid = array[0]
  102. url = "https://api.bilibili.com/x/web-interface/view?aid={0}".format(aid)
  103. rsp = self.fetch(url, headers=self.header)
  104. jRoot = json.loads(rsp.text)
  105. jo = jRoot['data']
  106. title = jo['title'].replace("<em class=\"keyword\">", "").replace("</em>", "")
  107. pic = jo['pic']
  108. desc = jo['desc']
  109. timeStamp = jo['pubdate']
  110. timeArray = time.localtime(timeStamp)
  111. year = str(time.strftime("%Y", timeArray))
  112. dire = jo['owner']['name']
  113. typeName = jo['tname']
  114. remark = str(jo['duration']).strip()
  115. vod = {
  116. "vod_id": aid,
  117. "vod_name": title,
  118. "vod_pic": pic,
  119. "type_name": typeName,
  120. "vod_year": year,
  121. "vod_area": "",
  122. "vod_remarks": remark,
  123. "vod_actor": "",
  124. "vod_director": dire,
  125. "vod_content": desc
  126. }
  127. ja = jo['pages']
  128. playUrl = ''
  129. for tmpJo in ja:
  130. cid = tmpJo['cid']
  131. part = tmpJo['part'].replace("#", "-")
  132. playUrl = playUrl + '{0}${1}_{2}#'.format(part, aid, cid)
  133. vod['vod_play_from'] = 'B站视频'
  134. vod['vod_play_url'] = playUrl
  135. result = {
  136. 'list': [
  137. vod
  138. ]
  139. }
  140. return result
  141. def searchContent(self, key, quick):
  142. header = {
  143. "Referer": "https://www.bilibili.com",
  144. "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"
  145. }
  146. url = 'https://api.bilibili.com/x/web-interface/search/type?search_type=video&keyword={0}'.format(key)
  147. if len(self.cookies) <= 0:
  148. self.getCookie()
  149. rsp = self.fetch(url, cookies=self.cookies,headers=header)
  150. content = rsp.text
  151. jo = json.loads(content)
  152. if jo['code'] != 0:
  153. rspRetry = self.fetch(url, cookies=self.getCookie())
  154. content = rspRetry.text
  155. jo = json.loads(content)
  156. videos = []
  157. vodList = jo['data']['result']
  158. for vod in vodList:
  159. aid = str(vod['aid']).strip()
  160. title = vod['title'].replace("<em class=\"keyword\">", "").replace("</em>", "").replace("&quot;", '"')
  161. img = 'https:' + vod['pic'].strip()
  162. remark = str(vod['duration']).strip()
  163. videos.append({
  164. "vod_id": aid,
  165. "vod_name": title,
  166. "vod_pic": img,
  167. "vod_remarks": remark
  168. })
  169. result = {
  170. 'list': videos
  171. }
  172. return result
  173. def playerContent(self, flag, id, vipFlags):
  174. result = {}
  175. ids = id.split("_")
  176. url = 'https://api.bilibili.com:443/x/player/playurl?avid={0}&cid={1}&qn=116'.format(ids[0], ids[1])
  177. if len(self.cookies) <= 0:
  178. self.getCookie()
  179. rsp = self.fetch(url, cookies=self.cookies)
  180. jRoot = json.loads(rsp.text)
  181. jo = jRoot['data']
  182. ja = jo['durl']
  183. maxSize = -1
  184. position = -1
  185. for i in range(len(ja)):
  186. tmpJo = ja[i]
  187. if maxSize < int(tmpJo['size']):
  188. maxSize = int(tmpJo['size'])
  189. position = i
  190. url = ''
  191. if len(ja) > 0:
  192. if position == -1:
  193. position = 0
  194. url = ja[position]['url']
  195. result["parse"] = 0
  196. result["playUrl"] = ''
  197. result["url"] = url
  198. result["header"] = {
  199. "Referer": "https://www.bilibili.com",
  200. "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"
  201. }
  202. result["contentType"] = 'video/x-flv'
  203. return result
  204. config = {
  205. "player": {},
  206. "filter": {}
  207. }
  208. header = {}
  209. def localProxy(self, param):
  210. return [200, "video/MP2T", action, ""]