]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/litv.py
[cleanup, docs] Misc cleanup
[yt-dlp.git] / yt_dlp / extractor / litv.py
CommitLineData
3b01a9fb
YCH
1# coding: utf-8
2from __future__ import unicode_literals
3
4import json
3b01a9fb
YCH
5
6from .common import InfoExtractor
7from ..utils import (
8 ExtractorError,
9 int_or_none,
f40ee5e9 10 traverse_obj,
3b01a9fb
YCH
11 smuggle_url,
12 unsmuggle_url,
13)
14
15
16class LiTVIE(InfoExtractor):
92519402 17 _VALID_URL = r'https?://(?:www\.)?litv\.tv/(?:vod|promo)/[^/]+/(?:content\.do)?\?.*?\b(?:content_)?id=(?P<id>[^&]+)'
3b01a9fb
YCH
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',
39e1c4f0 30 'md5': '969e343d9244778cb29acec608e53640',
3b01a9fb
YCH
31 'info_dict': {
32 'id': 'VOD00041610',
33 'ext': 'mp4',
34 'title': '花千骨第1集',
ec85ded8 35 'thumbnail': r're:https?://.*\.jpg$',
3b01a9fb
YCH
36 'description': 'md5:c7017aa144c87467c4fb2909c4b05d6f',
37 'episode_number': 1,
38 },
39 'params': {
40 'noplaylist': True,
39e1c4f0
YCH
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': '楚威王二年,太史令唐昧夜觀星象,發現霸星即將現世。王后得知霸星的預言後,想盡辦法不讓孩子順利出生,幸得莒姬相護化解危機。沒想到眾人期待下出生的霸星卻是位公主,楚威王對此失望至極。楚王后命人將女嬰丟棄河中,居然奇蹟似的被少司命像攔下,楚威王認為此女非同凡響,為她取名芈月。',
3b01a9fb
YCH
51 },
52 'skip': 'Georestricted to Taiwan',
53 }]
54
9dde0e04
YCH
55 def _extract_playlist(self, season_list, video_id, program_info, prompt=True):
56 episode_title = program_info['title']
3b01a9fb
YCH
57 content_id = season_list['contentId']
58
3b01a9fb
YCH
59 all_episodes = [
60 self.url_result(smuggle_url(
9dde0e04 61 self._URL_TEMPLATE % (program_info['contentType'], episode['contentId']),
3b01a9fb
YCH
62 {'force_noplaylist': True})) # To prevent infinite recursion
63 for episode in season_list['episode']]
64
65 return self.playlist_result(all_episodes, content_id, episode_title)
66
67 def _real_extract(self, url):
88f23a18 68 url, smuggled_data = unsmuggle_url(url, {})
3b01a9fb
YCH
69
70 video_id = self._match_id(url)
71
3b01a9fb
YCH
72 webpage = self._download_webpage(url, video_id)
73
9dde0e04 74 program_info = self._parse_json(self._search_regex(
ec85ded8 75 r'var\s+programInfo\s*=\s*([^;]+)', webpage, 'VOD data', default='{}'),
3b01a9fb
YCH
76 video_id)
77
9dde0e04 78 season_list = list(program_info.get('seasonList', {}).values())
f40ee5e9 79 playlist_id = traverse_obj(season_list, 0, 'contentId')
80 if self._yes_playlist(playlist_id, video_id, smuggled_data):
81 return self._extract_playlist(season_list[0], video_id, program_info)
3b01a9fb
YCH
82
83 # In browsers `getMainUrl` request is always issued. Usually this
84 # endpoint gives the same result as the data embedded in the webpage.
85 # If georestricted, there are no embedded data, so an extra request is
86 # necessary to get the error code
9dde0e04
YCH
87 if 'assetId' not in program_info:
88 program_info = self._download_json(
39e1c4f0
YCH
89 'https://www.litv.tv/vod/ajax/getProgramInfo', video_id,
90 query={'contentId': video_id},
91 headers={'Accept': 'application/json'})
3b01a9fb
YCH
92 video_data = self._parse_json(self._search_regex(
93 r'uiHlsUrl\s*=\s*testBackendData\(([^;]+)\);',
94 webpage, 'video data', default='{}'), video_id)
95 if not video_data:
96 payload = {
9dde0e04
YCH
97 'assetId': program_info['assetId'],
98 'watchDevices': program_info['watchDevices'],
99 'contentType': program_info['contentType'],
3b01a9fb
YCH
100 }
101 video_data = self._download_json(
102 'https://www.litv.tv/vod/getMainUrl', video_id,
103 data=json.dumps(payload).encode('utf-8'),
104 headers={'Content-Type': 'application/json'})
105
106 if not video_data.get('fullpath'):
107 error_msg = video_data.get('errorMessage')
108 if error_msg == 'vod.error.outsideregionerror':
109 self.raise_geo_restricted('This video is available in Taiwan only')
110 if error_msg:
111 raise ExtractorError('%s said: %s' % (self.IE_NAME, error_msg), expected=True)
112 raise ExtractorError('Unexpected result from %s' % self.IE_NAME)
113
114 formats = self._extract_m3u8_formats(
39e1c4f0
YCH
115 video_data['fullpath'], video_id, ext='mp4',
116 entry_protocol='m3u8_native', m3u8_id='hls')
3b01a9fb
YCH
117 for a_format in formats:
118 # LiTV HLS segments doesn't like compressions
119 a_format.setdefault('http_headers', {})['Youtubedl-no-compression'] = True
120
9dde0e04
YCH
121 title = program_info['title'] + program_info.get('secondaryMark', '')
122 description = program_info.get('description')
123 thumbnail = program_info.get('imageFile')
124 categories = [item['name'] for item in program_info.get('category', [])]
125 episode = int_or_none(program_info.get('episode'))
3b01a9fb
YCH
126
127 return {
128 'id': video_id,
129 'formats': formats,
130 'title': title,
131 'description': description,
132 'thumbnail': thumbnail,
133 'categories': categories,
134 'episode_number': episode,
135 }