py_bilibili.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. "演唱会": "演唱会"
  34. }
  35. classes = []
  36. for k in cateManual:
  37. classes.append({
  38. 'type_name':k,
  39. 'type_id':cateManual[k]
  40. })
  41. result['class'] = classes
  42. if(filter):
  43. result['filters'] = self.config['filter']
  44. return result
  45. def homeVideoContent(self):
  46. result = {
  47. 'list':[]
  48. }
  49. return result
  50. cookies = ''
  51. def getCookie(self):
  52. rsp = self.fetch("https://www.bilibili.com/")
  53. self.cookies = rsp.cookies
  54. return rsp.cookies
  55. def categoryContent(self,tid,pg,filter,extend):
  56. result = {}
  57. url = 'https://api.bilibili.com/x/web-interface/search/type?search_type=video&keyword={0}&duration=4&page={1}'.format(tid,pg)
  58. if len(self.cookies) <= 0:
  59. self.getCookie()
  60. rsp = self.fetch(url,cookies=self.cookies)
  61. content = rsp.text
  62. jo = json.loads(content)
  63. if jo['code'] != 0:
  64. rspRetry = self.fetch(url,cookies=self.getCookie())
  65. content = rspRetry.text
  66. jo = json.loads(content)
  67. videos = []
  68. vodList = jo['data']['result']
  69. for vod in vodList:
  70. aid = str(vod['aid']).strip()
  71. title = vod['title'].strip().replace("<em class=\"keyword\">","").replace("</em>","")
  72. img = 'https:' + vod['pic'].strip()
  73. remark = str(vod['duration']).strip()
  74. videos.append({
  75. "vod_id":aid,
  76. "vod_name":title,
  77. "vod_pic":img,
  78. "vod_remarks":remark
  79. })
  80. result['list'] = videos
  81. result['page'] = pg
  82. result['pagecount'] = 9999
  83. result['limit'] = 90
  84. result['total'] = 999999
  85. return result
  86. def cleanSpace(self,str):
  87. return str.replace('\n','').replace('\t','').replace('\r','').replace(' ','')
  88. def detailContent(self,array):
  89. aid = array[0]
  90. url = "https://api.bilibili.com/x/web-interface/view?aid={0}".format(aid)
  91. rsp = self.fetch(url,headers=self.header)
  92. jRoot = json.loads(rsp.text)
  93. jo = jRoot['data']
  94. title = jo['title'].replace("<em class=\"keyword\">","").replace("</em>","")
  95. pic = jo['pic']
  96. desc = jo['desc']
  97. typeName = jo['tname']
  98. vod = {
  99. "vod_id":aid,
  100. "vod_name":title,
  101. "vod_pic":pic,
  102. "type_name":typeName,
  103. "vod_year":"",
  104. "vod_area":"",
  105. "vod_remarks":"",
  106. "vod_actor":"",
  107. "vod_director":"",
  108. "vod_content":desc
  109. }
  110. ja = jo['pages']
  111. playUrl = ''
  112. for tmpJo in ja:
  113. cid = tmpJo['cid']
  114. part = tmpJo['part']
  115. playUrl = playUrl + '{0}${1}_{2}#'.format(part,aid,cid)
  116. vod['vod_play_from'] = 'B站'
  117. vod['vod_play_url'] = playUrl
  118. result = {
  119. 'list':[
  120. vod
  121. ]
  122. }
  123. return result
  124. def searchContent(self,key,quick):
  125. result = {
  126. 'list':[]
  127. }
  128. return result
  129. def playerContent(self,flag,id,vipFlags):
  130. # https://www.555dianying.cc/vodplay/static/js/playerconfig.js
  131. result = {}
  132. ids = id.split("_")
  133. url = 'https://api.bilibili.com:443/x/player/playurl?avid={0}&cid=%20%20{1}&qn=112'.format(ids[0],ids[1])
  134. rsp = self.fetch(url)
  135. jRoot = json.loads(rsp.text)
  136. jo = jRoot['data']
  137. ja = jo['durl']
  138. maxSize = -1
  139. position = -1
  140. for i in range(len(ja)):
  141. tmpJo = ja[i]
  142. if maxSize < int(tmpJo['size']):
  143. maxSize = int(tmpJo['size'])
  144. position = i
  145. url = ''
  146. if len(ja) > 0:
  147. if position == -1:
  148. position = 0
  149. url = ja[position]['url']
  150. result["parse"] = 0
  151. result["playUrl"] = ''
  152. result["url"] = url
  153. result["header"] = {
  154. "Referer":"https://www.bilibili.com",
  155. "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"
  156. }
  157. result["contentType"] = 'video/x-flv'
  158. return result
  159. config = {
  160. "player": {},
  161. "filter": {}
  162. }
  163. header = {}
  164. def localProxy(self,param):
  165. return [200, "video/MP2T", action, ""]