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