tencent.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. import functools
  2. import random
  3. import re
  4. import string
  5. import time
  6. from .common import InfoExtractor
  7. from ..aes import aes_cbc_encrypt_bytes
  8. from ..utils import (
  9. ExtractorError,
  10. determine_ext,
  11. int_or_none,
  12. js_to_json,
  13. traverse_obj,
  14. urljoin,
  15. )
  16. class TencentBaseIE(InfoExtractor):
  17. """Subclasses must set _API_URL, _APP_VERSION, _PLATFORM, _HOST, _REFERER"""
  18. def _get_ckey(self, video_id, url, guid):
  19. ua = self.get_param('http_headers')['User-Agent']
  20. payload = (f'{video_id}|{int(time.time())}|mg3c3b04ba|{self._APP_VERSION}|{guid}|'
  21. f'{self._PLATFORM}|{url[:48]}|{ua.lower()[:48]}||Mozilla|Netscape|Windows x86_64|00|')
  22. return aes_cbc_encrypt_bytes(
  23. bytes(f'|{sum(map(ord, payload))}|{payload}', 'utf-8'),
  24. b'Ok\xda\xa3\x9e/\x8c\xb0\x7f^r-\x9e\xde\xf3\x14',
  25. b'\x01PJ\xf3V\xe6\x19\xcf.B\xbb\xa6\x8c?p\xf9',
  26. padding_mode='whitespace').hex().upper()
  27. def _get_video_api_response(self, video_url, video_id, series_id, subtitle_format, video_format, video_quality):
  28. guid = ''.join([random.choice(string.digits + string.ascii_lowercase) for _ in range(16)])
  29. ckey = self._get_ckey(video_id, video_url, guid)
  30. query = {
  31. 'vid': video_id,
  32. 'cid': series_id,
  33. 'cKey': ckey,
  34. 'encryptVer': '8.1',
  35. 'spcaptiontype': '1' if subtitle_format == 'vtt' else '0',
  36. 'sphls': '2' if video_format == 'hls' else '0',
  37. 'dtype': '3' if video_format == 'hls' else '0',
  38. 'defn': video_quality,
  39. 'spsrt': '2', # Enable subtitles
  40. 'sphttps': '1', # Enable HTTPS
  41. 'otype': 'json',
  42. 'spwm': '1',
  43. # For SHD
  44. 'host': self._HOST,
  45. 'referer': self._REFERER,
  46. 'ehost': video_url,
  47. 'appVer': self._APP_VERSION,
  48. 'platform': self._PLATFORM,
  49. # For VQQ
  50. 'guid': guid,
  51. 'flowid': ''.join(random.choice(string.digits + string.ascii_lowercase) for _ in range(32)),
  52. }
  53. return self._search_json(r'QZOutputJson=', self._download_webpage(
  54. self._API_URL, video_id, query=query), 'api_response', video_id)
  55. def _extract_video_formats_and_subtitles(self, api_response, video_id):
  56. video_response = api_response['vl']['vi'][0]
  57. video_width, video_height = video_response.get('vw'), video_response.get('vh')
  58. formats, subtitles = [], {}
  59. for video_format in video_response['ul']['ui']:
  60. if video_format.get('hls') or determine_ext(video_format['url']) == 'm3u8':
  61. fmts, subs = self._extract_m3u8_formats_and_subtitles(
  62. video_format['url'] + traverse_obj(video_format, ('hls', 'pt'), default=''),
  63. video_id, 'mp4', fatal=False)
  64. for f in fmts:
  65. f.update({'width': video_width, 'height': video_height})
  66. formats.extend(fmts)
  67. self._merge_subtitles(subs, target=subtitles)
  68. else:
  69. formats.append({
  70. 'url': f'{video_format["url"]}{video_response["fn"]}?vkey={video_response["fvkey"]}',
  71. 'width': video_width,
  72. 'height': video_height,
  73. 'ext': 'mp4',
  74. })
  75. return formats, subtitles
  76. def _extract_video_native_subtitles(self, api_response, subtitles_format):
  77. subtitles = {}
  78. for subtitle in traverse_obj(api_response, ('sfl', 'fi')) or ():
  79. subtitles.setdefault(subtitle['lang'].lower(), []).append({
  80. 'url': subtitle['url'],
  81. 'ext': subtitles_format,
  82. 'protocol': 'm3u8_native' if determine_ext(subtitle['url']) == 'm3u8' else 'http',
  83. })
  84. return subtitles
  85. def _extract_all_video_formats_and_subtitles(self, url, video_id, series_id):
  86. formats, subtitles = [], {}
  87. for video_format, subtitle_format, video_quality in (
  88. # '': 480p, 'shd': 720p, 'fhd': 1080p
  89. ('mp4', 'srt', ''), ('hls', 'vtt', 'shd'), ('hls', 'vtt', 'fhd')):
  90. api_response = self._get_video_api_response(
  91. url, video_id, series_id, subtitle_format, video_format, video_quality)
  92. if api_response.get('em') != 0 and api_response.get('exem') != 0:
  93. if '您所在区域暂无此内容版权' in api_response.get('msg'):
  94. self.raise_geo_restricted()
  95. raise ExtractorError(f'Tencent said: {api_response.get("msg")}')
  96. fmts, subs = self._extract_video_formats_and_subtitles(api_response, video_id)
  97. native_subtitles = self._extract_video_native_subtitles(api_response, subtitle_format)
  98. formats.extend(fmts)
  99. self._merge_subtitles(subs, native_subtitles, target=subtitles)
  100. return formats, subtitles
  101. def _get_clean_title(self, title):
  102. return re.sub(
  103. r'\s*[_\-]\s*(?:Watch online|腾讯视频|(?:高清)?1080P在线观看平台).*?$',
  104. '', title or '').strip() or None
  105. class VQQBaseIE(TencentBaseIE):
  106. _VALID_URL_BASE = r'https?://v\.qq\.com'
  107. _API_URL = 'https://h5vv6.video.qq.com/getvinfo'
  108. _APP_VERSION = '3.5.57'
  109. _PLATFORM = '10901'
  110. _HOST = 'v.qq.com'
  111. _REFERER = 'v.qq.com'
  112. def _get_webpage_metadata(self, webpage, video_id):
  113. return self._parse_json(
  114. self._search_regex(
  115. r'(?s)<script[^>]*>[^<]*window\.__pinia\s*=\s*([^<]+)</script>',
  116. webpage, 'pinia data', fatal=False),
  117. video_id, transform_source=js_to_json, fatal=False)
  118. class VQQVideoIE(VQQBaseIE):
  119. IE_NAME = 'vqq:video'
  120. _VALID_URL = VQQBaseIE._VALID_URL_BASE + r'/x/(?:page|cover/(?P<series_id>\w+))/(?P<id>\w+)'
  121. _TESTS = [{
  122. 'url': 'https://v.qq.com/x/page/q326831cny0.html',
  123. 'md5': '826ef93682df09e3deac4a6e6e8cdb6e',
  124. 'info_dict': {
  125. 'id': 'q326831cny0',
  126. 'ext': 'mp4',
  127. 'title': '我是选手:雷霆裂阵,终极时刻',
  128. 'description': 'md5:e7ed70be89244017dac2a835a10aeb1e',
  129. 'thumbnail': r're:^https?://[^?#]+q326831cny0',
  130. },
  131. }, {
  132. 'url': 'https://v.qq.com/x/page/o3013za7cse.html',
  133. 'md5': 'b91cbbeada22ef8cc4b06df53e36fa21',
  134. 'info_dict': {
  135. 'id': 'o3013za7cse',
  136. 'ext': 'mp4',
  137. 'title': '欧阳娜娜VLOG',
  138. 'description': 'md5:29fe847497a98e04a8c3826e499edd2e',
  139. 'thumbnail': r're:^https?://[^?#]+o3013za7cse',
  140. },
  141. }, {
  142. 'url': 'https://v.qq.com/x/cover/7ce5noezvafma27/a00269ix3l8.html',
  143. 'md5': '71459c5375c617c265a22f083facce67',
  144. 'info_dict': {
  145. 'id': 'a00269ix3l8',
  146. 'ext': 'mp4',
  147. 'title': '鸡毛飞上天 第01集',
  148. 'description': 'md5:8cae3534327315b3872fbef5e51b5c5b',
  149. 'thumbnail': r're:^https?://[^?#]+7ce5noezvafma27',
  150. 'series': '鸡毛飞上天',
  151. },
  152. }, {
  153. 'url': 'https://v.qq.com/x/cover/mzc00200p29k31e/s0043cwsgj0.html',
  154. 'md5': '96b9fd4a189fdd4078c111f21d7ac1bc',
  155. 'info_dict': {
  156. 'id': 's0043cwsgj0',
  157. 'ext': 'mp4',
  158. 'title': '第1集:如何快乐吃糖?',
  159. 'description': 'md5:1d8c3a0b8729ae3827fa5b2d3ebd5213',
  160. 'thumbnail': r're:^https?://[^?#]+s0043cwsgj0',
  161. 'series': '青年理工工作者生活研究所',
  162. },
  163. }, {
  164. # Geo-restricted to China
  165. 'url': 'https://v.qq.com/x/cover/mcv8hkc8zk8lnov/x0036x5qqsr.html',
  166. 'only_matching': True,
  167. }]
  168. def _real_extract(self, url):
  169. video_id, series_id = self._match_valid_url(url).group('id', 'series_id')
  170. webpage = self._download_webpage(url, video_id)
  171. webpage_metadata = self._get_webpage_metadata(webpage, video_id)
  172. formats, subtitles = self._extract_all_video_formats_and_subtitles(url, video_id, series_id)
  173. return {
  174. 'id': video_id,
  175. 'title': self._get_clean_title(self._og_search_title(webpage)
  176. or traverse_obj(webpage_metadata, ('global', 'videoInfo', 'title'))),
  177. 'description': (self._og_search_description(webpage)
  178. or traverse_obj(webpage_metadata, ('global', 'videoInfo', 'desc'))),
  179. 'formats': formats,
  180. 'subtitles': subtitles,
  181. 'thumbnail': (self._og_search_thumbnail(webpage)
  182. or traverse_obj(webpage_metadata, ('global', 'videoInfo', 'pic160x90'))),
  183. 'series': traverse_obj(webpage_metadata, ('global', 'coverInfo', 'title')),
  184. }
  185. class VQQSeriesIE(VQQBaseIE):
  186. IE_NAME = 'vqq:series'
  187. _VALID_URL = VQQBaseIE._VALID_URL_BASE + r'/x/cover/(?P<id>\w+)\.html/?(?:[?#]|$)'
  188. _TESTS = [{
  189. 'url': 'https://v.qq.com/x/cover/7ce5noezvafma27.html',
  190. 'info_dict': {
  191. 'id': '7ce5noezvafma27',
  192. 'title': '鸡毛飞上天',
  193. 'description': 'md5:8cae3534327315b3872fbef5e51b5c5b',
  194. },
  195. 'playlist_count': 55,
  196. }, {
  197. 'url': 'https://v.qq.com/x/cover/oshd7r0vy9sfq8e.html',
  198. 'info_dict': {
  199. 'id': 'oshd7r0vy9sfq8e',
  200. 'title': '恋爱细胞2',
  201. 'description': 'md5:9d8a2245679f71ca828534b0f95d2a03',
  202. },
  203. 'playlist_count': 12,
  204. }]
  205. def _real_extract(self, url):
  206. series_id = self._match_id(url)
  207. webpage = self._download_webpage(url, series_id)
  208. webpage_metadata = self._get_webpage_metadata(webpage, series_id)
  209. episode_paths = [f'/x/cover/{series_id}/{video_id}.html' for video_id in re.findall(
  210. r'<div[^>]+data-vid="(?P<video_id>[^"]+)"[^>]+class="[^"]+episode-item-rect--number',
  211. webpage)]
  212. return self.playlist_from_matches(
  213. episode_paths, series_id, ie=VQQVideoIE, getter=functools.partial(urljoin, url),
  214. title=self._get_clean_title(traverse_obj(webpage_metadata, ('coverInfo', 'title'))
  215. or self._og_search_title(webpage)),
  216. description=(traverse_obj(webpage_metadata, ('coverInfo', 'description'))
  217. or self._og_search_description(webpage)))
  218. class WeTvBaseIE(TencentBaseIE):
  219. _VALID_URL_BASE = r'https?://(?:www\.)?wetv\.vip/(?:[^?#]+/)?play'
  220. _API_URL = 'https://play.wetv.vip/getvinfo'
  221. _APP_VERSION = '3.5.57'
  222. _PLATFORM = '4830201'
  223. _HOST = 'wetv.vip'
  224. _REFERER = 'wetv.vip'
  225. def _get_webpage_metadata(self, webpage, video_id):
  226. return self._parse_json(
  227. traverse_obj(self._search_nextjs_data(webpage, video_id), ('props', 'pageProps', 'data')),
  228. video_id, fatal=False)
  229. def _extract_episode(self, url):
  230. video_id, series_id = self._match_valid_url(url).group('id', 'series_id')
  231. webpage = self._download_webpage(url, video_id)
  232. webpage_metadata = self._get_webpage_metadata(webpage, video_id)
  233. formats, subtitles = self._extract_all_video_formats_and_subtitles(url, video_id, series_id)
  234. return {
  235. 'id': video_id,
  236. 'title': self._get_clean_title(self._og_search_title(webpage)
  237. or traverse_obj(webpage_metadata, ('coverInfo', 'title'))),
  238. 'description': (traverse_obj(webpage_metadata, ('coverInfo', 'description'))
  239. or self._og_search_description(webpage)),
  240. 'formats': formats,
  241. 'subtitles': subtitles,
  242. 'thumbnail': self._og_search_thumbnail(webpage),
  243. 'duration': int_or_none(traverse_obj(webpage_metadata, ('videoInfo', 'duration'))),
  244. 'series': traverse_obj(webpage_metadata, ('coverInfo', 'title')),
  245. 'episode_number': int_or_none(traverse_obj(webpage_metadata, ('videoInfo', 'episode'))),
  246. }
  247. def _extract_series(self, url, ie):
  248. series_id = self._match_id(url)
  249. webpage = self._download_webpage(url, series_id)
  250. webpage_metadata = self._get_webpage_metadata(webpage, series_id)
  251. episode_paths = ([f'/play/{series_id}/{episode["vid"]}' for episode in webpage_metadata.get('videoList')]
  252. or re.findall(r'<a[^>]+class="play-video__link"[^>]+href="(?P<path>[^"]+)', webpage))
  253. return self.playlist_from_matches(
  254. episode_paths, series_id, ie=ie, getter=functools.partial(urljoin, url),
  255. title=self._get_clean_title(traverse_obj(webpage_metadata, ('coverInfo', 'title'))
  256. or self._og_search_title(webpage)),
  257. description=(traverse_obj(webpage_metadata, ('coverInfo', 'description'))
  258. or self._og_search_description(webpage)))
  259. class WeTvEpisodeIE(WeTvBaseIE):
  260. IE_NAME = 'wetv:episode'
  261. _VALID_URL = WeTvBaseIE._VALID_URL_BASE + r'/(?P<series_id>\w+)(?:-[^?#]+)?/(?P<id>\w+)(?:-[^?#]+)?'
  262. _TESTS = [{
  263. 'url': 'https://wetv.vip/en/play/air11ooo2rdsdi3-Cute-Programmer/v0040pr89t9-EP1-Cute-Programmer',
  264. 'md5': '0c70fdfaa5011ab022eebc598e64bbbe',
  265. 'info_dict': {
  266. 'id': 'v0040pr89t9',
  267. 'ext': 'mp4',
  268. 'title': 'EP1: Cute Programmer',
  269. 'description': 'md5:e87beab3bf9f392d6b9e541a63286343',
  270. 'thumbnail': r're:^https?://[^?#]+air11ooo2rdsdi3',
  271. 'series': 'Cute Programmer',
  272. 'episode': 'Episode 1',
  273. 'episode_number': 1,
  274. 'duration': 2835,
  275. },
  276. }, {
  277. 'url': 'https://wetv.vip/en/play/u37kgfnfzs73kiu/p0039b9nvik',
  278. 'md5': '3b3c15ca4b9a158d8d28d5aa9d7c0a49',
  279. 'info_dict': {
  280. 'id': 'p0039b9nvik',
  281. 'ext': 'mp4',
  282. 'title': 'EP1: You Are My Glory',
  283. 'description': 'md5:831363a4c3b4d7615e1f3854be3a123b',
  284. 'thumbnail': r're:^https?://[^?#]+u37kgfnfzs73kiu',
  285. 'series': 'You Are My Glory',
  286. 'episode': 'Episode 1',
  287. 'episode_number': 1,
  288. 'duration': 2454,
  289. },
  290. }, {
  291. 'url': 'https://wetv.vip/en/play/lcxgwod5hapghvw-WeTV-PICK-A-BOO/i0042y00lxp-Zhao-Lusi-Describes-The-First-Experiences-She-Had-In-Who-Rules-The-World-%7C-WeTV-PICK-A-BOO',
  292. 'md5': '71133f5c2d5d6cad3427e1b010488280',
  293. 'info_dict': {
  294. 'id': 'i0042y00lxp',
  295. 'ext': 'mp4',
  296. 'title': 'md5:f7a0857dbe5fbbe2e7ad630b92b54e6a',
  297. 'description': 'md5:76260cb9cdc0ef76826d7ca9d92fadfa',
  298. 'thumbnail': r're:^https?://[^?#]+lcxgwod5hapghvw',
  299. 'series': 'WeTV PICK-A-BOO',
  300. 'episode': 'Episode 0',
  301. 'episode_number': 0,
  302. 'duration': 442,
  303. },
  304. }]
  305. def _real_extract(self, url):
  306. return self._extract_episode(url)
  307. class WeTvSeriesIE(WeTvBaseIE):
  308. _VALID_URL = WeTvBaseIE._VALID_URL_BASE + r'/(?P<id>\w+)(?:-[^/?#]+)?/?(?:[?#]|$)'
  309. _TESTS = [{
  310. 'url': 'https://wetv.vip/play/air11ooo2rdsdi3-Cute-Programmer',
  311. 'info_dict': {
  312. 'id': 'air11ooo2rdsdi3',
  313. 'title': 'Cute Programmer',
  314. 'description': 'md5:e87beab3bf9f392d6b9e541a63286343',
  315. },
  316. 'playlist_count': 30,
  317. }, {
  318. 'url': 'https://wetv.vip/en/play/u37kgfnfzs73kiu-You-Are-My-Glory',
  319. 'info_dict': {
  320. 'id': 'u37kgfnfzs73kiu',
  321. 'title': 'You Are My Glory',
  322. 'description': 'md5:831363a4c3b4d7615e1f3854be3a123b',
  323. },
  324. 'playlist_count': 32,
  325. }]
  326. def _real_extract(self, url):
  327. return self._extract_series(url, WeTvEpisodeIE)
  328. class IflixBaseIE(WeTvBaseIE):
  329. _VALID_URL_BASE = r'https?://(?:www\.)?iflix\.com/(?:[^?#]+/)?play'
  330. _API_URL = 'https://vplay.iflix.com/getvinfo'
  331. _APP_VERSION = '3.5.57'
  332. _PLATFORM = '330201'
  333. _HOST = 'www.iflix.com'
  334. _REFERER = 'www.iflix.com'
  335. class IflixEpisodeIE(IflixBaseIE):
  336. IE_NAME = 'iflix:episode'
  337. _VALID_URL = IflixBaseIE._VALID_URL_BASE + r'/(?P<series_id>\w+)(?:-[^?#]+)?/(?P<id>\w+)(?:-[^?#]+)?'
  338. _TESTS = [{
  339. 'url': 'https://www.iflix.com/en/play/daijrxu03yypu0s/a0040kvgaza',
  340. 'md5': '9740f9338c3a2105290d16b68fb3262f',
  341. 'info_dict': {
  342. 'id': 'a0040kvgaza',
  343. 'ext': 'mp4',
  344. 'title': 'EP1: Put Your Head On My Shoulder 2021',
  345. 'description': 'md5:c095a742d3b7da6dfedd0c8170727a42',
  346. 'thumbnail': r're:^https?://[^?#]+daijrxu03yypu0s',
  347. 'series': 'Put Your Head On My Shoulder 2021',
  348. 'episode': 'Episode 1',
  349. 'episode_number': 1,
  350. 'duration': 2639,
  351. },
  352. }, {
  353. 'url': 'https://www.iflix.com/en/play/fvvrcc3ra9lbtt1-Take-My-Brother-Away/i0029sd3gm1-EP1%EF%BC%9ATake-My-Brother-Away',
  354. 'md5': '375c9b8478fdedca062274b2c2f53681',
  355. 'info_dict': {
  356. 'id': 'i0029sd3gm1',
  357. 'ext': 'mp4',
  358. 'title': 'EP1:Take My Brother Away',
  359. 'description': 'md5:f0f7be1606af51cd94d5627de96b0c76',
  360. 'thumbnail': r're:^https?://[^?#]+fvvrcc3ra9lbtt1',
  361. 'series': 'Take My Brother Away',
  362. 'episode': 'Episode 1',
  363. 'episode_number': 1,
  364. 'duration': 228,
  365. },
  366. }]
  367. def _real_extract(self, url):
  368. return self._extract_episode(url)
  369. class IflixSeriesIE(IflixBaseIE):
  370. _VALID_URL = IflixBaseIE._VALID_URL_BASE + r'/(?P<id>\w+)(?:-[^/?#]+)?/?(?:[?#]|$)'
  371. _TESTS = [{
  372. 'url': 'https://www.iflix.com/en/play/g21a6qk4u1s9x22-You-Are-My-Hero',
  373. 'info_dict': {
  374. 'id': 'g21a6qk4u1s9x22',
  375. 'title': 'You Are My Hero',
  376. 'description': 'md5:9c4d844bc0799cd3d2b5aed758a2050a',
  377. },
  378. 'playlist_count': 40,
  379. }, {
  380. 'url': 'https://www.iflix.com/play/0s682hc45t0ohll',
  381. 'info_dict': {
  382. 'id': '0s682hc45t0ohll',
  383. 'title': 'Miss Gu Who Is Silent',
  384. 'description': 'md5:a9651d0236f25af06435e845fa2f8c78',
  385. },
  386. 'playlist_count': 20,
  387. }]
  388. def _real_extract(self, url):
  389. return self._extract_series(url, IflixEpisodeIE)