py_bilibili_baby.py 4.6 KB

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