logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

tencent.py (19887B)


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