py_初中学习1.py 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. "7年级地理":"7年级地理",
  23. "7年级生物":"7年级生物",
  24. "7年级物理":"7年级物理",
  25. "7年级化学":"7年级化学",
  26. "8年级语文":"8年级语文",
  27. "8年级数学":"8年级数学",
  28. "8年级英语":"8年级英语",
  29. "8年级历史":"8年级历史",
  30. "8年级地理":"8年级地理",
  31. "8年级生物":"8年级生物",
  32. "8年级物理":"8年级物理",
  33. "8年级化学":"8年级化学",
  34. "9年级语文":"9年级语文",
  35. "9年级数学":"9年级数学",
  36. "9年级英语":"9年级英语",
  37. "9年级历史":"9年级历史",
  38. "9年级地理":"9年级地理",
  39. "9年级生物":"9年级生物",
  40. "9年级物理":"9年级物理",
  41. "9年级化学":"9年级化学"
  42. }
  43. classes = []
  44. for k in cateManual:
  45. classes.append({
  46. 'type_name':k,
  47. 'type_id':cateManual[k]
  48. })
  49. result['class'] = classes
  50. if(filter):
  51. result['filters'] = self.config['filter']
  52. return result
  53. def homeVideoContent(self):
  54. result = {
  55. 'list':[]
  56. }
  57. return result
  58. cookies = ''
  59. def getCookie(self):
  60. import requests
  61. import http.cookies
  62. # 这里填cookie
  63. raw_cookie_line = "buvid3=93456A95-EBF2-D2E9-8E7E-BF4CAEE28EBF15630infoc; b_nut=1678187414; buvid4=FECB5AD9-8C05-D3E6-2CA4-04D8F11C81E315630-123030719-/UB6HDpRwO7Y8xPozjrudA%3D%3D; bsource=search_baidu; _uuid=68786BC5-E3C10-1011D-277F-21E8E581086C481391infoc; buvid_fp_plain=undefined; fingerprint=dae430368fff18fc48ba52945bee0873; buvid_fp=dae430368fff18fc48ba52945bee0873; SESSDATA=31d18870%2C1693740532%2C4c963%2A31; bili_jct=4e7176ba803644d029a992524cf2b9d2; DedeUserID=1634385901; DedeUserID__ckMd5=a66f7f7df57f3a5a; sid=5yxkn9i3; b_lsid=DF626619_186BFF1D8CD; rpdid=|(l|JYkkY|0J'uY~)kl~ll~"
  64. simple_cookie = http.cookies.SimpleCookie(raw_cookie_line)
  65. cookie_jar = requests.cookies.RequestsCookieJar()
  66. cookie_jar.update(simple_cookie)
  67. return cookie_jar
  68. def get_dynamic(self,pg):
  69. result = {}
  70. url= 'https://api.bilibili.com/x/polymer/web-dynamic/v1/feed/all?timezone_offset=-480&type=all&page={0}'.format(pg)
  71. rsp = self.fetch(url,cookies=self.getCookie())
  72. content = rsp.text
  73. jo = json.loads(content)
  74. if jo['code'] == 0:
  75. videos = []
  76. vodList = jo['data']['items']
  77. for vod in vodList:
  78. if vod['type'] == 'DYNAMIC_TYPE_AV':
  79. ivod = vod['modules']['module_dynamic']['major']['archive']
  80. aid = str(ivod['aid']).strip()
  81. title = ivod['title'].strip().replace("<em class=\"keyword\">","").replace("</em>","")
  82. img = ivod['cover'].strip()
  83. remark = str(ivod['duration_text']).strip()
  84. videos.append({
  85. "vod_id":aid,
  86. "vod_name":title,
  87. "vod_pic":img,
  88. "vod_remarks":remark
  89. })
  90. result['list'] = videos
  91. result['page'] = pg
  92. result['pagecount'] = 9999
  93. result['limit'] = 90
  94. result['total'] = 999999
  95. return result
  96. def get_hot(self,pg):
  97. result = {}
  98. url= 'https://api.bilibili.com/x/web-interface/popular?ps=20&pn={0}'.format(pg)
  99. rsp = self.fetch(url,cookies=self.getCookie())
  100. content = rsp.text
  101. jo = json.loads(content)
  102. if jo['code'] == 0:
  103. videos = []
  104. vodList = jo['data']['list']
  105. for vod in vodList:
  106. aid = str(vod['aid']).strip()
  107. title = vod['title'].strip().replace("<em class=\"keyword\">","").replace("</em>","")
  108. img = vod['pic'].strip()
  109. remark = str(vod['duration']).strip()
  110. videos.append({
  111. "vod_id":aid,
  112. "vod_name":title,
  113. "vod_pic":img,
  114. "vod_remarks":remark
  115. })
  116. result['list'] = videos
  117. result['page'] = pg
  118. result['pagecount'] = 9999
  119. result['limit'] = 90
  120. result['total'] = 999999
  121. return result
  122. def get_rank(self):
  123. result = {}
  124. url= 'https://api.bilibili.com/x/web-interface/ranking/v2?rid=0&type=all'
  125. rsp = self.fetch(url,cookies=self.getCookie())
  126. content = rsp.text
  127. jo = json.loads(content)
  128. if jo['code'] == 0:
  129. videos = []
  130. vodList = jo['data']['list']
  131. for vod in vodList:
  132. aid = str(vod['aid']).strip()
  133. title = vod['title'].strip().replace("<em class=\"keyword\">","").replace("</em>","")
  134. img = vod['pic'].strip()
  135. remark = str(vod['duration']).strip()
  136. videos.append({
  137. "vod_id":aid,
  138. "vod_name":title,
  139. "vod_pic":img,
  140. "vod_remarks":remark
  141. })
  142. result['list'] = videos
  143. result['page'] = 1
  144. result['pagecount'] = 1
  145. result['limit'] = 90
  146. result['total'] = 999999
  147. return result
  148. def categoryContent(self,tid,pg,filter,extend):
  149. result = {}
  150. if tid == "热门":
  151. return self.get_hot(pg=pg)
  152. if tid == "排行榜" :
  153. return self.get_rank()
  154. if tid == '动态':
  155. return self.get_dynamic(pg=pg)
  156. url = 'https://api.bilibili.com/x/web-interface/search/type?search_type=video&keyword={0}&page={1}'.format(tid,pg)
  157. if len(self.cookies) <= 0:
  158. self.getCookie()
  159. rsp = self.fetch(url,cookies=self.getCookie())
  160. content = rsp.text
  161. jo = json.loads(content)
  162. if jo['code'] != 0:
  163. rspRetry = self.fetch(url,cookies=self.getCookie())
  164. content = rspRetry.text
  165. jo = json.loads(content)
  166. videos = []
  167. vodList = jo['data']['result']
  168. for vod in vodList:
  169. aid = str(vod['aid']).strip()
  170. title = tid + ":" + vod['title'].strip().replace("<em class=\"keyword\">","").replace("</em>","")
  171. img = 'https:' + vod['pic'].strip()
  172. remark = str(vod['duration']).strip()
  173. videos.append({
  174. "vod_id":aid,
  175. "vod_name":title,
  176. "vod_pic":img,
  177. "vod_remarks":remark
  178. })
  179. result['list'] = videos
  180. result['page'] = pg
  181. result['pagecount'] = 9999
  182. result['limit'] = 90
  183. result['total'] = 999999
  184. return result
  185. def cleanSpace(self,str):
  186. return str.replace('\n','').replace('\t','').replace('\r','').replace(' ','')
  187. def detailContent(self,array):
  188. aid = array[0]
  189. url = "https://api.bilibili.com/x/web-interface/view?aid={0}".format(aid)
  190. rsp = self.fetch(url,headers=self.header,cookies=self.getCookie())
  191. jRoot = json.loads(rsp.text)
  192. jo = jRoot['data']
  193. title = jo['title'].replace("<em class=\"keyword\">","").replace("</em>","")
  194. pic = jo['pic']
  195. desc = jo['desc']
  196. typeName = jo['tname']
  197. vod = {
  198. "vod_id":aid,
  199. "vod_name":title,
  200. "vod_pic":pic,
  201. "type_name":typeName,
  202. "vod_year":"",
  203. "vod_area":"bilidanmu",
  204. "vod_remarks":"",
  205. "vod_actor":jo['owner']['name'],
  206. "vod_director":jo['owner']['name'],
  207. "vod_content":desc
  208. }
  209. ja = jo['pages']
  210. playUrl = ''
  211. for tmpJo in ja:
  212. cid = tmpJo['cid']
  213. part = tmpJo['part']
  214. playUrl = playUrl + '{0}${1}_{2}#'.format(part,aid,cid)
  215. vod['vod_play_from'] = 'B站'
  216. vod['vod_play_url'] = playUrl
  217. result = {
  218. 'list':[
  219. vod
  220. ]
  221. }
  222. return result
  223. def searchContent(self,key,quick):
  224. search = self.categoryContent(tid=key,pg=1,filter=None,extend=None)
  225. result = {
  226. 'list':search['list']
  227. }
  228. return result
  229. def playerContent(self,flag,id,vipFlags):
  230. # https://www.555dianying.cc/vodplay/static/js/playerconfig.js
  231. result = {}
  232. ids = id.split("_")
  233. url = 'https://api.bilibili.com:443/x/player/playurl?avid={0}&cid=%20%20{1}&qn=112'.format(ids[0],ids[1])
  234. rsp = self.fetch(url,cookies=self.getCookie())
  235. jRoot = json.loads(rsp.text)
  236. jo = jRoot['data']
  237. ja = jo['durl']
  238. maxSize = -1
  239. position = -1
  240. for i in range(len(ja)):
  241. tmpJo = ja[i]
  242. if maxSize < int(tmpJo['size']):
  243. maxSize = int(tmpJo['size'])
  244. position = i
  245. url = ''
  246. if len(ja) > 0:
  247. if position == -1:
  248. position = 0
  249. url = ja[position]['url']
  250. result["parse"] = 0
  251. result["playUrl"] = ''
  252. result["url"] = url
  253. result["header"] = {
  254. "Referer":"https://www.bilibili.com",
  255. "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"
  256. }
  257. result["contentType"] = 'video/x-flv'
  258. return result
  259. config = {
  260. "player": {},
  261. "filter": {}
  262. }
  263. header = {}
  264. def localProxy(self,param):
  265. return [200, "video/MP2T", action, ""]