cctv_spider_ttttt.py 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. # coding=utf-8
  2. # !/usr/bin/python
  3. import os.path
  4. import sys
  5. sys.path.append('..')
  6. try:
  7. from base.spider import Spider as BaseSpider
  8. except ImportError:
  9. from t4.base.spider import BaseSpider
  10. import json
  11. import time
  12. import base64
  13. import re
  14. from urllib import request, parse
  15. from pathlib import Path
  16. import urllib
  17. import urllib.request
  18. """
  19. 配置示例:
  20. t4的配置里ext节点会自动变成api对应query参数extend,但t4的ext字符串不支持路径格式,比如./开头或者.json结尾
  21. api里会自动含有ext参数是base64编码后的选中的筛选条件
  22. 错误示例,ext含有json:
  23. {
  24. "key":"hipy_cctv",
  25. "name":"hipy_cctv",
  26. "type":4,
  27. "api":"http://192.168.31.49:5707/api/v1/vod/cctv_spider?api_ext={{host}}/txt/hipy/cctv_spider.json",
  28. "searchable":1,
  29. "quickSearch":1,
  30. "filterable":1,
  31. "ext":"cctv_spider.json"
  32. }
  33. 正确示例。同时存在ext和api_ext会优先取ext作为extend加载init
  34. {
  35. "key":"hipy_t4_cctv",
  36. "name":"cctv(hipy_t4)",
  37. "type":4,
  38. "api":"http://192.168.31.49:5707/api/v1/vod/cctv_spider?api_ext={{host}}/txt/hipy/cctv_spider.json",
  39. "searchable":1,
  40. "quickSearch":1,
  41. "filterable":1,
  42. "ext":"cctv_spider"
  43. }
  44. {
  45. "key": "hipy_t3_cctv",
  46. "name": "cctv(hipy_t3)",
  47. "type": 3,
  48. "api": "{{host}}/txt/hipy/cctv_spider.py",
  49. "searchable": 1,
  50. "quickSearch": 1,
  51. "filterable": 1,
  52. "ext": "{{host}}/txt/hipy/cctv_spider.json"
  53. }
  54. """
  55. class Spider(BaseSpider): # 元类 默认的元类 type
  56. module = None
  57. def getDependence(self):
  58. return ['base_spider']
  59. def getName(self):
  60. return "中央电视台" # 可搜索
  61. def init_api_ext_file(self):
  62. ext_file = __file__.replace('.py', '.json')
  63. print(f'ext_file:{ext_file}')
  64. # 特别节目网页: https://tv.cctv.com/yxg/index.shtml?spm=C28340.PlFTqGe6Zk8M.E2PQtIunpEaz.65
  65. # 特别节目分类筛选获取页面: https://tv.cctv.com/yxg/tbjm/index.shtml
  66. # 纪录片网页: https://tv.cctv.com/yxg/index.shtml?spm=C28340.PlFTqGe6Zk8M.E2PQtIunpEaz.65
  67. # 纪录片分类筛选获取页面:https://tv.cctv.com/yxg/jlp/index.shtml
  68. # ==================== 获取特别节目的筛选条件 ======================
  69. r = self.fetch('https://tv.cctv.com/yxg/tbjm/index.shtml')
  70. html = r.text
  71. html = self.html(html)
  72. filter_tbjm = []
  73. lis = html.xpath('//*[@id="pindao"]/li')
  74. li_value = []
  75. for li in lis:
  76. li_value.append({
  77. 'n': ''.join(li.xpath('./span//text()')),
  78. 'v': ''.join(li.xpath('@datacd')),
  79. })
  80. # print(li_value)
  81. filter_tbjm.append({
  82. "key": "datapd-channel",
  83. "name": "频道",
  84. "value": li_value
  85. })
  86. lis = html.xpath('//*[@id="fenlei"]/li')
  87. li_value = []
  88. for li in lis:
  89. li_value.append({
  90. 'n': ''.join(li.xpath('./span//text()')),
  91. 'v': ''.join(li.xpath('@datalx')),
  92. })
  93. # print(li_value)
  94. filter_tbjm.append({
  95. "key": "datafl-sc",
  96. "name": "类型",
  97. "value": li_value
  98. })
  99. lis = html.xpath('//*[@id="zimu"]/li')
  100. li_value = []
  101. for li in lis:
  102. li_value.append({
  103. 'n': ''.join(li.xpath('./span//text()')),
  104. 'v': ''.join(li.xpath('@datazm')),
  105. })
  106. # print(li_value)
  107. filter_tbjm.append({
  108. "key": "dataszm-letter",
  109. "name": "首字母",
  110. "value": li_value
  111. })
  112. print(filter_tbjm)
  113. # ==================== 纪录片筛选获取 ======================
  114. r = self.fetch('https://tv.cctv.com/yxg/jlp/index.shtml')
  115. html = r.text
  116. html = self.html(html)
  117. filter_jlp = []
  118. lis = html.xpath('//*[@id="pindao"]/li')
  119. li_value = []
  120. for li in lis:
  121. li_value.append({
  122. 'n': ''.join(li.xpath('./span//text()')),
  123. 'v': ''.join(li.xpath('@datacd')),
  124. })
  125. # print(li_value)
  126. filter_jlp.append({
  127. "key": "datapd-channel",
  128. "name": "频道",
  129. "value": li_value
  130. })
  131. lis = html.xpath('//*[@id="fenlei"]/li')
  132. li_value = []
  133. for li in lis:
  134. li_value.append({
  135. 'n': ''.join(li.xpath('./span//text()')),
  136. 'v': ''.join(li.xpath('@datalx')),
  137. })
  138. # print(li_value)
  139. filter_jlp.append({
  140. "key": "datafl-sc",
  141. "name": "类型",
  142. "value": li_value
  143. })
  144. lis = html.xpath('//*[@id="nianfen"]/li')
  145. li_value = []
  146. for li in lis:
  147. li_value.append({
  148. 'n': ''.join(li.xpath('./span//text()')),
  149. 'v': ''.join(li.xpath('@datanf')),
  150. })
  151. # print(li_value)
  152. filter_jlp.append({
  153. "key": "datanf-year",
  154. "name": "年份",
  155. "value": li_value
  156. })
  157. lis = html.xpath('//*[@id="zimu"]/li')
  158. li_value = []
  159. for li in lis:
  160. li_value.append({
  161. 'n': ''.join(li.xpath('./span//text()')),
  162. 'v': ''.join(li.xpath('@datazm')),
  163. })
  164. # print(li_value)
  165. filter_jlp.append({
  166. "key": "dataszm-letter",
  167. "name": "首字母",
  168. "value": li_value
  169. })
  170. print(filter_jlp)
  171. ext_file_dict = {
  172. "特别节目": filter_tbjm,
  173. "纪录片": filter_jlp,
  174. }
  175. # print(json.dumps(ext_file_dict,ensure_ascii=False,indent=4))
  176. with open(ext_file, mode='w+', encoding='utf-8') as f:
  177. # f.write(json.dumps(ext_file_dict,ensure_ascii=False,indent=4))
  178. f.write(json.dumps(ext_file_dict, ensure_ascii=False))
  179. def init(self, extend=""):
  180. def init_file(ext_file):
  181. ext_file = Path(ext_file).as_posix()
  182. # print(f'ext_file:{ext_file}')
  183. if os.path.exists(ext_file):
  184. # print('存在扩展文件')
  185. with open(ext_file, mode='r', encoding='utf-8') as f:
  186. try:
  187. ext_dict = json.loads(f.read())
  188. # print(ext_dict)
  189. self.config['filter'].update(ext_dict)
  190. except Exception as e:
  191. print(f'更新扩展筛选条件发生错误:{e}')
  192. print("============{0}============".format(extend))
  193. if isinstance(extend, str):
  194. if extend.startswith('./'):
  195. ext_file = os.path.join(os.path.dirname(__file__), extend)
  196. init_file(ext_file)
  197. elif extend.startswith('http'):
  198. try:
  199. r = self.fetch(extend)
  200. self.config['filter'].update(r.json())
  201. except Exception as e:
  202. print(f'更新扩展筛选条件发生错误:{e}')
  203. elif extend and not extend.startswith('./') and not extend.startswith('http'):
  204. ext_file = os.path.join(os.path.dirname(__file__), './' + extend + '.json')
  205. init_file(ext_file)
  206. elif isinstance(extend, list):
  207. for lib in extend:
  208. if '.Spider' in str(type(lib)):
  209. self.module = lib
  210. break
  211. def isVideoFormat(self, url):
  212. pass
  213. def manualVideoCheck(self):
  214. pass
  215. def homeContent(self, filter):
  216. result = {}
  217. cateManual = {
  218. "栏目大全": "栏目大全",
  219. "特别节目": "特别节目",
  220. "纪录片": "纪录片",
  221. "电视剧": "电视剧",
  222. "动画片": "动画片"
  223. }
  224. classes = []
  225. for k in cateManual:
  226. classes.append({
  227. 'type_name': k,
  228. 'type_id': cateManual[k]
  229. })
  230. result['class'] = classes
  231. if (filter):
  232. result['filters'] = self.config['filter']
  233. return result
  234. def homeVideoContent(self):
  235. result = {
  236. 'list': []
  237. }
  238. if self.module:
  239. result = self.module.homeVideoContent()
  240. return result
  241. def categoryContent(self, tid, pg, filter, extend):
  242. result = {}
  243. month = "" # 月
  244. year = "" # 年
  245. area = '' # 地区
  246. channel = '' # 频道
  247. datafl = '' # 类型
  248. letter = '' # 字母
  249. pagecount = 24
  250. if tid == '动画片':
  251. id = urllib.parse.quote(tid)
  252. if 'datadq-area' in extend.keys():
  253. area = urllib.parse.quote(extend['datadq-area'])
  254. if 'dataszm-letter' in extend.keys():
  255. letter = extend['dataszm-letter']
  256. if 'datafl-sc' in extend.keys():
  257. datafl = urllib.parse.quote(extend['datafl-sc'])
  258. url = 'https://api.cntv.cn/list/getVideoAlbumList?channelid=CHAL1460955899450127&area={0}&sc={4}&fc={1}&letter={2}&p={3}&n=24&serviceId=tvcctv&topv=1&t=json'.format(
  259. area, id, letter, pg, datafl)
  260. elif tid == '纪录片':
  261. id = urllib.parse.quote(tid)
  262. if 'datapd-channel' in extend.keys():
  263. channel = urllib.parse.quote(extend['datapd-channel'])
  264. if 'datafl-sc' in extend.keys():
  265. datafl = urllib.parse.quote(extend['datafl-sc'])
  266. if 'datanf-year' in extend.keys():
  267. year = extend['datanf-year']
  268. if 'dataszm-letter' in extend.keys():
  269. letter = extend['dataszm-letter']
  270. url = 'https://api.cntv.cn/list/getVideoAlbumList?channelid=CHAL1460955924871139&fc={0}&channel={1}&sc={2}&year={3}&letter={4}&p={5}&n=24&serviceId=tvcctv&topv=1&t=json'.format(
  271. id, channel, datafl, year, letter, pg)
  272. elif tid == '电视剧':
  273. id = urllib.parse.quote(tid)
  274. if 'datafl-sc' in extend.keys():
  275. datafl = urllib.parse.quote(extend['datafl-sc'])
  276. if 'datanf-year' in extend.keys():
  277. year = extend['datanf-year']
  278. if 'dataszm-letter' in extend.keys():
  279. letter = extend['dataszm-letter']
  280. url = 'https://api.cntv.cn/list/getVideoAlbumList?channelid=CHAL1460955853485115&area={0}&sc={1}&fc={2}&year={3}&letter={4}&p={5}&n=24&serviceId=tvcctv&topv=1&t=json'.format(
  281. area, datafl, id, year, letter, pg)
  282. elif tid == '特别节目':
  283. id = urllib.parse.quote(tid)
  284. if 'datapd-channel' in extend.keys():
  285. channel = urllib.parse.quote(extend['datapd-channel'])
  286. if 'datafl-sc' in extend.keys():
  287. datafl = urllib.parse.quote(extend['datafl-sc'])
  288. if 'dataszm-letter' in extend.keys():
  289. letter = extend['dataszm-letter']
  290. url = 'https://api.cntv.cn/list/getVideoAlbumList?channelid=CHAL1460955953877151&channel={0}&sc={1}&fc={2}&bigday=&letter={3}&p={4}&n=24&serviceId=tvcctv&topv=1&t=json'.format(
  291. channel, datafl, id, letter, pg)
  292. elif tid == '栏目大全':
  293. cid = '' # 频道
  294. if 'cid' in extend.keys():
  295. cid = extend['cid']
  296. fc = '' # 分类
  297. if 'fc' in extend.keys():
  298. fc = extend['fc']
  299. fl = '' # 字母
  300. if 'fl' in extend.keys():
  301. fl = extend['fl']
  302. url = 'https://api.cntv.cn/lanmu/columnSearch?&fl={0}&fc={1}&cid={2}&p={3}&n=20&serviceId=tvcctv&t=json&cb=ko'.format(
  303. fl, fc, cid, pg)
  304. pagecount = 20
  305. else:
  306. url = 'https://tv.cctv.com/epg/index.shtml'
  307. videos = []
  308. htmlText = self.fetch(url).text
  309. if tid == '栏目大全':
  310. index = htmlText.rfind(');')
  311. if index > -1:
  312. htmlText = htmlText[3:index]
  313. videos = self.get_list1(html=htmlText, tid=tid)
  314. else:
  315. videos = self.get_list(html=htmlText, tid=tid)
  316. # print(videos)
  317. result['list'] = videos
  318. result['page'] = pg
  319. result['pagecount'] = 9999 if len(videos) >= pagecount else pg
  320. result['limit'] = 90
  321. result['total'] = 999999
  322. return result
  323. def detailContent(self, array):
  324. result = {}
  325. aid = array[0].split('||')
  326. tid = aid[0]
  327. logo = aid[3]
  328. lastVideo = aid[2]
  329. title = aid[1]
  330. id = aid[4]
  331. vod_year = aid[5]
  332. actors = aid[6] if len(aid) > 6 else ''
  333. brief = aid[7] if len(aid) > 7 else '' # get请求最长255,这个描述会有可能直接被干没了。
  334. fromId = 'CCTV'
  335. if tid == "栏目大全":
  336. lastUrl = 'https://api.cntv.cn/video/videoinfoByGuid?guid={0}&serviceId=tvcctv'.format(id)
  337. # htmlTxt = self.webReadFile(urlStr=lastUrl, header=self.header)
  338. htmlTxt = self.fetch(lastUrl).text
  339. topicId = json.loads(htmlTxt)['ctid']
  340. Url = "https://api.cntv.cn/NewVideo/getVideoListByColumn?id={0}&d=&p=1&n=100&sort=desc&mode=0&serviceId=tvcctv&t=json".format(
  341. topicId)
  342. # htmlTxt = self.webReadFile(urlStr=Url, header=self.header)
  343. htmlTxt = self.fetch(Url).text
  344. else:
  345. Url = 'https://api.cntv.cn/NewVideo/getVideoListByAlbumIdNew?id={0}&serviceId=tvcctv&p=1&n=100&mode=0&pub=1'.format(
  346. id)
  347. jRoot = ''
  348. videoList = []
  349. try:
  350. if tid == "搜索":
  351. fromId = '中央台'
  352. videoList = [title + "$" + lastVideo]
  353. else:
  354. # htmlTxt = self.webReadFile(urlStr=Url, header=self.header)
  355. htmlTxt = self.fetch(Url).text
  356. jRoot = json.loads(htmlTxt)
  357. data = jRoot['data']
  358. jsonList = data['list']
  359. videoList = self.get_EpisodesList(jsonList=jsonList)
  360. if len(videoList) < 1:
  361. # htmlTxt = self.webReadFile(urlStr=lastVideo, header=self.header)
  362. htmlTxt = self.fetch(lastVideo).text
  363. if tid == "电视剧" or tid == "纪录片":
  364. patternTxt = r"'title':\s*'(?P<title>.+?)',\n{0,1}\s*'brief':\s*'(.+?)',\n{0,1}\s*'img':\s*'(.+?)',\n{0,1}\s*'url':\s*'(?P<url>.+?)'"
  365. elif tid == "特别节目":
  366. patternTxt = r'class="tp1"><a\s*href="(?P<url>https://.+?)"\s*target="_blank"\s*title="(?P<title>.+?)"></a></div>'
  367. elif tid == "动画片":
  368. patternTxt = r"'title':\s*'(?P<title>.+?)',\n{0,1}\s*'img':\s*'(.+?)',\n{0,1}\s*'brief':\s*'(.+?)',\n{0,1}\s*'url':\s*'(?P<url>.+?)'"
  369. elif tid == "栏目大全":
  370. patternTxt = r'href="(?P<url>.+?)" target="_blank" alt="(?P<title>.+?)" title=".+?">'
  371. videoList = self.get_EpisodesList_re(htmlTxt=htmlTxt, patternTxt=patternTxt)
  372. fromId = '央视'
  373. except:
  374. pass
  375. if len(videoList) == 0:
  376. return {}
  377. vod = {
  378. "vod_id": array[0],
  379. "vod_name": title,
  380. "vod_pic": logo,
  381. "type_name": tid,
  382. "vod_year": vod_year,
  383. "vod_area": "",
  384. "vod_remarks": '',
  385. "vod_actor": actors,
  386. "vod_director": '',
  387. "vod_content": brief
  388. }
  389. vod['vod_play_from'] = fromId
  390. vod['vod_play_url'] = "#".join(videoList)
  391. result = {
  392. 'list': [
  393. vod
  394. ]
  395. }
  396. return result
  397. def get_lineList(self, Txt, mark, after):
  398. circuit = []
  399. origin = Txt.find(mark)
  400. while origin > 8:
  401. end = Txt.find(after, origin)
  402. circuit.append(Txt[origin:end])
  403. origin = Txt.find(mark, end)
  404. return circuit
  405. def get_RegexGetTextLine(self, Text, RegexText, Index):
  406. returnTxt = []
  407. pattern = re.compile(RegexText, re.M | re.S)
  408. ListRe = pattern.findall(Text)
  409. if len(ListRe) < 1:
  410. return returnTxt
  411. for value in ListRe:
  412. returnTxt.append(value)
  413. return returnTxt
  414. def searchContent(self, key, quick, pg=1):
  415. key = urllib.parse.quote(key)
  416. Url = 'https://search.cctv.com/ifsearch.php?page=1&qtext={0}&sort=relevance&pageSize=20&type=video&vtime=-1&datepid=1&channel=&pageflag=0&qtext_str={0}'.format(
  417. key)
  418. # htmlTxt = self.webReadFile(urlStr=Url, header=self.header)
  419. htmlTxt = self.fetch(Url).text
  420. videos = self.get_list_search(html=htmlTxt, tid='搜索')
  421. result = {
  422. 'list': videos
  423. }
  424. return result
  425. def playerContent(self, flag, id, vipFlags):
  426. result = {}
  427. url = ''
  428. parse = 0
  429. headers = {
  430. 'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1'
  431. }
  432. if flag == 'CCTV':
  433. url = self.get_m3u8(urlTxt=id)
  434. else:
  435. try:
  436. # htmlTxt = self.webReadFile(urlStr=id, header=self.header)
  437. htmlTxt = self.fetch(id).text
  438. guid = self.get_RegexGetText(Text=htmlTxt, RegexText=r'var\sguid\s*=\s*"(.+?)";', Index=1)
  439. url = self.get_m3u8(urlTxt=guid)
  440. except:
  441. url = id
  442. parse = 1
  443. if url.find('https:') < 0:
  444. url = id
  445. parse = 1
  446. result["parse"] = parse # 1=嗅探,0=播放
  447. result["playUrl"] = ''
  448. result["url"] = url
  449. result["header"] = headers
  450. return result
  451. # 分类抓取地址:
  452. # 栏目大全:https://tv.cctv.com/lm/index.shtml?spm=C28340.Pu9TN9YUsfNZ.E2PQtIunpEaz.24
  453. # 电视剧:https://tv.cctv.com/yxg/index.shtml?spm=C28340.PlFTqGe6Zk8M.E2PQtIunpEaz.65#datacid=dsj&datafl=&datadq=&fc=%E7%94%B5%E8%A7%86%E5%89%A7&datanf=&dataszm=
  454. # 动画片:https://tv.cctv.com/yxg/index.shtml?spm=C28340.PlFTqGe6Zk8M.E2PQtIunpEaz.65#datacid=dhp&datafl=&datadq=&fc=%E5%8A%A8%E7%94%BB%E7%89%87&dataszm=
  455. # 记录片:https://tv.cctv.com/yxg/index.shtml?spm=C28340.PlFTqGe6Zk8M.E2PQtIunpEaz.65#datacid=jlp&datapd=&datafl=&fc=%E7%BA%AA%E5%BD%95%E7%89%87&datanf=&dataszm=
  456. # 特别节目:https://tv.cctv.com/yxg/index.shtml?spm=C28340.PlFTqGe6Zk8M.E2PQtIunpEaz.65#datacid=tbjm&datapd=&datafl=&fc=%E7%89%B9%E5%88%AB%E8%8A%82%E7%9B%AE&datajr=&dataszm=
  457. config = {
  458. "player": {},
  459. "filter": {
  460. "电视剧": [
  461. {"key": "datafl-sc", "name": "类型",
  462. "value": [{"n": "全部", "v": ""}, {"n": "谍战", "v": "谍战"}, {"n": "悬疑", "v": "悬疑"},
  463. {"n": "刑侦", "v": "刑侦"}, {"n": "历史", "v": "历史"}, {"n": "古装", "v": "古装"},
  464. {"n": "武侠", "v": "武侠"}, {"n": "军旅", "v": "军旅"}, {"n": "战争", "v": "战争"},
  465. {"n": "喜剧", "v": "喜剧"}, {"n": "青春", "v": "青春"}, {"n": "言情", "v": "言情"},
  466. {"n": "偶像", "v": "偶像"}, {"n": "家庭", "v": "家庭"}, {"n": "年代", "v": "年代"},
  467. {"n": "革命", "v": "革命"}, {"n": "农村", "v": "农村"}, {"n": "都市", "v": "都市"},
  468. {"n": "其他", "v": "其他"}]},
  469. {"key": "datadq-area", "name": "地区",
  470. "value": [{"n": "全部", "v": ""}, {"n": "中国大陆", "v": "中国大陆"}, {"n": "中国香港", "v": "香港"},
  471. {"n": "美国", "v": "美国"}, {"n": "欧洲", "v": "欧洲"}, {"n": "泰国2", "v": "泰国"}]},
  472. {"key": "datanf-year", "name": "年份",
  473. "value": [{"n": "全部", "v": ""}, {"n": "2024", "v": "2024"},{"n": "2023", "v": "2023"}, {"n": "2022", "v": "2022"},
  474. {"n": "2021", "v": "2021"}, {"n": "2020", "v": "2020"}, {"n": "2019", "v": "2019"},
  475. {"n": "2018", "v": "2018"}, {"n": "2017", "v": "2017"}, {"n": "2016", "v": "2016"},
  476. {"n": "2015", "v": "2015"}, {"n": "2014", "v": "2014"}, {"n": "2013", "v": "2013"},
  477. {"n": "2012", "v": "2012"}, {"n": "2011", "v": "2011"}, {"n": "2010", "v": "2010"},
  478. {"n": "2009", "v": "2009"}, {"n": "2008", "v": "2008"}, {"n": "2007", "v": "2007"},
  479. {"n": "2006", "v": "2006"}, {"n": "2005", "v": "2005"}, {"n": "2004", "v": "2004"},
  480. {"n": "2003", "v": "2003"}, {"n": "2002", "v": "2002"}, {"n": "2001", "v": "2001"},
  481. {"n": "2000", "v": "2000"}, {"n": "1999", "v": "1999"}, {"n": "1998", "v": "1998"},
  482. {"n": "1997", "v": "1997"}]},
  483. {"key": "dataszm-letter", "name": "字母",
  484. "value": [{"n": "全部", "v": ""}, {"n": "A", "v": "A"}, {"n": "C", "v": "C"}, {"n": "E", "v": "E"},
  485. {"n": "F", "v": "F"}, {"n": "G", "v": "G"}, {"n": "H", "v": "H"}, {"n": "I", "v": "I"},
  486. {"n": "J", "v": "J"}, {"n": "K", "v": "K"}, {"n": "L", "v": "L"}, {"n": "M", "v": "M"},
  487. {"n": "N", "v": "N"}, {"n": "O", "v": "O"}, {"n": "P", "v": "P"}, {"n": "Q", "v": "Q"},
  488. {"n": "R", "v": "R"}, {"n": "S", "v": "S"}, {"n": "T", "v": "T"}, {"n": "U", "v": "U"},
  489. {"n": "V", "v": "V"}, {"n": "W", "v": "W"}, {"n": "X", "v": "X"}, {"n": "Y", "v": "Y"},
  490. {"n": "Z", "v": "Z"}, {"n": "0-9", "v": "0-9"}]}
  491. ],
  492. "动画片": [
  493. {"key": "datafl-sc", "name": "类型",
  494. "value": [{"n": "全部", "v": ""}, {"n": "亲子", "v": "亲子"}, {"n": "搞笑", "v": "搞笑"},
  495. {"n": "冒险", "v": "冒险"}, {"n": "动作", "v": "动作"}, {"n": "宠物", "v": "宠物"},
  496. {"n": "体育", "v": "体育"}, {"n": "益智", "v": "益智"}, {"n": "历史", "v": "历史"},
  497. {"n": "教育", "v": "教育"}, {"n": "校园", "v": "校园"}, {"n": "言情", "v": "言情"},
  498. {"n": "武侠", "v": "武侠"}, {"n": "经典", "v": "经典"}, {"n": "未来", "v": "未来"},
  499. {"n": "古代", "v": "古代"}, {"n": "神话", "v": "神话"}, {"n": "真人", "v": "真人"},
  500. {"n": "励志", "v": "励志"}, {"n": "热血", "v": "热血"}, {"n": "奇幻", "v": "奇幻"},
  501. {"n": "童话", "v": "童话"}, {"n": "剧情", "v": "剧情"}, {"n": "夺宝", "v": "夺宝"},
  502. {"n": "其他", "v": "其他"}]},
  503. {"key": "datadq-area", "name": "地区",
  504. "value": [{"n": "全部", "v": ""}, {"n": "中国大陆", "v": "中国大陆"}, {"n": "美国", "v": "美国"},
  505. {"n": "欧洲", "v": "欧洲"}]},
  506. {"key": "dataszm-letter", "name": "字母",
  507. "value": [{"n": "全部", "v": ""}, {"n": "A", "v": "A"}, {"n": "C", "v": "C"}, {"n": "E", "v": "E"},
  508. {"n": "F", "v": "F"}, {"n": "G", "v": "G"}, {"n": "H", "v": "H"}, {"n": "I", "v": "I"},
  509. {"n": "J", "v": "J"}, {"n": "K", "v": "K"}, {"n": "L", "v": "L"}, {"n": "M", "v": "M"},
  510. {"n": "N", "v": "N"}, {"n": "O", "v": "O"}, {"n": "P", "v": "P"}, {"n": "Q", "v": "Q"},
  511. {"n": "R", "v": "R"}, {"n": "S", "v": "S"}, {"n": "T", "v": "T"}, {"n": "U", "v": "U"},
  512. {"n": "V", "v": "V"}, {"n": "W", "v": "W"}, {"n": "X", "v": "X"}, {"n": "Y", "v": "Y"},
  513. {"n": "Z", "v": "Z"}, {"n": "0-9", "v": "0-9"}]}
  514. ],
  515. "纪录片": [
  516. {"key": "datapd-channel", "name": "频道",
  517. "value": [{"n": "全部", "v": ""}, {"n": "CCTV{1 综合", "v": "CCTV{1 综合"},
  518. {"n": "CCTV{2 财经", "v": "CCTV{2 财经"}, {"n": "CCTV{3 综艺", "v": "CCTV{3 综艺"},
  519. {"n": "CCTV{4 中文国际", "v": "CCTV{4 中文国际"}, {"n": "CCTV{5 体育", "v": "CCTV{5 体育"},
  520. {"n": "CCTV{6 电影", "v": "CCTV{6 电影"}, {"n": "CCTV{7 国防军事", "v": "CCTV{7 国防军事"},
  521. {"n": "CCTV{8 电视剧", "v": "CCTV{8 电视剧"}, {"n": "CCTV{9 纪录", "v": "CCTV{9 纪录"},
  522. {"n": "CCTV{10 科教", "v": "CCTV{10 科教"}, {"n": "CCTV{11 戏曲", "v": "CCTV{11 戏曲"},
  523. {"n": "CCTV{12 社会与法", "v": "CCTV{12 社会与法"},
  524. {"n": "CCTV{13 新闻", "v": "CCTV{13 新闻"}, {"n": "CCTV{14 少儿", "v": "CCTV{14 少儿"},
  525. {"n": "CCTV{15 音乐", "v": "CCTV{15 音乐"},
  526. {"n": "CCTV{17 农业农村", "v": "CCTV{17 农业农村"}]},
  527. {"key": "datafl-sc", "name": "类型",
  528. "value": [{"n": "全部", "v": ""}, {"n": "人文历史", "v": "人文历史"}, {"n": "人物", "v": "人物"},
  529. {"n": "军事", "v": "军事"}, {"n": "探索", "v": "探索"}, {"n": "社会", "v": "社会"},
  530. {"n": "时政", "v": "时政"}, {"n": "经济", "v": "经济"}, {"n": "科技", "v": "科技"}]},
  531. {"key": "datanf-year", "name": "年份",
  532. "value": [{"n": "全部", "v": ""}, {"n": "2024", "v": "2024"},{"n": "2023", "v": "2023"}, {"n": "2022", "v": "2022"},
  533. {"n": "2021", "v": "2021"}, {"n": "2020", "v": "2020"}, {"n": "2019", "v": "2019"},
  534. {"n": "2018", "v": "2018"}, {"n": "2017", "v": "2017"}, {"n": "2016", "v": "2016"},
  535. {"n": "2015", "v": "2015"}, {"n": "2014", "v": "2014"}, {"n": "2013", "v": "2013"},
  536. {"n": "2012", "v": "2012"}, {"n": "2011", "v": "2011"}, {"n": "2010", "v": "2010"},
  537. {"n": "2009", "v": "2009"}, {"n": "2008", "v": "2008"}]},
  538. {"key": "dataszm-letter", "name": "字母",
  539. "value": [{"n": "全部", "v": ""}, {"n": "A", "v": "A"}, {"n": "C", "v": "C"}, {"n": "E", "v": "E"},
  540. {"n": "F", "v": "F"}, {"n": "G", "v": "G"}, {"n": "H", "v": "H"}, {"n": "I", "v": "I"},
  541. {"n": "J", "v": "J"}, {"n": "K", "v": "K"}, {"n": "L", "v": "L"}, {"n": "M", "v": "M"},
  542. {"n": "N", "v": "N"}, {"n": "O", "v": "O"}, {"n": "P", "v": "P"}, {"n": "Q", "v": "Q"},
  543. {"n": "R", "v": "R"}, {"n": "S", "v": "S"}, {"n": "T", "v": "T"}, {"n": "U", "v": "U"},
  544. {"n": "V", "v": "V"}, {"n": "W", "v": "W"}, {"n": "X", "v": "X"}, {"n": "Y", "v": "Y"},
  545. {"n": "Z", "v": "Z"}, {"n": "0-9", "v": "0-9"}]}
  546. ],
  547. "特别节目": [
  548. {"key": "datapd-channel", "name": "频道",
  549. "value": [{"n": "全部", "v": ""}, {"n": "CCTV{1 综合", "v": "CCTV{1 综合"},
  550. {"n": "CCTV{2 财经", "v": "CCTV{2 财经"}, {"n": "CCTV{3 综艺", "v": "CCTV{3 综艺"},
  551. {"n": "CCTV{4 中文国际", "v": "CCTV{4 中文国际"}, {"n": "CCTV{5 体育", "v": "CCTV{5 体育"},
  552. {"n": "CCTV{6 电影", "v": "CCTV{6 电影"}, {"n": "CCTV{7 国防军事", "v": "CCTV{7 国防军事"},
  553. {"n": "CCTV{8 电视剧", "v": "CCTV{8 电视剧"}, {"n": "CCTV{9 纪录", "v": "CCTV{9 纪录"},
  554. {"n": "CCTV{10 科教", "v": "CCTV{10 科教"}, {"n": "CCTV{11 戏曲", "v": "CCTV{11 戏曲"},
  555. {"n": "CCTV{12 社会与法", "v": "CCTV{12 社会与法"},
  556. {"n": "CCTV{13 新闻", "v": "CCTV{13 新闻"}, {"n": "CCTV{14 少儿", "v": "CCTV{14 少儿"},
  557. {"n": "CCTV{15 音乐", "v": "CCTV{15 音乐"},
  558. {"n": "CCTV{17 农业农村", "v": "CCTV{17 农业农村"}]},
  559. {"key": "datafl-sc", "name": "类型",
  560. "value": [{"n": "全部", "v": ""}, {"n": "全部", "v": "全部"}, {"n": "新闻", "v": "新闻"},
  561. {"n": "经济", "v": "经济"}, {"n": "综艺", "v": "综艺"}, {"n": "体育", "v": "体育"},
  562. {"n": "军事", "v": "军事"}, {"n": "影视", "v": "影视"}, {"n": "科教", "v": "科教"},
  563. {"n": "戏曲", "v": "戏曲"}, {"n": "青少", "v": "青少"}, {"n": "音乐", "v": "音乐"},
  564. {"n": "社会", "v": "社会"}, {"n": "公益", "v": "公益"}, {"n": "其他", "v": "其他"}]},
  565. {"key": "dataszm-letter", "name": "字母",
  566. "value": [{"n": "全部", "v": ""}, {"n": "A", "v": "A"}, {"n": "C", "v": "C"}, {"n": "E", "v": "E"},
  567. {"n": "F", "v": "F"}, {"n": "G", "v": "G"}, {"n": "H", "v": "H"}, {"n": "I", "v": "I"},
  568. {"n": "J", "v": "J"}, {"n": "K", "v": "K"}, {"n": "L", "v": "L"}, {"n": "M", "v": "M"},
  569. {"n": "N", "v": "N"}, {"n": "O", "v": "O"}, {"n": "P", "v": "P"}, {"n": "Q", "v": "Q"},
  570. {"n": "R", "v": "R"}, {"n": "S", "v": "S"}, {"n": "T", "v": "T"}, {"n": "U", "v": "U"},
  571. {"n": "V", "v": "V"}, {"n": "W", "v": "W"}, {"n": "X", "v": "X"}, {"n": "Y", "v": "Y"},
  572. {"n": "Z", "v": "Z"}, {"n": "0-9", "v": "0-9"}]}
  573. ],
  574. "栏目大全": [{"key": "cid", "name": "频道",
  575. "value": [{"n": "全部", "v": ""}, {"n": "CCTV-1综合", "v": "EPGC1386744804340101"},
  576. {"n": "CCTV-2财经", "v": "EPGC1386744804340102"},
  577. {"n": "CCTV-3综艺", "v": "EPGC1386744804340103"},
  578. {"n": "CCTV-4中文国际", "v": "EPGC1386744804340104"},
  579. {"n": "CCTV-5体育", "v": "EPGC1386744804340107"},
  580. {"n": "CCTV-6电影", "v": "EPGC1386744804340108"},
  581. {"n": "CCTV-7国防军事", "v": "EPGC1386744804340109"},
  582. {"n": "CCTV-8电视剧", "v": "EPGC1386744804340110"},
  583. {"n": "CCTV-9纪录", "v": "EPGC1386744804340112"},
  584. {"n": "CCTV-10科教", "v": "EPGC1386744804340113"},
  585. {"n": "CCTV-11戏曲", "v": "EPGC1386744804340114"},
  586. {"n": "CCTV-12社会与法", "v": "EPGC1386744804340115"},
  587. {"n": "CCTV-13新闻", "v": "EPGC1386744804340116"},
  588. {"n": "CCTV-14少儿", "v": "EPGC1386744804340117"},
  589. {"n": "CCTV-15音乐", "v": "EPGC1386744804340118"},
  590. {"n": "CCTV-16奥林匹克", "v": "EPGC1634630207058998"},
  591. {"n": "CCTV-17农业农村", "v": "EPGC1563932742616872"},
  592. {"n": "CCTV-5+体育赛事", "v": "EPGC1468294755566101"}]},
  593. {"key": "fc", "name": "分类",
  594. "value": [{"n": "全部", "v": ""}, {"n": "新闻", "v": "新闻"}, {"n": "体育", "v": "体育"},
  595. {"n": "综艺", "v": "综艺"}, {"n": "健康", "v": "健康"}, {"n": "生活", "v": "生活"},
  596. {"n": "科教", "v": "科教"}, {"n": "经济", "v": "经济"}, {"n": "农业", "v": "农业"},
  597. {"n": "法治", "v": "法治"}, {"n": "军事", "v": "军事"}, {"n": "少儿", "v": "少儿"},
  598. {"n": "动画", "v": "动画"}, {"n": "纪实", "v": "纪实"}, {"n": "戏曲", "v": "戏曲"},
  599. {"n": "音乐", "v": "音乐"}, {"n": "影视", "v": "影视"}]},
  600. {"key": "fl", "name": "字母",
  601. "value": [{"n": "全部", "v": ""}, {"n": "A", "v": "A"}, {"n": "B", "v": "B"},
  602. {"n": "C", "v": "C"}, {"n": "D", "v": "D"}, {"n": "E", "v": "E"},
  603. {"n": "F", "v": "F"}, {"n": "G", "v": "G"}, {"n": "H", "v": "H"},
  604. {"n": "I", "v": "I"}, {"n": "J", "v": "J"}, {"n": "K", "v": "K"},
  605. {"n": "L", "v": "L"}, {"n": "M", "v": "M"}, {"n": "N", "v": "N"},
  606. {"n": "O", "v": "O"}, {"n": "P", "v": "P"}, {"n": "Q", "v": "Q"},
  607. {"n": "R", "v": "R"}, {"n": "S", "v": "S"}, {"n": "T", "v": "T"},
  608. {"n": "U", "v": "U"}, {"n": "V", "v": "V"}, {"n": "W", "v": "W"},
  609. {"n": "X", "v": "X"}, {"n": "Y", "v": "Y"}, {"n": "Z", "v": "Z"}]},
  610. ]
  611. }
  612. }
  613. header = {
  614. "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.54 Safari/537.36",
  615. "Host": "tv.cctv.com",
  616. "Referer": "https://tv.cctv.com/"
  617. }
  618. def localProxy(self, param):
  619. return [200, "video/MP2T", action, ""]
  620. # -----------------------------------------------自定义函数-----------------------------------------------
  621. # 访问网页
  622. def webReadFile(self, urlStr, header):
  623. html = ''
  624. req = urllib.request.Request(url=urlStr) # ,headers=header
  625. with urllib.request.urlopen(req) as response:
  626. html = response.read().decode('utf-8')
  627. return html
  628. # 判断网络地址是否存在
  629. def TestWebPage(self, urlStr, header):
  630. html = ''
  631. req = urllib.request.Request(url=urlStr, method='HEAD') # ,headers=header
  632. with urllib.request.urlopen(req) as response:
  633. html = response.getcode()
  634. return html
  635. # 正则取文本
  636. def get_RegexGetText(self, Text, RegexText, Index):
  637. returnTxt = ""
  638. Regex = re.search(RegexText, Text, re.M | re.S)
  639. if Regex is None:
  640. returnTxt = ""
  641. else:
  642. returnTxt = Regex.group(Index)
  643. return returnTxt
  644. # 取集数
  645. def get_EpisodesList(self, jsonList):
  646. videos = []
  647. for vod in jsonList:
  648. url = vod['guid']
  649. title = vod['title']
  650. if len(url) == 0:
  651. continue
  652. videos.append(title + "$" + url)
  653. return videos
  654. # 取集数
  655. def get_EpisodesList_re(self, htmlTxt, patternTxt):
  656. ListRe = re.finditer(patternTxt, htmlTxt, re.M | re.S)
  657. videos = []
  658. for vod in ListRe:
  659. url = vod.group('url')
  660. title = vod.group('title')
  661. if len(url) == 0:
  662. continue
  663. videos.append(title + "$" + url)
  664. return videos
  665. # 取剧集区
  666. def get_lineList(self, Txt, mark, after):
  667. circuit = []
  668. origin = Txt.find(mark)
  669. while origin > 8:
  670. end = Txt.find(after, origin)
  671. circuit.append(Txt[origin:end])
  672. origin = Txt.find(mark, end)
  673. return circuit
  674. # 正则取文本,返回数组
  675. def get_RegexGetTextLine(self, Text, RegexText, Index):
  676. returnTxt = []
  677. pattern = re.compile(RegexText, re.M | re.S)
  678. ListRe = pattern.findall(Text)
  679. if len(ListRe) < 1:
  680. return returnTxt
  681. for value in ListRe:
  682. returnTxt.append(value)
  683. return returnTxt
  684. # 删除html标签
  685. def removeHtml(self, txt):
  686. soup = re.compile(r'<[^>]+>', re.S)
  687. txt = soup.sub('', txt)
  688. return txt.replace("&nbsp;", " ")
  689. # 取m3u8
  690. def get_m3u8(self, urlTxt):
  691. url = "https://vdn.apps.cntv.cn/api/getHttpVideoInfo.do?pid={0}".format(urlTxt)
  692. # htmlTxt = self.webReadFile(urlStr=url, header=self.header)
  693. htmlTxt = self.fetch(url).text
  694. jo = json.loads(htmlTxt)
  695. link = jo['hls_url'].strip()
  696. html = self.webReadFile(urlStr=link, header=self.header)
  697. content = html.strip()
  698. arr = content.split('\n')
  699. urlPrefix = self.get_RegexGetText(Text=link, RegexText='(http[s]?://[a-zA-z0-9.]+)/', Index=1)
  700. subUrl = arr[-1].split('/')
  701. # subUrl[3] = '1200'
  702. subUrl[3] = '2000'
  703. # subUrl[-1] = '1200.m3u8'
  704. subUrl[-1] = '2000.m3u8'
  705. hdUrl = urlPrefix + '/'.join(subUrl)
  706. url = urlPrefix + arr[-1]
  707. hdRsp = self.TestWebPage(urlStr=hdUrl, header=self.header)
  708. if hdRsp == 200:
  709. url = hdUrl
  710. else:
  711. url = ''
  712. return url
  713. # 搜索
  714. def get_list_search(self, html, tid):
  715. jRoot = json.loads(html)
  716. jsonList = jRoot['list']
  717. videos = []
  718. for vod in jsonList:
  719. url = vod['urllink']
  720. title = self.removeHtml(txt=vod['title'])
  721. img = vod['imglink']
  722. id = vod['id']
  723. brief = vod['channel']
  724. year = vod['uploadtime']
  725. if len(url) == 0:
  726. continue
  727. guids = [tid, title, url, img, id, year, '', brief]
  728. guid = "||".join(guids)
  729. videos.append({
  730. "vod_id": guid,
  731. "vod_name": title,
  732. "vod_pic": img,
  733. "vod_remarks": year
  734. })
  735. return videos
  736. def get_list1(self, html, tid):
  737. jRoot = json.loads(html)
  738. videos = []
  739. data = jRoot['response']
  740. if data is None:
  741. return []
  742. jsonList = data['docs']
  743. for vod in jsonList:
  744. id = vod['lastVIDE']['videoSharedCode']
  745. desc = vod['lastVIDE']['videoTitle']
  746. title = vod['column_name']
  747. url = vod['column_website']
  748. img = vod['column_logo']
  749. year = vod['column_playdate']
  750. brief = vod['column_brief']
  751. actors = ''
  752. if len(url) == 0:
  753. continue
  754. guids = [tid, title, url, img, id, year, actors, brief]
  755. guid = "||".join(guids)
  756. # print(vod_id)
  757. videos.append({
  758. "vod_id": guid,
  759. "vod_name": title,
  760. "vod_pic": img,
  761. "vod_remarks": desc.split('》')[1].strip() if '》' in desc else desc.strip()
  762. })
  763. # print(videos)
  764. return videos
  765. # 分类取结果
  766. def get_list(self, html, tid):
  767. jRoot = json.loads(html)
  768. videos = []
  769. data = jRoot['data']
  770. if data is None:
  771. return []
  772. jsonList = data['list']
  773. for vod in jsonList:
  774. url = vod['url']
  775. title = vod['title']
  776. img = vod['image']
  777. id = vod['id']
  778. try:
  779. brief = vod['brief']
  780. except:
  781. brief = ''
  782. try:
  783. year = vod['year']
  784. except:
  785. year = ''
  786. try:
  787. actors = vod['actors']
  788. except:
  789. actors = ''
  790. if len(url) == 0:
  791. continue
  792. guids = [tid, title, url, img, id, year, actors, brief]
  793. guid = "||".join(guids)
  794. # print(vod_id)
  795. videos.append({
  796. "vod_id": guid,
  797. "vod_name": title,
  798. "vod_pic": img,
  799. "vod_remarks": ''
  800. })
  801. return videos
  802. if __name__ == '__main__':
  803. spider = Spider()
  804. spider.init()
  805. spider.init_api_ext_file()
  806. # url = 'https://api.cntv.cn/lanmu/columnSearch?&fl=&fc=%E6%96%B0%E9%97%BB&cid=&p=1&n=20&serviceId=tvcctv&t=jsonp&cb=Callback'
  807. # url = 'https://api.cntv.cn/lanmu/columnSearch?&fl=&fc=&cid=&p=1&n=20&serviceId=tvcctv&t=json&cb=ko'
  808. # r = spider.fetch(url)
  809. # print(r.text)
  810. # home_content = spider.homeContent(None)
  811. # print(home_content)
  812. # cate_content = spider.categoryContent('栏目大全', 1, {'cid': 'n'}, {})
  813. # print(cate_content)
  814. # vid = cate_content['list'][0]['vod_id']
  815. # print(vid)
  816. # detail_content = spider.detailContent([vid])
  817. # print(detail_content)
  818. #
  819. # vod_play_from = detail_content['list'][0]['vod_play_from']
  820. # vod_play_url = detail_content['list'][0]['vod_play_url']
  821. # print(vod_play_from, vod_play_url)
  822. # _url = vod_play_url.split('#')[0].split('$')[1]
  823. # print(_url)
  824. # play = spider.playerContent(vod_play_from, _url, None)
  825. # print(play)