2 from __future__ import unicode_literals
6 from .common import InfoExtractor
10 compat_urllib_parse_unquote,
23 class ViewsterIE(InfoExtractor):
24 _VALID_URL = r'https?://(?:www\.)?viewster\.com/(?:serie|movie)/(?P<id>\d+-\d+-\d+)'
27 'url': 'http://www.viewster.com/movie/1140-11855-000/the-listening-project/',
28 'md5': 'e642d1b27fcf3a4ffa79f194f5adde36',
30 'id': '1140-11855-000',
32 'title': 'The listening Project',
33 'description': 'md5:bac720244afd1a8ea279864e67baa071',
34 'timestamp': 1214870400,
35 'upload_date': '20080701',
39 # series episode, Type=Episode
40 'url': 'http://www.viewster.com/serie/1284-19427-001/the-world-and-a-wall/',
41 'md5': '9243079a8531809efe1b089db102c069',
43 'id': '1284-19427-001',
45 'title': 'The World and a Wall',
46 'description': 'md5:24814cf74d3453fdf5bfef9716d073e3',
47 'timestamp': 1428192000,
48 'upload_date': '20150405',
53 'url': 'http://www.viewster.com/serie/1303-19426-000/',
55 'id': '1303-19426-000',
56 'title': 'Is It Wrong to Try to Pick up Girls in a Dungeon?',
57 'description': 'md5:eeda9bef25b0d524b3a29a97804c2f11',
61 # unfinished serie, no Type
62 'url': 'http://www.viewster.com/serie/1284-19427-000/baby-steps-season-2/',
64 'id': '1284-19427-000',
65 'title': 'Baby Steps—Season 2',
66 'description': 'md5:e7097a8fc97151e25f085c9eb7a1cdb1',
68 'playlist_mincount': 16,
70 # geo restricted series
71 'url': 'https://www.viewster.com/serie/1280-18794-002/',
72 'only_matching': True,
74 # geo restricted video
75 'url': 'https://www.viewster.com/serie/1280-18794-002/what-is-extraterritoriality-lawo/',
76 'only_matching': True,
79 _ACCEPT_HEADER = 'application/json, text/javascript, */*; q=0.01'
81 def _download_json(self, url, video_id, note='Downloading JSON metadata', fatal=True):
82 request = sanitized_Request(url)
83 request.add_header('Accept', self._ACCEPT_HEADER)
84 request.add_header('Auth-token', self._AUTH_TOKEN)
85 return super(ViewsterIE, self)._download_json(request, video_id, note, fatal=fatal)
87 def _real_extract(self, url):
88 video_id = self._match_id(url)
89 # Get 'api_token' cookie
90 self._request_webpage(HEADRequest('http://www.viewster.com/'), video_id)
91 cookies = self._get_cookies('http://www.viewster.com/')
92 self._AUTH_TOKEN = compat_urllib_parse_unquote(cookies['api_token'].value)
94 info = self._download_json(
95 'https://public-api.viewster.com/search/%s' % video_id,
96 video_id, 'Downloading entry JSON')
98 entry_id = info.get('Id') or info['id']
100 # unfinished serie has no Type
101 if info.get('Type') in ('Serie', None):
103 episodes = self._download_json(
104 'https://public-api.viewster.com/series/%s/episodes' % entry_id,
105 video_id, 'Downloading series JSON')
106 except ExtractorError as e:
107 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 404:
108 self.raise_geo_restricted()
113 'http://www.viewster.com/movie/%s' % episode['OriginId'], 'Viewster')
114 for episode in episodes]
115 title = (info.get('Title') or info['Synopsis']['Title']).strip()
116 description = info.get('Synopsis', {}).get('Detailed')
117 return self.playlist_result(entries, video_id, title, description)
122 for media_type in ('application/f4m+xml', 'application/x-mpegURL', 'video/mp4'):
123 media = self._download_json(
124 'https://public-api.viewster.com/movies/%s/video?mediaType=%s'
125 % (entry_id, compat_urllib_parse.quote(media_type)),
126 video_id, 'Downloading %s JSON' % media_type, fatal=False)
129 video_url = media.get('Uri')
132 ext = determine_ext(video_url)
134 manifest_url = video_url
135 video_url += '&' if '?' in video_url else '?'
136 video_url += 'hdcore=3.2.0&plugin=flowplayer-3.2.0.1'
137 formats.extend(self._extract_f4m_formats(
138 video_url, video_id, f4m_id='hds'))
140 manifest_url = video_url
141 m3u8_formats = self._extract_m3u8_formats(
142 video_url, video_id, 'mp4', m3u8_id='hls',
143 fatal=False) # m3u8 sometimes fail
145 formats.extend(m3u8_formats)
147 qualities_basename = self._search_regex(
149 manifest_url, 'qualities basename', default=None)
150 if not qualities_basename:
152 QUALITIES_RE = r'((,\d+k)+,?)'
153 qualities = self._search_regex(
154 QUALITIES_RE, qualities_basename,
155 'qualities', default=None)
158 qualities = list(map(lambda q: int(q[:-1]), qualities.strip(',').split(',')))
160 http_template = re.sub(QUALITIES_RE, r'%dk', qualities_basename)
161 http_url_basename = url_basename(video_url)
163 self._sort_formats(m3u8_formats)
164 m3u8_formats = list(filter(
165 lambda f: f.get('vcodec') != 'none' and f.get('resolution') != 'multiple',
167 if len(qualities) == len(m3u8_formats):
168 for q, m3u8_format in zip(qualities, m3u8_formats):
169 f = m3u8_format.copy()
171 'url': video_url.replace(http_url_basename, http_template % q),
172 'format_id': f['format_id'].replace('hls', 'http'),
179 'url': video_url.replace(http_url_basename, http_template % q),
181 'format_id': 'http-%d' % q,
185 if not formats and not info.get('LanguageSets') and not info.get('VODSettings'):
186 self.raise_geo_restricted()
188 self._sort_formats(formats)
190 synopsis = info.get('Synopsis') or {}
191 # Prefer title outside synopsis since it's less messy
192 title = (info.get('Title') or synopsis['Title']).strip()
193 description = synopsis.get('Detailed') or (info.get('Synopsis') or {}).get('Short')
194 duration = int_or_none(info.get('Duration'))
195 timestamp = parse_iso8601(info.get('ReleaseDate'))
200 'description': description,
201 'timestamp': timestamp,
202 'duration': duration,