Unify coding cookie
[youtube-dl] / 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': '6d3ca61a8d0633c9c542b92fcb936b0c',
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': 'e54a254fb8b871968fd8403255f28589',
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         return {
89             'id': video_id,
90             'title': title,
91             'thumbnail': thumbnail,
92             'formats': formats,
93             'description': data.get('web_site_text'),
94             'duration': int_or_none(data.get('duration')),
95             'view_count': int_or_none(data.get('views')),
96         }