py_bilimy.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. box_video_type = ''
  13. def getDependence(self):
  14. return ['py_bilibili']
  15. def getName(self):
  16. return "我的哔哩"
  17. def init(self, extend=""):
  18. self.bilibili = extend[0]
  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. "cosplay": "cosplay",
  31. "周杰伦": "周杰伦",
  32. "狗狗": "汪星人",
  33. "猫咪": "喵星人",
  34. "请自定义关键词": "美女",
  35. # ————————以下可自定义UP主,冒号后须填写UID————————
  36. "虫哥说电影": "29296192",
  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. filters = {}
  47. for lk in cateManual:
  48. if not cateManual[lk].isdigit():
  49. link = cateManual[lk]
  50. filters.update({
  51. link: [{"key": "order", "name": "排序",
  52. "value": [{"n": "综合排序", "v": "totalrank"}, {"n": "最新发布", "v": "pubdate"},
  53. {"n": "最多点击", "v": "click"}, {"n": "最多收藏", "v": "stow"},
  54. {"n": "最多弹幕", "v": "dm"}, ]},
  55. {"key": "duration", "name": "时长",
  56. "value": [{"n": "全部", "v": "0"}, {"n": "60分钟以上", "v": "4"},
  57. {"n": "30~60分钟", "v": "3"}, {"n": "5~30分钟", "v": "2"},
  58. {"n": "5分钟以下", "v": "1"}]}]
  59. })
  60. result['filters'] = filters
  61. return result
  62. # 用户cookies,请在py_bilibili里填写,此处不用改
  63. cookies = ''
  64. def getCookie(self):
  65. self.cookies = self.bilibili.getCookie()
  66. return self.cookies
  67. def homeVideoContent(self):
  68. result = {}
  69. return result
  70. def get_up_videos(self, tid, pg):
  71. result = {}
  72. url = 'https://api.bilibili.com/x/space/arc/search?mid={0}&pn={1}&ps=20'.format(tid, pg)
  73. rsp = self.fetch(url, cookies=self.cookies)
  74. content = rsp.text
  75. jo = json.loads(content)
  76. if jo['code'] == 0:
  77. videos = []
  78. vodList = jo['data']['list']['vlist']
  79. for vod in vodList:
  80. aid = str(vod['aid']).strip()
  81. title = vod['title'].strip().replace("<em class=\"keyword\">", "").replace("</em>", "")
  82. img = vod['pic'].strip()
  83. remark = str(vod['length']).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 categoryContent(self, tid, pg, filter, extend):
  97. self.box_video_type = "分区"
  98. if tid.isdigit():
  99. return self.get_up_videos(tid, pg)
  100. else:
  101. result = self.bilibili.categoryContent(tid, pg, filter, extend)
  102. return result
  103. def cleanSpace(self, str):
  104. return str.replace('\n', '').replace('\t', '').replace('\r', '').replace(' ', '')
  105. def detailContent(self, array):
  106. if self.box_video_type == "搜索":
  107. mid = array[0]
  108. # 获取UP主视频列表,ps后面为视频数量,默认为20,加快加载速度
  109. url = 'https://api.bilibili.com/x/space/arc/search?mid={0}&pn=1&ps=20'.format(mid)
  110. rsp = self.fetch(url, headers=self.header)
  111. content = rsp.text
  112. jRoot = json.loads(content)
  113. jo = jRoot['data']['list']['vlist']
  114. url2 = "https://api.bilibili.com/x/web-interface/card?mid={0}".format(mid)
  115. rsp2 = self.fetch(url2, headers=self.header)
  116. jRoot2 = json.loads(rsp2.text)
  117. jo2 = jRoot2['data']['card']
  118. name = jo2['name'].replace("<em class=\"keyword\">", "").replace("</em>", "")
  119. pic = jo2['face']
  120. desc = jo2['Official']['desc'] + " " + jo2['Official']['title']
  121. vod = {
  122. "vod_id": mid,
  123. "vod_name": name + " " + "个人主页",
  124. "vod_pic": pic,
  125. "type_name": "最近投稿",
  126. "vod_year": "",
  127. "vod_area": "bilidanmu",
  128. "vod_remarks": "", # 不会显示
  129. 'vod_tags': 'mv', # 不会显示
  130. "vod_actor": "粉丝数:" + self.bilibili.zh(jo2['fans']),
  131. "vod_director": name,
  132. "vod_content": desc
  133. }
  134. playUrl = ''
  135. for tmpJo in jo:
  136. eid = tmpJo['aid']
  137. url3 = "https://api.bilibili.com/x/web-interface/view?aid=%s" % str(eid)
  138. rsp3 = self.fetch(url3)
  139. jRoot3 = json.loads(rsp3.text)
  140. cid = jRoot3['data']['cid']
  141. part = tmpJo['title'].replace("#", "-")
  142. playUrl = playUrl + '{0}${1}_{2}#'.format(part, eid, cid)
  143. vod['vod_play_from'] = 'B站'
  144. vod['vod_play_url'] = playUrl
  145. result = {
  146. 'list': [
  147. vod
  148. ]
  149. }
  150. return result
  151. else:
  152. return self.bilibili.detailContent(array)
  153. def searchContent(self, key, quick):
  154. self.box_video_type = "搜索"
  155. if len(self.cookies) <= 0:
  156. self.getCookie()
  157. url = 'https://api.bilibili.com/x/web-interface/search/type?search_type=bili_user&keyword={0}'.format(key)
  158. rsp = self.fetch(url, cookies=self.cookies)
  159. content = rsp.text
  160. jo = json.loads(content)
  161. videos = []
  162. vodList = jo['data']['result']
  163. for vod in vodList:
  164. aid = str(vod['mid']) # str(vod["res"][0]["aid"])
  165. title = "UP主:" + vod['uname'].strip() + " ☜" + key
  166. img = 'https:' + vod['upic'].strip()
  167. remark = "粉丝数" + self.bilibili.zh(vod['fans'])
  168. videos.append({
  169. "vod_id": aid,
  170. "vod_name": title,
  171. "vod_pic": img,
  172. "vod_remarks": remark
  173. })
  174. result = {
  175. 'list': videos
  176. }
  177. return result
  178. def playerContent(self, flag, id, vipFlags):
  179. return self.bilibili.playerContent(flag, id, vipFlags)
  180. config = {
  181. "player": {},
  182. "filter": {
  183. }
  184. }
  185. header = {
  186. "Referer": "https://www.bilibili.com",
  187. "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'
  188. }
  189. def localProxy(self, param):
  190. return [200, "video/MP2T", action, ""]