py_幼儿教育1.py 4.9 KB

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