py_小学教育1.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. import requests
  10. class Spider(Spider): # 元类 默认的元类 type
  11. def getName(self):
  12. return "小学教育"
  13. def init(self,extend=""):
  14. print("============{0}============".format(extend))
  15. pass
  16. def isVideoFormat(self,url):
  17. pass
  18. def manualVideoCheck(self):
  19. pass
  20. def homeContent(self,filter):
  21. result = {}
  22. cateManual = {
  23. "1年级语文": "1年级语文",
  24. "1年级数学": "1年级数学",
  25. "1年级英语": "1年级英语",
  26. "2年级语文": "2年级语文",
  27. "2年级数学": "2年级数学",
  28. "2年级英语": "2年级英语",
  29. "3年级语文": "3年级语文",
  30. "3年级数学": "3年级数学",
  31. "3年级英语": "3年级英语",
  32. "4年级语文": "4年级语文",
  33. "4年级数学": "4年级数学",
  34. "4年级英语": "4年级英语",
  35. "5年级语文": "5年级语文",
  36. "5年级数学": "5年级数学",
  37. "5年级英语": "5年级英语",
  38. "6年级语文": "6年级语文",
  39. "6年级数学": "6年级数学",
  40. "6年级英语": "6年级英语"
  41. }
  42. classes = []
  43. for k in cateManual:
  44. classes.append({
  45. 'type_name':k,
  46. 'type_id':cateManual[k]
  47. })
  48. result['class'] = classes
  49. if(filter):
  50. result['filters'] = self.config['filter']
  51. return result
  52. def homeVideoContent(self):
  53. result = {
  54. 'list':[]
  55. }
  56. return result
  57. cookies = ''
  58. def getCookie(self):
  59. cookies_str = self.fetch("http://127.0.0.1:9978/file/tvbox/bilicookie.txt").text
  60. cookies_dic = dict([co.strip().split('=') for co in cookies_str.split(';')])
  61. rsp = requests.session()
  62. cookies_jar = requests.utils.cookiejar_from_dict(cookies_dic)
  63. rsp.cookies = cookies_jar
  64. content = self.fetch("http://api.bilibili.com/x/web-interface/nav", cookies=rsp.cookies)
  65. res = json.loads(content.text)
  66. if res["code"] == 0:
  67. self.cookies = rsp.cookies
  68. else:
  69. rsp = self.fetch("https://www.bilibili.com/")
  70. self.cookies = rsp.cookies
  71. return rsp.cookies
  72. def categoryContent(self,tid,pg,filter,extend):
  73. result = {}
  74. url = 'https://api.bilibili.com/x/web-interface/search/type?search_type=video&keyword={0}&duration=4&page={1}'.format(tid,pg)
  75. if len(self.cookies) <= 0:
  76. self.getCookie()
  77. rsp = self.fetch(url,cookies=self.cookies)
  78. content = rsp.text
  79. jo = json.loads(content)
  80. if jo['code'] != 0:
  81. rspRetry = self.fetch(url,cookies=self.getCookie())
  82. content = rspRetry.text
  83. jo = json.loads(content)
  84. videos = []
  85. vodList = jo['data']['result']
  86. for vod in vodList:
  87. aid = str(vod['aid']).strip()
  88. title = vod['title'].strip().replace("<em class=\"keyword\">","").replace("</em>","")
  89. img = 'https:' + vod['pic'].strip()
  90. remark = str(vod['duration']).strip()
  91. videos.append({
  92. "vod_id":aid,
  93. "vod_name":title,
  94. "vod_pic":img,
  95. "vod_remarks":remark
  96. })
  97. result['list'] = videos
  98. result['page'] = pg
  99. result['pagecount'] = 9999
  100. result['limit'] = 90
  101. result['total'] = 999999
  102. return result
  103. def cleanSpace(self,str):
  104. return str.replace('\n','').replace('\t','').replace('\r','').replace(' ','')
  105. def detailContent(self,array):
  106. aid = array[0]
  107. url = "https://api.bilibili.com/x/web-interface/view?aid={0}".format(aid)
  108. rsp = self.fetch(url,headers=self.header)
  109. jRoot = json.loads(rsp.text)
  110. jo = jRoot['data']
  111. title = jo['title'].replace("<em class=\"keyword\">","").replace("</em>","")
  112. pic = jo['pic']
  113. desc = jo['desc']
  114. typeName = jo['tname']
  115. vod = {
  116. "vod_id":aid,
  117. "vod_name":title,
  118. "vod_pic":pic,
  119. "type_name":typeName,
  120. "vod_year":"",
  121. "vod_area":"",
  122. "vod_remarks":"",
  123. "vod_actor":"",
  124. "vod_director":"",
  125. "vod_content":desc
  126. }
  127. ja = jo['pages']
  128. playUrl = ''
  129. for tmpJo in ja:
  130. cid = tmpJo['cid']
  131. part = tmpJo['part']
  132. playUrl = playUrl + '{0}${1}_{2}#'.format(part,aid,cid)
  133. vod['vod_play_from'] = 'B站'
  134. vod['vod_play_url'] = playUrl
  135. result = {
  136. 'list':[
  137. vod
  138. ]
  139. }
  140. return result
  141. def searchContent(self,key,quick):
  142. result = {
  143. 'list':[]
  144. }
  145. return result
  146. def playerContent(self,flag,id,vipFlags):
  147. # https://www.555dianying.cc/vodplay/static/js/playerconfig.js
  148. result = {}
  149. ids = id.split("_")
  150. url = 'https://api.bilibili.com:443/x/player/playurl?avid={0}&cid=%20%20{1}&qn=112'.format(ids[0],ids[1])
  151. rsp = self.fetch(url, cookies=self.cookies)
  152. jRoot = json.loads(rsp.text)
  153. jo = jRoot['data']
  154. ja = jo['durl']
  155. maxSize = -1
  156. position = -1
  157. for i in range(len(ja)):
  158. tmpJo = ja[i]
  159. if maxSize < int(tmpJo['size']):
  160. maxSize = int(tmpJo['size'])
  161. position = i
  162. url = ''
  163. if len(ja) > 0:
  164. if position == -1:
  165. position = 0
  166. url = ja[position]['url']
  167. result["parse"] = 0
  168. result["playUrl"] = ''
  169. result["url"] = url
  170. result["header"] = {
  171. "Referer":"https://www.bilibili.com",
  172. "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"
  173. }
  174. result["contentType"] = 'video/x-flv'
  175. return result
  176. config = {
  177. "player": {},
  178. "filter": {}
  179. }
  180. header = {}
  181. def localProxy(self,param):
  182. return [200, "video/MP2T", action, ""]