xiaopin.py 9.6 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 "相声小品"
  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. "文松":"文松",
  43. "开心麻花":"开心麻花",
  44. "屌丝男士":"屌丝男士",
  45. "喜剧综艺":"喜剧综艺"
  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 ="SESSDATA=7624af93%2C1696008331%2C862c8%2A42; bili_jct=141a474ef3ce8cf2fedf384e68f6625d; DedeUserID=3493271303096985; DedeUserID__ckMd5=212a836c164605b7"
  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, ""]