cntv央视.py 45 KB

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