py_bilibili2.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. "zane妈":"zane妈",
  26. "相声小品": "相声小品",
  27. "林芊妤":"林芊妤",
  28. "Zard": "Zard",
  29. "玩具汽车": "玩具汽车",
  30. "儿童": "儿童",
  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. import requests
  58. import http.cookies
  59. raw_cookie_line = ''
  60. simple_cookie = http.cookies.SimpleCookie(raw_cookie_line)
  61. cookie_jar = requests.cookies.RequestsCookieJar()
  62. cookie_jar.update(simple_cookie)
  63. return cookie_jar
  64. def get_dynamic(self,pg):
  65. result = {}
  66. if int(pg) > 1:
  67. return result
  68. offset = ''
  69. videos = []
  70. for i in range(0,10):
  71. url= 'https://api.bilibili.com/x/polymer/web-dynamic/v1/feed/all?timezone_offset=-480&type=all&page={0}&offset={1}'.format(pg,offset)
  72. rsp = self.fetch(url,cookies=self.getCookie())
  73. content = rsp.text
  74. jo = json.loads(content)
  75. if jo['code'] == 0:
  76. offset = jo['data']['offset']
  77. vodList = jo['data']['items']
  78. for vod in vodList:
  79. if vod['type'] == 'DYNAMIC_TYPE_AV':
  80. ivod = vod['modules']['module_dynamic']['major']['archive']
  81. aid = str(ivod['aid']).strip()
  82. title = ivod['title'].strip().replace("<em class=\"keyword\">","").replace("</em>","")
  83. img = ivod['cover'].strip()
  84. remark = str(ivod['duration_text']).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 get_hot(self,pg):
  98. result = {}
  99. url= 'https://api.bilibili.com/x/web-interface/popular?ps=20&pn={0}'.format(pg)
  100. rsp = self.fetch(url,cookies=self.getCookie())
  101. content = rsp.text
  102. jo = json.loads(content)
  103. if jo['code'] == 0:
  104. videos = []
  105. vodList = jo['data']['list']
  106. for vod in vodList:
  107. aid = str(vod['aid']).strip()
  108. title = vod['title'].strip().replace("<em class=\"keyword\">","").replace("</em>","")
  109. img = vod['pic'].strip()
  110. remark = str(vod['duration']).strip()
  111. videos.append({
  112. "vod_id":aid,
  113. "vod_name":title,
  114. "vod_pic":img,
  115. "vod_remarks":remark
  116. })
  117. result['list'] = videos
  118. result['page'] = pg
  119. result['pagecount'] = 9999
  120. result['limit'] = 90
  121. result['total'] = 999999
  122. return result
  123. def get_rank(self):
  124. result = {}
  125. url= 'https://api.bilibili.com/x/web-interface/ranking/v2?rid=0&type=all'
  126. rsp = self.fetch(url,cookies=self.getCookie())
  127. content = rsp.text
  128. jo = json.loads(content)
  129. if jo['code'] == 0:
  130. videos = []
  131. vodList = jo['data']['list']
  132. for vod in vodList:
  133. aid = str(vod['aid']).strip()
  134. title = vod['title'].strip().replace("<em class=\"keyword\">","").replace("</em>","")
  135. img = vod['pic'].strip()
  136. remark = str(vod['duration']).strip()
  137. videos.append({
  138. "vod_id":aid,
  139. "vod_name":title,
  140. "vod_pic":img,
  141. "vod_remarks":remark
  142. })
  143. result['list'] = videos
  144. result['page'] = 1
  145. result['pagecount'] = 1
  146. result['limit'] = 90
  147. result['total'] = 999999
  148. return result
  149. def categoryContent(self,tid,pg,filter,extend):
  150. result = {}
  151. if tid == "热门":
  152. return self.get_hot(pg=pg)
  153. if tid == "排行榜" :
  154. return self.get_rank()
  155. if tid == '动态':
  156. return self.get_dynamic(pg=pg)
  157. url = 'https://api.bilibili.com/x/web-interface/search/type?search_type=video&keyword={0}&page={1}'.format(tid,pg)
  158. if len(self.cookies) <= 0:
  159. self.getCookie()
  160. rsp = self.fetch(url,cookies=self.getCookie())
  161. content = rsp.text
  162. jo = json.loads(content)
  163. if jo['code'] != 0:
  164. rspRetry = self.fetch(url,cookies=self.getCookie())
  165. content = rspRetry.text
  166. jo = json.loads(content)
  167. videos = []
  168. vodList = jo['data']['result']
  169. for vod in vodList:
  170. aid = str(vod['aid']).strip()
  171. title = tid + ":" + vod['title'].strip().replace("<em class=\"keyword\">","").replace("</em>","")
  172. img = 'https:' + vod['pic'].strip()
  173. remark = str(vod['duration']).strip()
  174. videos.append({
  175. "vod_id":aid,
  176. "vod_name":title,
  177. "vod_pic":img,
  178. "vod_remarks":remark
  179. })
  180. result['list'] = videos
  181. result['page'] = pg
  182. result['pagecount'] = 9999
  183. result['limit'] = 90
  184. result['total'] = 999999
  185. return result
  186. def cleanSpace(self,str):
  187. return str.replace('\n','').replace('\t','').replace('\r','').replace(' ','')
  188. def detailContent(self,array):
  189. aid = array[0]
  190. url = "https://api.bilibili.com/x/web-interface/view?aid={0}".format(aid)
  191. rsp = self.fetch(url,headers=self.header,cookies=self.getCookie())
  192. jRoot = json.loads(rsp.text)
  193. jo = jRoot['data']
  194. title = jo['title'].replace("<em class=\"keyword\">","").replace("</em>","")
  195. pic = jo['pic']
  196. desc = jo['desc']
  197. typeName = jo['tname']
  198. vod = {
  199. "vod_id":aid,
  200. "vod_name":title,
  201. "vod_pic":pic,
  202. "type_name":typeName,
  203. "vod_year":"",
  204. "vod_area":"bilidanmu",
  205. "vod_remarks":"",
  206. "vod_actor":jo['owner']['name'],
  207. "vod_director":jo['owner']['name'],
  208. "vod_content":desc
  209. }
  210. ja = jo['pages']
  211. playUrl = ''
  212. for tmpJo in ja:
  213. cid = tmpJo['cid']
  214. part = tmpJo['part']
  215. playUrl = playUrl + '{0}${1}_{2}#'.format(part,aid,cid)
  216. vod['vod_play_from'] = 'B站'
  217. vod['vod_play_url'] = playUrl
  218. result = {
  219. 'list':[
  220. vod
  221. ]
  222. }
  223. return result
  224. def searchContent(self,key,quick):
  225. search = self.categoryContent(tid=key,pg=1,filter=None,extend=None)
  226. result = {
  227. 'list':search['list']
  228. }
  229. return result
  230. def playerContent(self,flag,id,vipFlags):
  231. # https://www.555dianying.cc/vodplay/static/js/playerconfig.js
  232. result = {}
  233. ids = id.split("_")
  234. url = 'https://api.bilibili.com:443/x/player/playurl?avid={0}&cid=%20%20{1}&qn=112'.format(ids[0],ids[1])
  235. rsp = self.fetch(url,cookies=self.getCookie())
  236. jRoot = json.loads(rsp.text)
  237. jo = jRoot['data']
  238. ja = jo['durl']
  239. maxSize = -1
  240. position = -1
  241. for i in range(len(ja)):
  242. tmpJo = ja[i]
  243. if maxSize < int(tmpJo['size']):
  244. maxSize = int(tmpJo['size'])
  245. position = i
  246. url = ''
  247. if len(ja) > 0:
  248. if position == -1:
  249. position = 0
  250. url = ja[position]['url']
  251. result["parse"] = 0
  252. result["playUrl"] = ''
  253. result["url"] = url
  254. result["header"] = {
  255. "Referer":"https://www.bilibili.com",
  256. "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"
  257. }
  258. result["contentType"] = 'video/x-flv'
  259. return result
  260. config = {
  261. "player": {},
  262. "filter": {}
  263. }
  264. header = {}
  265. def localProxy(self,param):
  266. return [200, "video/MP2T", action, ""]