yinyue.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 "B站音乐"
  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. "2022年热榜":"2022年热们歌曲",
  25. "经典老歌":"经典老歌",
  26. "古风歌曲":"古风歌曲",
  27. "闽南语歌曲":"闽南语歌曲",
  28. "印度歌舞":"印度歌舞",
  29. "8K":"8K",
  30. "4K杜比视界":"4K杜比视界",
  31. "4K":"4K",
  32. "黑胶":"黑胶",
  33. "MV":"MV",
  34. "4K小姐姐":"4K小姐姐",
  35. "超清小姐姐":"超清小姐姐",
  36. "经典粤语":"经典粤语",
  37. "经典老歌":"经典老歌",
  38. "宝华音乐台":"宝华音乐台",
  39. "翻唱":"翻唱",
  40. "张学友":"张学友",
  41. "伍佰":"伍佰",
  42. "陈奕迅":"陈奕迅",
  43. "演唱会":"演唱会",
  44. "欧美金曲":"欧美金曲",
  45. "日韩MV":"日韩MV",
  46. "DJ":"DJ"
  47. }
  48. classes = []
  49. for k in cateManual:
  50. classes.append({
  51. 'type_name':k,
  52. 'type_id':cateManual[k]
  53. })
  54. result['class'] = classes
  55. if(filter):
  56. result['filters'] = self.config['filter']
  57. return result
  58. def homeVideoContent(self):
  59. result = {
  60. 'list':[]
  61. }
  62. return result
  63. cookies = ''
  64. def getCookie(self):
  65. import requests
  66. import http.cookies
  67. # 这里填cookie
  68. raw_cookie_line ="buvid3=8B57D3BA-607A-1E85-018A-E8C430023CED42659infoc; b_lsid=BEB8EE7F_18742FF8C2E; bsource=search_baidu; _uuid=DE810E367-B52C-AF6E-A612-EDF4C31567F358591infoc; b_nut=100; buvid_fp=711a632b5c876fa8bbcf668c1efba551; SESSDATA=7624af93%2C1696008331%2C862c8%2A42; bili_jct=141a474ef3ce8cf2fedf384e68f6625d; DedeUserID=3493271303096985; DedeUserID__ckMd5=212a836c164605b7; sid=5h4ruv6o; buvid4=978E9208-13DA-F87A-3DC0-0B8EDF46E80434329-123040301-dWliG5BMrUb70r3g583u7w%3D%3D"
  69. simple_cookie = http.cookies.SimpleCookie(raw_cookie_line)
  70. cookie_jar = requests.cookies.RequestsCookieJar()
  71. cookie_jar.update(simple_cookie)
  72. return cookie_jar
  73. def get_dynamic(self,pg):
  74. result = {}
  75. url= 'https://api.bilibili.com/x/polymer/web-dynamic/v1/feed/all?timezone_offset=-480&type=all&page={0}'.format(pg)
  76. rsp = self.fetch(url,cookies=self.getCookie())
  77. content = rsp.text
  78. jo = json.loads(content)
  79. if jo['code'] == 0:
  80. videos = []
  81. vodList = jo['data']['items']
  82. for vod in vodList:
  83. if vod['type'] == 'DYNAMIC_TYPE_AV':
  84. ivod = vod['modules']['module_dynamic']['major']['archive']
  85. aid = str(ivod['aid']).strip()
  86. title = ivod['title'].strip().replace("<em class=\"keyword\">","").replace("</em>","")
  87. img = ivod['cover'].strip()
  88. remark = str(ivod['duration_text']).strip()
  89. videos.append({
  90. "vod_id":aid,
  91. "vod_name":title,
  92. "vod_pic":img,
  93. "vod_remarks":remark
  94. })
  95. result['list'] = videos
  96. result['page'] = pg
  97. result['pagecount'] = 9999
  98. result['limit'] = 90
  99. result['total'] = 999999
  100. return result
  101. def get_hot(self,pg):
  102. result = {}
  103. url= 'https://api.bilibili.com/x/web-interface/popular?ps=20&pn={0}'.format(pg)
  104. rsp = self.fetch(url,cookies=self.getCookie())
  105. content = rsp.text
  106. jo = json.loads(content)
  107. if jo['code'] == 0:
  108. videos = []
  109. vodList = jo['data']['list']
  110. for vod in vodList:
  111. aid = str(vod['aid']).strip()
  112. title = vod['title'].strip().replace("<em class=\"keyword\">","").replace("</em>","")
  113. img = vod['pic'].strip()
  114. remark = str(vod['duration']).strip()
  115. videos.append({
  116. "vod_id":aid,
  117. "vod_name":title,
  118. "vod_pic":img,
  119. "vod_remarks":remark
  120. })
  121. result['list'] = videos
  122. result['page'] = pg
  123. result['pagecount'] = 9999
  124. result['limit'] = 90
  125. result['total'] = 999999
  126. return result
  127. def get_rank(self):
  128. result = {}
  129. url= 'https://api.bilibili.com/x/web-interface/ranking/v2?rid=0&type=all'
  130. rsp = self.fetch(url,cookies=self.getCookie())
  131. content = rsp.text
  132. jo = json.loads(content)
  133. if jo['code'] == 0:
  134. videos = []
  135. vodList = jo['data']['list']
  136. for vod in vodList:
  137. aid = str(vod['aid']).strip()
  138. title = vod['title'].strip().replace("<em class=\"keyword\">","").replace("</em>","")
  139. img = vod['pic'].strip()
  140. remark = str(vod['duration']).strip()
  141. videos.append({
  142. "vod_id":aid,
  143. "vod_name":title,
  144. "vod_pic":img,
  145. "vod_remarks":remark
  146. })
  147. result['list'] = videos
  148. result['page'] = 1
  149. result['pagecount'] = 1
  150. result['limit'] = 90
  151. result['total'] = 999999
  152. return result
  153. def categoryContent(self,tid,pg,filter,extend):
  154. result = {}
  155. if tid == "热门":
  156. return self.get_hot(pg=pg)
  157. if tid == "排行榜" :
  158. return self.get_rank()
  159. if tid == '动态':
  160. return self.get_dynamic(pg=pg)
  161. url = 'https://api.bilibili.com/x/web-interface/search/type?search_type=video&keyword={0}&page={1}'.format(tid,pg)
  162. if len(self.cookies) <= 0:
  163. self.getCookie()
  164. rsp = self.fetch(url,cookies=self.getCookie())
  165. content = rsp.text
  166. jo = json.loads(content)
  167. if jo['code'] != 0:
  168. rspRetry = self.fetch(url,cookies=self.getCookie())
  169. content = rspRetry.text
  170. jo = json.loads(content)
  171. videos = []
  172. vodList = jo['data']['result']
  173. for vod in vodList:
  174. aid = str(vod['aid']).strip()
  175. title = tid + ":" + vod['title'].strip().replace("<em class=\"keyword\">","").replace("</em>","")
  176. img = 'https:' + vod['pic'].strip()
  177. remark = str(vod['duration']).strip()
  178. videos.append({
  179. "vod_id":aid,
  180. "vod_name":title,
  181. "vod_pic":img,
  182. "vod_remarks":remark
  183. })
  184. result['list'] = videos
  185. result['page'] = pg
  186. result['pagecount'] = 9999
  187. result['limit'] = 90
  188. result['total'] = 999999
  189. return result
  190. def cleanSpace(self,str):
  191. return str.replace('\n','').replace('\t','').replace('\r','').replace(' ','')
  192. def detailContent(self,array):
  193. aid = array[0]
  194. url = "https://api.bilibili.com/x/web-interface/view?aid={0}".format(aid)
  195. rsp = self.fetch(url,headers=self.header,cookies=self.getCookie())
  196. jRoot = json.loads(rsp.text)
  197. jo = jRoot['data']
  198. title = jo['title'].replace("<em class=\"keyword\">","").replace("</em>","")
  199. pic = jo['pic']
  200. desc = jo['desc']
  201. typeName = jo['tname']
  202. vod = {
  203. "vod_id":aid,
  204. "vod_name":title,
  205. "vod_pic":pic,
  206. "type_name":typeName,
  207. "vod_year":"",
  208. "vod_area":"bilidanmu",
  209. "vod_remarks":"",
  210. "vod_actor":jo['owner']['name'],
  211. "vod_director":jo['owner']['name'],
  212. "vod_content":desc
  213. }
  214. ja = jo['pages']
  215. playUrl = ''
  216. for tmpJo in ja:
  217. cid = tmpJo['cid']
  218. part = tmpJo['part']
  219. playUrl = playUrl + '{0}${1}_{2}#'.format(part,aid,cid)
  220. vod['vod_play_from'] = 'B站'
  221. vod['vod_play_url'] = playUrl
  222. result = {
  223. 'list':[
  224. vod
  225. ]
  226. }
  227. return result
  228. def searchContent(self,key,quick):
  229. search = self.categoryContent(tid=key,pg=1,filter=None,extend=None)
  230. result = {
  231. 'list':search['list']
  232. }
  233. return result
  234. def playerContent(self,flag,id,vipFlags):
  235. # https://www.555dianying.cc/vodplay/static/js/playerconfig.js
  236. result = {}
  237. ids = id.split("_")
  238. url = 'https://api.bilibili.com:443/x/player/playurl?avid={0}&cid=%20%20{1}&qn=112'.format(ids[0],ids[1])
  239. rsp = self.fetch(url,cookies=self.getCookie())
  240. jRoot = json.loads(rsp.text)
  241. jo = jRoot['data']
  242. ja = jo['durl']
  243. maxSize = -1
  244. position = -1
  245. for i in range(len(ja)):
  246. tmpJo = ja[i]
  247. if maxSize < int(tmpJo['size']):
  248. maxSize = int(tmpJo['size'])
  249. position = i
  250. url = ''
  251. if len(ja) > 0:
  252. if position == -1:
  253. position = 0
  254. url = ja[position]['url']
  255. result["parse"] = 0
  256. result["playUrl"] = ''
  257. result["url"] = url
  258. result["header"] = {
  259. "Referer":"https://www.bilibili.com",
  260. "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"
  261. }
  262. result["contentType"] = 'video/x-flv'
  263. return result
  264. config = {
  265. "player": {},
  266. "filter": {}
  267. }
  268. header = {}
  269. def localProxy(self,param):
  270. return [200, "video/MP2T", action, ""]