]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/viewster.py
Merge pull request #6421 from remitamine/vidme
[yt-dlp.git] / youtube_dl / extractor / viewster.py
CommitLineData
b68a2613 1# coding: utf-8
3647136f
S
2from __future__ import unicode_literals
3
4from .common import InfoExtractor
b68a2613
S
5from ..compat import (
6 compat_urllib_request,
7 compat_urllib_parse,
1f048735 8 compat_urllib_parse_unquote,
b68a2613
S
9)
10from ..utils import (
11 determine_ext,
12 int_or_none,
13 parse_iso8601,
14)
3647136f
S
15
16
17class ViewsterIE(InfoExtractor):
b68a2613 18 _VALID_URL = r'http://(?:www\.)?viewster\.com/(?:serie|movie)/(?P<id>\d+-\d+-\d+)'
7be5a62e 19 _TESTS = [{
b68a2613 20 # movie, Type=Movie
7be5a62e 21 'url': 'http://www.viewster.com/movie/1140-11855-000/the-listening-project/',
b68a2613 22 'md5': '14d3cfffe66d57b41ae2d9c873416f01',
7be5a62e
S
23 'info_dict': {
24 'id': '1140-11855-000',
b68a2613
S
25 'ext': 'flv',
26 'title': 'The listening Project',
27 'description': 'md5:bac720244afd1a8ea279864e67baa071',
28 'timestamp': 1214870400,
29 'upload_date': '20080701',
30 'duration': 4680,
31 },
7be5a62e 32 }, {
b68a2613
S
33 # series episode, Type=Episode
34 'url': 'http://www.viewster.com/serie/1284-19427-001/the-world-and-a-wall/',
35 'md5': 'd5434c80fcfdb61651cc2199a88d6ba3',
7be5a62e 36 'info_dict': {
b68a2613
S
37 'id': '1284-19427-001',
38 'ext': 'flv',
39 'title': 'The World and a Wall',
40 'description': 'md5:24814cf74d3453fdf5bfef9716d073e3',
41 'timestamp': 1428192000,
42 'upload_date': '20150405',
43 'duration': 1500,
44 },
45 }, {
46 # serie, Type=Serie
47 'url': 'http://www.viewster.com/serie/1303-19426-000/',
48 'info_dict': {
49 'id': '1303-19426-000',
50 'title': 'Is It Wrong to Try to Pick up Girls in a Dungeon?',
51 'description': 'md5:eeda9bef25b0d524b3a29a97804c2f11',
52 },
53 'playlist_count': 13,
54 }, {
55 # unfinished serie, no Type
56 'url': 'http://www.viewster.com/serie/1284-19427-000/baby-steps-season-2/',
57 'info_dict': {
58 'id': '1284-19427-000',
59 'title': 'Baby Steps—Season 2',
60 'description': 'md5:e7097a8fc97151e25f085c9eb7a1cdb1',
61 },
62 'playlist_mincount': 16,
7be5a62e 63 }]
3647136f
S
64
65 _ACCEPT_HEADER = 'application/json, text/javascript, */*; q=0.01'
66
b68a2613
S
67 def _download_json(self, url, video_id, note='Downloading JSON metadata', fatal=True):
68 request = compat_urllib_request.Request(url)
3647136f 69 request.add_header('Accept', self._ACCEPT_HEADER)
b68a2613
S
70 request.add_header('Auth-token', self._AUTH_TOKEN)
71 return super(ViewsterIE, self)._download_json(request, video_id, note, fatal=fatal)
3647136f 72
b68a2613
S
73 def _real_extract(self, url):
74 video_id = self._match_id(url)
799207e8 75 # Get 'api_token' cookie
76 self._request_webpage(url, video_id)
77 cookies = self._get_cookies(url)
1f048735 78 self._AUTH_TOKEN = compat_urllib_parse_unquote(cookies['api_token'].value)
7be5a62e 79
b68a2613
S
80 info = self._download_json(
81 'https://public-api.viewster.com/search/%s' % video_id,
82 video_id, 'Downloading entry JSON')
7be5a62e 83
b68a2613 84 entry_id = info.get('Id') or info['id']
7be5a62e 85
b68a2613
S
86 # unfinished serie has no Type
87 if info.get('Type') in ['Serie', None]:
88 episodes = self._download_json(
89 'https://public-api.viewster.com/series/%s/episodes' % entry_id,
90 video_id, 'Downloading series JSON')
91 entries = [
92 self.url_result(
93 'http://www.viewster.com/movie/%s' % episode['OriginId'], 'Viewster')
94 for episode in episodes]
c84683c8 95 title = (info.get('Title') or info['Synopsis']['Title']).strip()
b68a2613
S
96 description = info.get('Synopsis', {}).get('Detailed')
97 return self.playlist_result(entries, video_id, title, description)
7be5a62e 98
b68a2613
S
99 formats = []
100 for media_type in ('application/f4m+xml', 'application/x-mpegURL'):
101 media = self._download_json(
102 'https://public-api.viewster.com/movies/%s/video?mediaType=%s'
103 % (entry_id, compat_urllib_parse.quote(media_type)),
104 video_id, 'Downloading %s JSON' % media_type, fatal=False)
105 if not media:
106 continue
107 video_url = media.get('Uri')
108 if not video_url:
109 continue
110 ext = determine_ext(video_url)
111 if ext == 'f4m':
112 video_url += '&' if '?' in video_url else '?'
113 video_url += 'hdcore=3.2.0&plugin=flowplayer-3.2.0.1'
114 formats.extend(self._extract_f4m_formats(
115 video_url, video_id, f4m_id='hds'))
116 elif ext == 'm3u8':
117 formats.extend(self._extract_m3u8_formats(
118 video_url, video_id, 'mp4', m3u8_id='hls',
119 fatal=False # m3u8 sometimes fail
120 ))
121 else:
122 formats.append({
123 'url': video_url,
7be5a62e 124 })
b68a2613 125 self._sort_formats(formats)
7be5a62e 126
b68a2613
S
127 synopsis = info.get('Synopsis', {})
128 # Prefer title outside synopsis since it's less messy
c84683c8 129 title = (info.get('Title') or synopsis['Title']).strip()
b68a2613
S
130 description = synopsis.get('Detailed') or info.get('Synopsis', {}).get('Short')
131 duration = int_or_none(info.get('Duration'))
132 timestamp = parse_iso8601(info.get('ReleaseDate'))
133
134 return {
135 'id': video_id,
136 'title': title,
137 'description': description,
138 'timestamp': timestamp,
139 'duration': duration,
140 'formats': formats,
141 }