小学教育.py 10 KB

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