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