py_bilibili.py 5.5 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 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. "Zard": "Zard",
  23. "玩具汽车": "玩具汽车",
  24. "儿童": "儿童",
  25. "幼儿": "幼儿",
  26. "儿童玩具": "儿童玩具",
  27. "轮滑": "轮滑",
  28. "蹦极": "蹦极",
  29. "美女": "美女",
  30. "宇宙": "宇宙",
  31. "天文": "天文",
  32. "飞碟探索": "飞碟探索",
  33. "BBC记录": "BBC记录",
  34. "Discovery": "Discovery",
  35. "地理": "地理",
  36. "历史": "历史",
  37. "纪实": "纪实",
  38. "人文": "人文",
  39. "考古": "考古",
  40. "自然": "自然",
  41. "航拍": "航拍",
  42. "昆虫": "昆虫",
  43. "动物世界": "动物世界",
  44. "荒野求生": "荒野求生",
  45. "大灾难": "大灾难",
  46. "纪录片": "纪录片",
  47. "皮划艇": "皮划艇",
  48. "冲浪": "冲浪",
  49. "赶海": "赶海",
  50. "钓鱼": "钓鱼",
  51. "户外": "户外",
  52. "斗牛": "斗牛",
  53. "摔角": "摔角",
  54. "武林风": "武林风",
  55. "太极": "太极",
  56. "美食": "美食",
  57. "旅游": "旅游",
  58. "相声小品": "相声小品",
  59. "赵本山": "赵本山",
  60. "宋小宝": "宋小宝",
  61. "文松": "文松",
  62. "戏曲": "戏曲",
  63. "搞笑": "搞笑",
  64. "快板": "快板",
  65. "评书": "评书",
  66. "广场舞": "广场舞",
  67. "mtv": "mtv",
  68. "健身": "健身",
  69. "DJ热播": "DJ热播",
  70. "演唱会": "演唱会",
  71. "张帝": "张帝",
  72. "张行": "张行",
  73. "刘文正": "刘文正",
  74. "张蔷": "张蔷",
  75. "吴涤清": "吴涤清",
  76. "刀郎": "刀郎",
  77. "陈淑桦": "陈淑桦",
  78. "足球": "足球",
  79. "篮球": "篮球",
  80. "排球": "排球",
  81. "乒乓球": "乒乓球",
  82. "曲棍球": "曲棍球",
  83. "橄榄球": "橄榄球",
  84. "高尔夫": "高尔夫",
  85. "台球": "台球",
  86. "斯诺克": "斯诺克",
  87. "畜牧": "畜牧",
  88. "养殖": "养殖",
  89. "水产": "水产",
  90. "假窗-白噪音": "窗+白噪音"
  91. }
  92. classes = []
  93. for k in cateManual:
  94. classes.append({
  95. 'type_name':k,
  96. 'type_id':cateManual[k]
  97. })
  98. result['class'] = classes
  99. if(filter):
  100. result['filters'] = self.config['filter']
  101. return result
  102. def homeVideoContent(self):
  103. result = {
  104. 'list':[]
  105. }
  106. return result
  107. cookies = ''
  108. def getCookie(self):
  109. rsp = self.fetch("https://www.bilibili.com/")
  110. self.cookies = rsp.cookies
  111. return rsp.cookies
  112. def categoryContent(self,tid,pg,filter,extend):
  113. result = {}
  114. url = 'https://api.bilibili.com/x/web-interface/search/type?search_type=video&keyword={0}&duration=4&page={1}'.format(tid,pg)
  115. if len(self.cookies) <= 0:
  116. self.getCookie()
  117. rsp = self.fetch(url,cookies=self.cookies)
  118. content = rsp.text
  119. jo = json.loads(content)
  120. if jo['code'] != 0:
  121. rspRetry = self.fetch(url,cookies=self.getCookie())
  122. content = rspRetry.text
  123. jo = json.loads(content)
  124. videos = []
  125. vodList = jo['data']['result']
  126. for vod in vodList:
  127. aid = str(vod['aid']).strip()
  128. title = vod['title'].strip().replace("<em class=\"keyword\">","").replace("</em>","")
  129. img = 'https:' + vod['pic'].strip()
  130. remark = str(vod['duration']).strip()
  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 cleanSpace(self,str):
  144. return str.replace('\n','').replace('\t','').replace('\r','').replace(' ','')
  145. def detailContent(self,array):
  146. aid = array[0]
  147. url = "https://api.bilibili.com/x/web-interface/view?aid={0}".format(aid)
  148. rsp = self.fetch(url,headers=self.header)
  149. jRoot = json.loads(rsp.text)
  150. jo = jRoot['data']
  151. title = jo['title'].replace("<em class=\"keyword\">","").replace("</em>","")
  152. pic = jo['pic']
  153. desc = jo['desc']
  154. typeName = jo['tname']
  155. vod = {
  156. "vod_id":aid,
  157. "vod_name":title,
  158. "vod_pic":pic,
  159. "type_name":typeName,
  160. "vod_year":"",
  161. "vod_area":"",
  162. "vod_remarks":"",
  163. "vod_actor":"",
  164. "vod_director":"",
  165. "vod_content":desc
  166. }
  167. ja = jo['pages']
  168. playUrl = ''
  169. for tmpJo in ja:
  170. cid = tmpJo['cid']
  171. part = tmpJo['part']
  172. playUrl = playUrl + '{0}${1}_{2}#'.format(part,aid,cid)
  173. vod['vod_play_from'] = 'B站'
  174. vod['vod_play_url'] = playUrl
  175. result = {
  176. 'list':[
  177. vod
  178. ]
  179. }
  180. return result
  181. def searchContent(self,key,quick):
  182. result = {
  183. 'list':[]
  184. }
  185. return result
  186. def playerContent(self,flag,id,vipFlags):
  187. # https://www.555dianying.cc/vodplay/static/js/playerconfig.js
  188. result = {}
  189. ids = id.split("_")
  190. url = 'https://api.bilibili.com:443/x/player/playurl?avid={0}&cid=%20%20{1}&qn=112'.format(ids[0],ids[1])
  191. rsp = self.fetch(url)
  192. jRoot = json.loads(rsp.text)
  193. jo = jRoot['data']
  194. ja = jo['durl']
  195. maxSize = -1
  196. position = -1
  197. for i in range(len(ja)):
  198. tmpJo = ja[i]
  199. if maxSize < int(tmpJo['size']):
  200. maxSize = int(tmpJo['size'])
  201. position = i
  202. url = ''
  203. if len(ja) > 0:
  204. if position == -1:
  205. position = 0
  206. url = ja[position]['url']
  207. result["parse"] = 0
  208. result["playUrl"] = ''
  209. result["url"] = url
  210. result["header"] = {
  211. "Referer":"https://www.bilibili.com",
  212. "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"
  213. }
  214. result["contentType"] = 'video/x-flv'
  215. return result
  216. config = {
  217. "player": {},
  218. "filter": {}
  219. }
  220. header = {}
  221. def localProxy(self,param):
  222. return [200, "video/MP2T", action, ""]