音乐合集.py 10 KB

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