py_bilibili.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. def second_to_time(a):
  10. #将秒数转化为 时分秒的格式
  11. if a < 3600:
  12. return time.strftime("%M:%S", time.gmtime(a))
  13. else:
  14. return time.strftime("%H:%M:%S", time.gmtime(a))
  15. class Spider(Spider): # 元类 默认的元类 type
  16. def getName(self):
  17. return "哔哩哔哩"
  18. def init(self,extend=""):
  19. print("============{0}============".format(extend))
  20. pass
  21. def isVideoFormat(self,url):
  22. pass
  23. def manualVideoCheck(self):
  24. pass
  25. def homeContent(self,filter):
  26. result = {}
  27. cateManual = {
  28. "动态":"动态",
  29. '历史记录':'历史记录',
  30. "热门":"热门",
  31. "排行榜":"排行榜",
  32. "zane妈":"zane妈",
  33. "相声小品": "相声小品",
  34. "林芊妤":"林芊妤",
  35. "Zard": "Zard",
  36. "玩具汽车": "玩具汽车",
  37. "儿童": "儿童",
  38. "幼儿": "幼儿",
  39. "儿童玩具": "儿童玩具",
  40. "昆虫": "昆虫",
  41. "动物世界": "动物世界",
  42. "纪录片": "纪录片",
  43. "搞笑": "搞笑",
  44. "假窗-白噪音": "窗+白噪音",
  45. "演唱会": "演唱会"
  46. }
  47. classes = []
  48. for k in cateManual:
  49. classes.append({
  50. 'type_name':k,
  51. 'type_id':cateManual[k]
  52. })
  53. result['class'] = classes
  54. if(filter):
  55. result['filters'] = self.config['filter']
  56. return result
  57. def homeVideoContent(self):
  58. result = {
  59. 'list':[]
  60. }
  61. return result
  62. cookies = ''
  63. def getCookie(self):
  64. import requests
  65. import http.cookies
  66. #请在此输入 cookies
  67. raw_cookie_line = "buvid3=4EACEA62-1738-2499-7BD4-E14A8CF3062E54093infoc; b_nut=1667861954; i-wanna-go-back=-1; _uuid=10CBF8E88-88DF-2CA7-553B-1B9A285A3102158757infoc; buvid4=D8D5168E-DBA5-DBB3-D4EA-177D3940058059522-122110806-wmrIoPMDWijsLAkYRQhX/w%3D%3D; buvid_fp_plain=undefined; DedeUserID=429617357; DedeUserID__ckMd5=58f591cbe4b64558; rpdid=|()kYJ~m)Ru0J'uYY~)Yk|Yu; theme_style=light; nostalgia_conf=-1; b_lsid=4F946927_18457361FF1; b_ut=5; fingerprint=be3fab1635c4bfbf3cd92cc5a78721a8; SESSDATA=2a58ae02%2C1683463077%2C849da%2Ab2; bili_jct=ac2e50764bf43e4a5eadef19dcda2905; buvid_fp=2467d99d8522264426fa300d333a8f1e; sid=79wlt089; innersign=1; CURRENT_FNVAL=4048; CURRENT_QUALITY=112"
  68. simple_cookie = http.cookies.SimpleCookie(raw_cookie_line)
  69. cookie_jar = requests.cookies.RequestsCookieJar()
  70. cookie_jar.update(simple_cookie)
  71. return cookie_jar
  72. def get_dynamic(self,pg):
  73. result = {}
  74. if int(pg) > 1:
  75. return result
  76. offset = ''
  77. videos = []
  78. for i in range(0,10):
  79. url= 'https://api.bilibili.com/x/polymer/web-dynamic/v1/feed/all?timezone_offset=-480&type=all&page={0}&offset={1}'.format(pg,offset)
  80. rsp = self.fetch(url,cookies=self.getCookie())
  81. content = rsp.text
  82. jo = json.loads(content)
  83. if jo['code'] == 0:
  84. offset = jo['data']['offset']
  85. vodList = jo['data']['items']
  86. for vod in vodList:
  87. if vod['type'] == 'DYNAMIC_TYPE_AV':
  88. ivod = vod['modules']['module_dynamic']['major']['archive']
  89. aid = str(ivod['aid']).strip()
  90. title = ivod['title'].strip().replace("<em class=\"keyword\">","").replace("</em>","")
  91. img = ivod['cover'].strip()
  92. remark = str(ivod['duration_text']).strip()
  93. videos.append({
  94. "vod_id":aid,
  95. "vod_name":title,
  96. "vod_pic":img,
  97. "vod_remarks":remark
  98. })
  99. result['list'] = videos
  100. result['page'] = pg
  101. result['pagecount'] = 9999
  102. result['limit'] = 90
  103. result['total'] = 999999
  104. return result
  105. def get_history(self,pg):
  106. result = {}
  107. url = 'http://api.bilibili.com/x/v2/history?pn=%s' % pg
  108. rsp = self.fetch(url,cookies=self.getCookie())
  109. content = rsp.text
  110. jo = json.loads(content) #解析api接口,转化成json数据对象
  111. if jo['code'] == 0:
  112. videos = []
  113. vodList = jo['data']
  114. for vod in vodList:
  115. if vod['duration'] > 0: #筛选掉非视频的历史记录
  116. aid = str(vod["aid"]).strip() #获取 aid
  117. #获取标题
  118. title = vod["title"].replace("<em class=\"keyword\">", "").replace("</em>", "").replace("&quot;",
  119. '"')
  120. #封面图片
  121. img = vod["pic"].strip()
  122. #获取已观看时间
  123. if str(vod['progress'])=='-1':
  124. process=str(second_to_time(vod['duration'])).strip()
  125. else:
  126. process = str(second_to_time(vod['progress'])).strip()
  127. #获取视频总时长
  128. total_time= str(second_to_time(vod['duration'])).strip()
  129. #组合 已观看时间 / 总时长 ,赋值给 remark
  130. remark = process+' / '+total_time
  131. videos.append({
  132. "vod_id": aid,
  133. "vod_name": title,
  134. "vod_pic": img,
  135. "vod_remarks": remark
  136. })
  137. result['list'] = videos
  138. result['page'] = pg
  139. result['pagecount'] = 9999
  140. result['limit'] = 90
  141. result['total'] = 999999
  142. return result
  143. def get_hot(self,pg):
  144. result = {}
  145. url= 'https://api.bilibili.com/x/web-interface/popular?ps=20&pn={0}'.format(pg)
  146. rsp = self.fetch(url,cookies=self.getCookie())
  147. content = rsp.text
  148. jo = json.loads(content)
  149. if jo['code'] == 0:
  150. videos = []
  151. vodList = jo['data']['list']
  152. for vod in vodList:
  153. aid = str(vod['aid']).strip()
  154. title = vod['title'].strip().replace("<em class=\"keyword\">","").replace("</em>","")
  155. img = 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['list'] = videos
  164. result['page'] = pg
  165. result['pagecount'] = 9999
  166. result['limit'] = 90
  167. result['total'] = 999999
  168. return result
  169. def get_rank(self):
  170. result = {}
  171. url= 'https://api.bilibili.com/x/web-interface/ranking/v2?rid=0&type=all'
  172. rsp = self.fetch(url,cookies=self.getCookie())
  173. content = rsp.text
  174. jo = json.loads(content)
  175. if jo['code'] == 0:
  176. videos = []
  177. vodList = jo['data']['list']
  178. for vod in vodList:
  179. aid = str(vod['aid']).strip()
  180. title = vod['title'].strip().replace("<em class=\"keyword\">","").replace("</em>","")
  181. img = vod['pic'].strip()
  182. remark = str(vod['duration']).strip()
  183. videos.append({
  184. "vod_id":aid,
  185. "vod_name":title,
  186. "vod_pic":img,
  187. "vod_remarks":remark
  188. })
  189. result['list'] = videos
  190. result['page'] = 1
  191. result['pagecount'] = 1
  192. result['limit'] = 90
  193. result['total'] = 999999
  194. return result
  195. def categoryContent(self,tid,pg,filter,extend):
  196. result = {}
  197. if tid == "热门":
  198. return self.get_hot(pg=pg)
  199. if tid == "排行榜" :
  200. return self.get_rank()
  201. if tid == '动态':
  202. return self.get_dynamic(pg=pg)
  203. if tid == '历史记录':
  204. return self.get_history(pg=pg)
  205. url = 'https://api.bilibili.com/x/web-interface/search/type?search_type=video&keyword={0}&page={1}'.format(tid,pg)
  206. if len(self.cookies) <= 0:
  207. self.getCookie()
  208. rsp = self.fetch(url,cookies=self.getCookie())
  209. content = rsp.text
  210. jo = json.loads(content)
  211. if jo['code'] != 0:
  212. rspRetry = self.fetch(url,cookies=self.getCookie())
  213. content = rspRetry.text
  214. jo = json.loads(content)
  215. videos = []
  216. vodList = jo['data']['result']
  217. for vod in vodList:
  218. aid = str(vod['aid']).strip()
  219. title = tid + ":" + vod['title'].strip().replace("<em class=\"keyword\">","").replace("</em>","")
  220. img = 'https:' + vod['pic'].strip()
  221. remark = str(vod['duration']).strip()
  222. videos.append({
  223. "vod_id":aid,
  224. "vod_name":title,
  225. "vod_pic":img,
  226. "vod_remarks":remark
  227. })
  228. result['list'] = videos
  229. result['page'] = pg
  230. result['pagecount'] = 9999
  231. result['limit'] = 90
  232. result['total'] = 999999
  233. return result
  234. def cleanSpace(self,str):
  235. return str.replace('\n','').replace('\t','').replace('\r','').replace(' ','')
  236. def detailContent(self,array):
  237. aid = array[0]
  238. url = "https://api.bilibili.com/x/web-interface/view?aid={0}".format(aid)
  239. rsp = self.fetch(url,headers=self.header,cookies=self.getCookie())
  240. jRoot = json.loads(rsp.text)
  241. jo = jRoot['data']
  242. title = jo['title'].replace("<em class=\"keyword\">","").replace("</em>","")
  243. pic = jo['pic']
  244. desc = jo['desc']
  245. typeName = jo['tname']
  246. vod = {
  247. "vod_id":aid,
  248. "vod_name":title,
  249. "vod_pic":pic,
  250. "type_name":typeName,
  251. "vod_year":"",
  252. "vod_area":"bilidanmu",
  253. "vod_remarks":"",
  254. "vod_actor":jo['owner']['name'],
  255. "vod_director":jo['owner']['name'],
  256. "vod_content":desc
  257. }
  258. ja = jo['pages']
  259. playUrl = ''
  260. for tmpJo in ja:
  261. cid = tmpJo['cid']
  262. part = tmpJo['part']
  263. playUrl = playUrl + '{0}${1}_{2}#'.format(part,aid,cid)
  264. vod['vod_play_from'] = 'B站'
  265. vod['vod_play_url'] = playUrl
  266. result = {
  267. 'list':[
  268. vod
  269. ]
  270. }
  271. return result
  272. def searchContent(self,key,quick):
  273. search = self.categoryContent(tid=key,pg=1,filter=None,extend=None)
  274. result = {
  275. 'list':search['list']
  276. }
  277. return result
  278. def playerContent(self,flag,id,vipFlags):
  279. # https://www.555dianying.cc/vodplay/static/js/playerconfig.js
  280. result = {}
  281. ids = id.split("_")
  282. url = 'https://api.bilibili.com:443/x/player/playurl?avid={0}&cid=%20%20{1}&qn=112'.format(ids[0],ids[1])
  283. rsp = self.fetch(url,cookies=self.getCookie())
  284. jRoot = json.loads(rsp.text)
  285. jo = jRoot['data']
  286. ja = jo['durl']
  287. maxSize = -1
  288. position = -1
  289. for i in range(len(ja)):
  290. tmpJo = ja[i]
  291. if maxSize < int(tmpJo['size']):
  292. maxSize = int(tmpJo['size'])
  293. position = i
  294. url = ''
  295. if len(ja) > 0:
  296. if position == -1:
  297. position = 0
  298. url = ja[position]['url']
  299. result["parse"] = 0
  300. result["playUrl"] = ''
  301. result["url"] = url
  302. result["header"] = {
  303. "Referer":"https://www.bilibili.com",
  304. "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"
  305. }
  306. result["contentType"] = 'video/x-flv'
  307. return result
  308. config = {
  309. "player": {},
  310. "filter": {}
  311. }
  312. header = {}
  313. def localProxy(self,param):
  314. return [200, "video/MP2T", action, ""]