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