]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/litv.py
3356d015d9f6876ced6b86734fb436fd5ef1d4d0
[youtubedl] / 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/[^/]+/content\.do\?.*?\bid=(?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 'info_dict': {
31 'id': 'VOD00041610',
32 'ext': 'mp4',
33 'title': '花千骨第1集',
34 'thumbnail': 're:https?://.*\.jpg$',
35 'description': 'md5:c7017aa144c87467c4fb2909c4b05d6f',
36 'episode_number': 1,
37 },
38 'params': {
39 'noplaylist': True,
40 'skip_download': True, # m3u8 download
41 },
42 'skip': 'Georestricted to Taiwan',
43 }]
44
45 def _extract_playlist(self, season_list, video_id, vod_data, view_data, prompt=True):
46 episode_title = view_data['title']
47 content_id = season_list['contentId']
48
49 if prompt:
50 self.to_screen('Downloading playlist %s - add --no-playlist to just download video %s' % (content_id, video_id))
51
52 all_episodes = [
53 self.url_result(smuggle_url(
54 self._URL_TEMPLATE % (view_data['contentType'], episode['contentId']),
55 {'force_noplaylist': True})) # To prevent infinite recursion
56 for episode in season_list['episode']]
57
58 return self.playlist_result(all_episodes, content_id, episode_title)
59
60 def _real_extract(self, url):
61 url, data = unsmuggle_url(url, {})
62
63 video_id = self._match_id(url)
64
65 noplaylist = self._downloader.params.get('noplaylist')
66 noplaylist_prompt = True
67 if 'force_noplaylist' in data:
68 noplaylist = data['force_noplaylist']
69 noplaylist_prompt = False
70
71 webpage = self._download_webpage(url, video_id)
72
73 view_data = dict(map(lambda t: (t[0], t[2]), re.findall(
74 r'viewData\.([a-zA-Z]+)\s*=\s*(["\'])([^"\']+)\2',
75 webpage)))
76
77 vod_data = self._parse_json(self._search_regex(
78 'var\s+vod\s*=\s*([^;]+)', webpage, 'VOD data', default='{}'),
79 video_id)
80
81 season_list = list(vod_data.get('seasonList', {}).values())
82 if season_list:
83 if not noplaylist:
84 return self._extract_playlist(
85 season_list[0], video_id, vod_data, view_data,
86 prompt=noplaylist_prompt)
87
88 if noplaylist_prompt:
89 self.to_screen('Downloading just video %s because of --no-playlist' % video_id)
90
91 # In browsers `getMainUrl` request is always issued. Usually this
92 # endpoint gives the same result as the data embedded in the webpage.
93 # If georestricted, there are no embedded data, so an extra request is
94 # necessary to get the error code
95 video_data = self._parse_json(self._search_regex(
96 r'uiHlsUrl\s*=\s*testBackendData\(([^;]+)\);',
97 webpage, 'video data', default='{}'), video_id)
98 if not video_data:
99 payload = {
100 'assetId': view_data['assetId'],
101 'watchDevices': vod_data['watchDevices'],
102 'contentType': view_data['contentType'],
103 }
104 video_data = self._download_json(
105 'https://www.litv.tv/vod/getMainUrl', video_id,
106 data=json.dumps(payload).encode('utf-8'),
107 headers={'Content-Type': 'application/json'})
108
109 if not video_data.get('fullpath'):
110 error_msg = video_data.get('errorMessage')
111 if error_msg == 'vod.error.outsideregionerror':
112 self.raise_geo_restricted('This video is available in Taiwan only')
113 if error_msg:
114 raise ExtractorError('%s said: %s' % (self.IE_NAME, error_msg), expected=True)
115 raise ExtractorError('Unexpected result from %s' % self.IE_NAME)
116
117 formats = self._extract_m3u8_formats(
118 video_data['fullpath'], video_id, ext='mp4', m3u8_id='hls')
119 for a_format in formats:
120 # LiTV HLS segments doesn't like compressions
121 a_format.setdefault('http_headers', {})['Youtubedl-no-compression'] = True
122
123 title = view_data['title'] + view_data.get('secondaryMark', '')
124 description = view_data.get('description')
125 thumbnail = view_data.get('imageFile')
126 categories = [item['name'] for item in vod_data.get('category', [])]
127 episode = int_or_none(view_data.get('episode'))
128
129 return {
130 'id': video_id,
131 'formats': formats,
132 'title': title,
133 'description': description,
134 'thumbnail': thumbnail,
135 'categories': categories,
136 'episode_number': episode,
137 }