py_biliys.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. from requests import session, utils
  8. import os
  9. import time
  10. import base64
  11. class Spider(Spider):
  12. def getDependence(self):
  13. return ['py_bilibili']
  14. def getName(self):
  15. return "哔哩影视"
  16. def init(self, extend=""):
  17. self.bilibili = extend[0]
  18. print("============{0}============".format(extend))
  19. pass
  20. def isVideoFormat(self, url):
  21. pass
  22. def manualVideoCheck(self):
  23. pass
  24. # 主页
  25. def homeContent(self, filter):
  26. result = {}
  27. cateManual = {
  28. "番剧": "1",
  29. "国创": "4",
  30. "电影": "2",
  31. "电视剧": "5",
  32. "纪录片": "3",
  33. "综艺": "7",
  34. "全部": "全部",
  35. "时间表": "时间表",
  36. # ————————以下可自定义关键字,结果以影视类搜索展示————————
  37. # "喜羊羊": "喜羊羊"
  38. }
  39. classes = []
  40. for k in cateManual:
  41. classes.append({
  42. 'type_name': k,
  43. 'type_id': cateManual[k]
  44. })
  45. result['class'] = classes
  46. if (filter):
  47. result['filters'] = self.config['filter']
  48. return result
  49. # 用户cookies
  50. cookies = ''
  51. def getCookie(self):
  52. self.cookies = self.bilibili.getCookie()
  53. return self.cookies
  54. # 将超过10000的数字换成成以万和亿为单位
  55. def zh(self, num):
  56. if int(num) >= 100000000:
  57. p = round(float(num) / float(100000000), 1)
  58. p = str(p) + '亿'
  59. else:
  60. if int(num) >= 10000:
  61. p = round(float(num) / float(10000), 1)
  62. p = str(p) + '万'
  63. else:
  64. p = str(num)
  65. return p
  66. def homeVideoContent(self):
  67. result = {}
  68. videos = self.get_rank2(tid=4, pg=1)['list'][0:3]
  69. #videos = self.get_rank(tid=1, pg=1)['list'][0:5]
  70. #for i in [4, 2, 5, 3, 7]:
  71. # videos += self.get_rank2(tid=i, pg=1)['list'][0:5]
  72. result['list'] = videos
  73. return result
  74. def get_rank(self, tid, pg):
  75. ps=9
  76. pg_max= int(pg) * ps
  77. pg_min= pg_max - ps
  78. result = {}
  79. url = 'https://api.bilibili.com/pgc/web/rank/list?season_type={0}&day=3'.format(tid)
  80. rsp = self.fetch(url, headers=self.header, cookies=self.cookies)
  81. content = rsp.text
  82. jo = json.loads(content)
  83. if jo['code'] == 0:
  84. videos = []
  85. vodList = jo['result']['list']
  86. pc = int(len(vodList) / ps) + 1
  87. vodList = vodList[pg_min:pg_max]
  88. for vod in vodList:
  89. aid = str(vod['season_id']).strip()
  90. title = vod['title'].strip()
  91. img = vod['cover'].strip()
  92. remark = ''
  93. if 'index_show' in vod['new_ep']:
  94. remark = vod['new_ep']['index_show']
  95. videos.append({
  96. "vod_id": 'ss' + aid,
  97. "vod_name": title,
  98. "vod_pic": img + '@672w_378h_1c.jpg',
  99. "vod_remarks": remark
  100. })
  101. result['list'] = videos
  102. result['page'] = pg
  103. result['pagecount'] = pc
  104. result['limit'] = 2
  105. result['total'] = 999999
  106. return result
  107. def get_rank2(self, tid, pg):
  108. ps=9
  109. pg_max= int(pg) * ps
  110. pg_min= pg_max - ps
  111. result = {}
  112. url = 'https://api.bilibili.com/pgc/season/rank/web/list?season_type={0}&day=3'.format(tid)
  113. rsp = self.fetch(url, headers=self.header, cookies=self.cookies)
  114. content = rsp.text
  115. jo = json.loads(content)
  116. if jo['code'] == 0:
  117. videos = []
  118. vodList = jo['data']['list']
  119. pc = int(len(vodList) / ps) + 1
  120. vodList = vodList[pg_min:pg_max]
  121. for vod in vodList:
  122. aid = str(vod['season_id']).strip()
  123. title = vod['title'].strip()
  124. img = vod['cover'].strip()
  125. remark = ''
  126. if 'index_show' in vod['new_ep']:
  127. remark = vod['new_ep']['index_show']
  128. videos.append({
  129. "vod_id": 'ss' + aid,
  130. "vod_name": title,
  131. "vod_pic": img + '@672w_378h_1c.jpg',
  132. "vod_remarks": remark
  133. })
  134. result['list'] = videos
  135. result['page'] = pg
  136. result['pagecount'] = pc
  137. result['limit'] = 2
  138. result['total'] = 999999
  139. return result
  140. def get_all(self, tid, pg, order, season_status, extend):
  141. result = {}
  142. if len(self.cookies) <= 0:
  143. self.getCookie()
  144. url = 'https://api.bilibili.com/pgc/season/index/result?order={2}&pagesize=10&type=1&season_type={0}&page={1}&season_status={3}'.format(tid, pg, order, season_status)
  145. rsp = self.fetch(url, headers=self.header, cookies=self.cookies)
  146. content = rsp.text
  147. jo = json.loads(content)
  148. videos = []
  149. vodList = jo['data']['list']
  150. for vod in vodList:
  151. aid = str(vod['season_id']).strip()
  152. title = vod['title']
  153. img = vod['cover'].strip()
  154. remark = vod['index_show'].strip()
  155. videos.append({
  156. "vod_id": 'ss' + aid,
  157. "vod_name": title,
  158. "vod_pic": img + '@672w_378h_1c.jpg',
  159. "vod_remarks": remark
  160. })
  161. result['list'] = videos
  162. result['page'] = pg
  163. result['pagecount'] = 9999
  164. result['limit'] = 2
  165. result['total'] = 999999
  166. return result
  167. def get_timeline(self, tid, pg):
  168. result = {}
  169. url = 'https://api.bilibili.com/pgc/web/timeline/v2?season_type={0}&day_before=2&day_after=4'.format(tid)
  170. rsp = self.fetch(url, headers=self.header, cookies=self.cookies)
  171. content = rsp.text
  172. jo = json.loads(content)
  173. if jo['code'] == 0:
  174. videos1 = []
  175. vodList = jo['result']['latest']
  176. for vod in vodList:
  177. aid = str(vod['season_id']).strip()
  178. title = vod['title'].strip()
  179. img = vod['cover'].strip()
  180. remark = vod['pub_index'] + ' ' + vod['follows'].replace('系列', '')
  181. videos1.append({
  182. "vod_id": 'ss' + aid,
  183. "vod_name": title,
  184. "vod_pic": img + '@672w_378h_1c.jpg',
  185. "vod_remarks": remark
  186. })
  187. videos2 = []
  188. for i in range(0, 7):
  189. vodList = jo['result']['timeline'][i]['episodes']
  190. for vod in vodList:
  191. if str(vod['published']) == "0":
  192. aid = str(vod['season_id']).strip()
  193. title = str(vod['title']).strip()
  194. img = str(vod['cover']).strip()
  195. date = str(time.strftime("%m-%d %H:%M", time.localtime(vod['pub_ts'])))
  196. remark = date + " " + vod['pub_index']
  197. videos2.append({
  198. "vod_id": 'ss' + aid,
  199. "vod_name": title,
  200. "vod_pic": img + '@672w_378h_1c.jpg',
  201. "vod_remarks": remark
  202. })
  203. result['list'] = videos2 + videos1
  204. result['page'] = 1
  205. result['pagecount'] = 1
  206. result['limit'] = 90
  207. result['total'] = 999999
  208. return result
  209. def categoryContent(self, tid, pg, filter, extend):
  210. result = {}
  211. if len(self.cookies) <= 0:
  212. self.getCookie()
  213. if tid == "1":
  214. return self.get_rank(tid=tid, pg=pg)
  215. elif tid in {"2", "3", "4", "5", "7"}:
  216. return self.get_rank2(tid=tid, pg=pg)
  217. elif tid == "全部":
  218. tid = '1' # 全部界面默认展示最多播放的番剧
  219. order = '2'
  220. season_status = '-1'
  221. if 'tid' in extend:
  222. tid = extend['tid']
  223. if 'order' in extend:
  224. order = extend['order']
  225. if 'season_status' in extend:
  226. season_status = extend['season_status']
  227. return self.get_all(tid, pg, order, season_status, extend)
  228. elif tid == "时间表":
  229. tid = 1
  230. if 'tid' in extend:
  231. tid = extend['tid']
  232. return self.get_timeline(tid, pg)
  233. else:
  234. result = self.searchContent(key=tid, quick="false")
  235. return result
  236. def cleanSpace(self, str):
  237. return str.replace('\n', '').replace('\t', '').replace('\r', '').replace(' ', '')
  238. def detailContent(self, array):
  239. return self.bilibili.ysContent(array)
  240. def searchContent(self, key, quick):
  241. if len(self.cookies) <= 0:
  242. self.getCookie()
  243. url1 = 'https://api.bilibili.com/x/web-interface/search/type?search_type=media_bangumi&keyword={0}'.format(
  244. key) # 番剧搜索
  245. rsp1 = self.fetch(url1, headers=self.header, cookies=self.cookies)
  246. content1 = rsp1.text
  247. jo1 = json.loads(content1)
  248. rs1 = jo1['data']
  249. url2 = 'https://api.bilibili.com/x/web-interface/search/type?search_type=media_ft&keyword={0}'.format(
  250. key) # 影视搜索
  251. rsp2 = self.fetch(url2, headers=self.header, cookies=self.cookies)
  252. content2 = rsp2.text
  253. jo2 = json.loads(content2)
  254. rs2 = jo2['data']
  255. videos = []
  256. if rs1['numResults'] == 0:
  257. vodList = jo2['data']['result']
  258. elif rs2['numResults'] == 0:
  259. vodList = jo1['data']['result']
  260. else:
  261. vodList = jo1['data']['result'] + jo2['data']['result']
  262. for vod in vodList:
  263. aid = str(vod['season_id']).strip()
  264. title = key + '➢' + vod['title'].strip().replace("<em class=\"keyword\">", "").replace("</em>", "")
  265. img = vod['cover'].strip() # vod['eps'][0]['cover'].strip()原来的错误写法
  266. remark = vod['index_show']
  267. videos.append({
  268. "vod_id": 'ss' + aid,
  269. "vod_name": title,
  270. "vod_pic": img + '@672w_378h_1c.jpg',
  271. "vod_remarks": remark
  272. })
  273. result = {
  274. 'list': videos
  275. }
  276. return result
  277. def playerContent(self, flag, id, vipFlags):
  278. return self.bilibili.playerContent(flag, id, vipFlags)
  279. config = {
  280. "player": {},
  281. "filter": {
  282. "全部": [
  283. {
  284. "key": "tid",
  285. "name": "分类",
  286. "value": [{
  287. "n": "番剧",
  288. "v": "1"
  289. },
  290. {
  291. "n": "国创",
  292. "v": "4"
  293. },
  294. {
  295. "n": "电影",
  296. "v": "2"
  297. },
  298. {
  299. "n": "电视剧",
  300. "v": "5"
  301. },
  302. {
  303. "n": "记录片",
  304. "v": "3"
  305. },
  306. {
  307. "n": "综艺",
  308. "v": "7"
  309. }
  310. ]
  311. },
  312. {
  313. "key": "order",
  314. "name": "排序",
  315. "value": [
  316. {
  317. "n": "播放数量",
  318. "v": "2"
  319. },
  320. {
  321. "n": "更新时间",
  322. "v": "0"
  323. },
  324. {
  325. "n": "最高评分",
  326. "v": "4"
  327. },
  328. {
  329. "n": "弹幕数量",
  330. "v": "1"
  331. },
  332. {
  333. "n": "追看人数",
  334. "v": "3"
  335. },
  336. {
  337. "n": "开播时间",
  338. "v": "5"
  339. },
  340. {
  341. "n": "上映时间",
  342. "v": "6"
  343. },
  344. ]
  345. },
  346. {
  347. "key": "season_status",
  348. "name": "付费",
  349. "value": [
  350. {
  351. "n": "全部",
  352. "v": "-1"
  353. },
  354. {
  355. "n": "免费",
  356. "v": "1"
  357. },
  358. {
  359. "n": "付费",
  360. "v": "2%2C6"
  361. },
  362. {
  363. "n": "大会员",
  364. "v": "4%2C6"
  365. },
  366. ]
  367. },
  368. ],
  369. "时间表": [{
  370. "key": "tid",
  371. "name": "分类",
  372. "value": [
  373. {
  374. "n": "番剧",
  375. "v": "1"
  376. },
  377. {
  378. "n": "国创",
  379. "v": "4"
  380. },
  381. ]
  382. },
  383. ],
  384. }
  385. }
  386. header = {
  387. "Referer": "https://www.bilibili.com",
  388. "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
  389. }
  390. def localProxy(self, param):
  391. return [200, "video/MP2T", action, ""]