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