]> jfr.im git - yt-dlp.git/blob - youtube_dl/extractor/litv.py
[litv] Support 'promo' URLs (closes #10385)
[yt-dlp.git] / youtube_dl / extractor / litv.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import json
5 import re
6
7 from .common import InfoExtractor
8 from ..utils import (
9 ExtractorError,
10 int_or_none,
11 smuggle_url,
12 unsmuggle_url,
13 )
14
15
16 class LiTVIE(InfoExtractor):
17 _VALID_URL = r'https?://www\.litv\.tv/(?:vod|promo)/[^/]+/(?:content\.do)?\?.*?\b(?:content_)?id=(?P<id>[^&]+)'
18
19 _URL_TEMPLATE = 'https://www.litv.tv/vod/%s/content.do?id=%s'
20
21 _TESTS = [{
22 'url': 'https://www.litv.tv/vod/drama/content.do?brc_id=root&id=VOD00041610&isUHEnabled=true&autoPlay=1',
23 'info_dict': {
24 'id': 'VOD00041606',
25 'title': '花千骨',
26 },
27 'playlist_count': 50,
28 }, {
29 'url': 'https://www.litv.tv/vod/drama/content.do?brc_id=root&id=VOD00041610&isUHEnabled=true&autoPlay=1',
30 'md5': '969e343d9244778cb29acec608e53640',
31 'info_dict': {
32 'id': 'VOD00041610',
33 'ext': 'mp4',
34 'title': '花千骨第1集',
35 'thumbnail': 're:https?://.*\.jpg$',
36 'description': 'md5:c7017aa144c87467c4fb2909c4b05d6f',
37 'episode_number': 1,
38 },
39 'params': {
40 'noplaylist': True,
41 },
42 'skip': 'Georestricted to Taiwan',
43 }, {
44 'url': 'https://www.litv.tv/promo/miyuezhuan/?content_id=VOD00044841&',
45 'md5': '88322ea132f848d6e3e18b32a832b918',
46 'info_dict': {
47 'id': 'VOD00044841',
48 'ext': 'mp4',
49 'title': '芈月傳第1集 霸星芈月降世楚國',
50 'description': '楚威王二年,太史令唐昧夜觀星象,發現霸星即將現世。王后得知霸星的預言後,想盡辦法不讓孩子順利出生,幸得莒姬相護化解危機。沒想到眾人期待下出生的霸星卻是位公主,楚威王對此失望至極。楚王后命人將女嬰丟棄河中,居然奇蹟似的被少司命像攔下,楚威王認為此女非同凡響,為她取名芈月。',
51 },
52 'skip': 'Georestricted to Taiwan',
53 }]
54
55 def _extract_playlist(self, season_list, video_id, vod_data, view_data, prompt=True):
56 episode_title = view_data['title']
57 content_id = season_list['contentId']
58
59 if prompt:
60 self.to_screen('Downloading playlist %s - add --no-playlist to just download video %s' % (content_id, video_id))
61
62 all_episodes = [
63 self.url_result(smuggle_url(
64 self._URL_TEMPLATE % (view_data['contentType'], episode['contentId']),
65 {'force_noplaylist': True})) # To prevent infinite recursion
66 for episode in season_list['episode']]
67
68 return self.playlist_result(all_episodes, content_id, episode_title)
69
70 def _real_extract(self, url):
71 url, data = unsmuggle_url(url, {})
72
73 video_id = self._match_id(url)
74
75 noplaylist = self._downloader.params.get('noplaylist')
76 noplaylist_prompt = True
77 if 'force_noplaylist' in data:
78 noplaylist = data['force_noplaylist']
79 noplaylist_prompt = False
80
81 webpage = self._download_webpage(url, video_id)
82
83 view_data = dict(map(lambda t: (t[0], t[2]), re.findall(
84 r'viewData\.([a-zA-Z]+)\s*=\s*(["\'])([^"\']+)\2',
85 webpage)))
86
87 vod_data = self._parse_json(self._search_regex(
88 'var\s+vod\s*=\s*([^;]+)', webpage, 'VOD data', default='{}'),
89 video_id)
90
91 season_list = list(vod_data.get('seasonList', {}).values())
92 if season_list:
93 if not noplaylist:
94 return self._extract_playlist(
95 season_list[0], video_id, vod_data, view_data,
96 prompt=noplaylist_prompt)
97
98 if noplaylist_prompt:
99 self.to_screen('Downloading just video %s because of --no-playlist' % video_id)
100
101 # In browsers `getMainUrl` request is always issued. Usually this
102 # endpoint gives the same result as the data embedded in the webpage.
103 # If georestricted, there are no embedded data, so an extra request is
104 # necessary to get the error code
105 if 'assetId' not in view_data:
106 view_data = self._download_json(
107 'https://www.litv.tv/vod/ajax/getProgramInfo', video_id,
108 query={'contentId': video_id},
109 headers={'Accept': 'application/json'})
110 video_data = self._parse_json(self._search_regex(
111 r'uiHlsUrl\s*=\s*testBackendData\(([^;]+)\);',
112 webpage, 'video data', default='{}'), video_id)
113 if not video_data:
114 payload = {
115 'assetId': view_data['assetId'],
116 'watchDevices': view_data['watchDevices'],
117 'contentType': view_data['contentType'],
118 }
119 video_data = self._download_json(
120 'https://www.litv.tv/vod/getMainUrl', video_id,
121 data=json.dumps(payload).encode('utf-8'),
122 headers={'Content-Type': 'application/json'})
123
124 if not video_data.get('fullpath'):
125 error_msg = video_data.get('errorMessage')
126 if error_msg == 'vod.error.outsideregionerror':
127 self.raise_geo_restricted('This video is available in Taiwan only')
128 if error_msg:
129 raise ExtractorError('%s said: %s' % (self.IE_NAME, error_msg), expected=True)
130 raise ExtractorError('Unexpected result from %s' % self.IE_NAME)
131
132 formats = self._extract_m3u8_formats(
133 video_data['fullpath'], video_id, ext='mp4',
134 entry_protocol='m3u8_native', m3u8_id='hls')
135 for a_format in formats:
136 # LiTV HLS segments doesn't like compressions
137 a_format.setdefault('http_headers', {})['Youtubedl-no-compression'] = True
138
139 title = view_data['title'] + view_data.get('secondaryMark', '')
140 description = view_data.get('description')
141 thumbnail = view_data.get('imageFile')
142 categories = [item['name'] for item in vod_data.get('category', [])]
143 episode = int_or_none(view_data.get('episode'))
144
145 return {
146 'id': video_id,
147 'formats': formats,
148 'title': title,
149 'description': description,
150 'thumbnail': thumbnail,
151 'categories': categories,
152 'episode_number': episode,
153 }