]> gitweb @ CieloNegro.org - youtube-dl.git/blob - youtube_dl/extractor/streamcz.py
[viddler] Modernize
[youtube-dl.git] / youtube_dl / extractor / streamcz.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import hashlib
5 import time
6
7 from .common import InfoExtractor
8 from ..utils import (
9     int_or_none,
10     sanitized_Request,
11 )
12
13
14 def _get_api_key(api_path):
15     if api_path.endswith('?'):
16         api_path = api_path[:-1]
17
18     api_key = 'fb5f58a820353bd7095de526253c14fd'
19     a = '{0:}{1:}{2:}'.format(api_key, api_path, int(round(time.time() / 24 / 3600)))
20     return hashlib.md5(a.encode('ascii')).hexdigest()
21
22
23 class StreamCZIE(InfoExtractor):
24     _VALID_URL = r'https?://(?:www\.)?stream\.cz/.+/(?P<id>[0-9]+)'
25     _API_URL = 'http://www.stream.cz/API'
26
27     _TESTS = [{
28         'url': 'http://www.stream.cz/peklonataliri/765767-ecka-pro-deti',
29         'md5': '934bb6a6d220d99c010783c9719960d5',
30         'info_dict': {
31             'id': '765767',
32             'ext': 'mp4',
33             'title': 'Peklo na talíři: Éčka pro děti',
34             'description': 'Taška s grónskou pomazánkou a další pekelnosti ZDE',
35             'thumbnail': 're:^http://im.stream.cz/episode/52961d7e19d423f8f06f0100',
36             'duration': 256,
37         },
38     }, {
39         'url': 'http://www.stream.cz/blanik/10002447-tri-roky-pro-mazanka',
40         'md5': '849a88c1e1ca47d41403c2ba5e59e261',
41         'info_dict': {
42             'id': '10002447',
43             'ext': 'mp4',
44             'title': 'Kancelář Blaník: Tři roky pro Mazánka',
45             'description': 'md5:3862a00ba7bf0b3e44806b544032c859',
46             'thumbnail': 're:^http://im.stream.cz/episode/537f838c50c11f8d21320000',
47             'duration': 368,
48         },
49     }]
50
51     def _real_extract(self, url):
52         video_id = self._match_id(url)
53         api_path = '/episode/%s' % video_id
54
55         req = sanitized_Request(self._API_URL + api_path)
56         req.add_header('Api-Password', _get_api_key(api_path))
57         data = self._download_json(req, video_id)
58
59         formats = []
60         for quality, video in enumerate(data['video_qualities']):
61             for f in video['formats']:
62                 typ = f['type'].partition('/')[2]
63                 qlabel = video.get('quality_label')
64                 formats.append({
65                     'format_note': '%s-%s' % (qlabel, typ) if qlabel else typ,
66                     'format_id': '%s-%s' % (typ, f['quality']),
67                     'url': f['source'],
68                     'height': int_or_none(f['quality'].rstrip('p')),
69                     'quality': quality,
70                 })
71         self._sort_formats(formats)
72
73         image = data.get('image')
74         if image:
75             thumbnail = self._proto_relative_url(
76                 image.replace('{width}', '1240').replace('{height}', '697'),
77                 scheme='http:',
78             )
79         else:
80             thumbnail = None
81
82         stream = data.get('_embedded', {}).get('stream:show', {}).get('name')
83         if stream:
84             title = '%s: %s' % (stream, data['name'])
85         else:
86             title = data['name']
87
88         subtitles = {}
89         srt_url = data.get('subtitles_srt')
90         if srt_url:
91             subtitles['cs'] = [{
92                 'ext': 'srt',
93                 'url': srt_url,
94             }]
95
96         return {
97             'id': video_id,
98             'title': title,
99             'thumbnail': thumbnail,
100             'formats': formats,
101             'description': data.get('web_site_text'),
102             'duration': int_or_none(data.get('duration')),
103             'view_count': int_or_none(data.get('views')),
104             'subtitles': subtitles,
105         }