Merge pull request #2979 from pulpe/streamcz_fix
[youtube-dl] / youtube_dl / extractor / streamcz.py
1 # -*- coding: utf-8 -*-
2 from __future__ import unicode_literals
3
4 import re
5 import json
6
7 from .common import InfoExtractor
8 from ..utils import int_or_none
9
10
11 class StreamCZIE(InfoExtractor):
12     _VALID_URL = r'https?://(?:www\.)?stream\.cz/.+/(?P<videoid>.+)'
13
14     _TESTS = [{
15         'url': 'http://www.stream.cz/peklonataliri/765767-ecka-pro-deti',
16         'md5': '6d3ca61a8d0633c9c542b92fcb936b0c',
17         'info_dict': {
18             'id': '765767',
19             'ext': 'mp4',
20             'title': 'Peklo na talíři: Éčka pro děti',
21             'description': 'md5:49ace0df986e95e331d0fe239d421519',
22             'thumbnail': 'http://im.stream.cz/episode/52961d7e19d423f8f06f0100',
23             'duration': 256,
24         },
25     }, {
26         'url': 'https://www.stream.cz/blanik/10002447-tri-roky-pro-mazanka',
27         'md5': '246272e753e26bbace7fcd9deca0650c',
28         'info_dict': {
29             'id': '10002447',
30             'ext': 'mp4',
31             'title': 'Kancelář Blaník: Tři roky pro Mazánka',
32             'description': 'md5:9177695a8b756a0a8ab160de4043b392',
33             'thumbnail': 'http://im.stream.cz/episode/537f838c50c11f8d21320000',
34             'duration': 368,
35         },
36     }]
37
38     def _real_extract(self, url):
39         mobj = re.match(self._VALID_URL, url)
40         video_id = mobj.group('videoid')
41
42         webpage = self._download_webpage(url, video_id)
43
44         data = self._html_search_regex(r'Stream\.Data\.Episode\((.+?)\);', webpage, 'stream data')
45
46         jsonData = json.loads(data)
47
48         formats = []
49         for video in jsonData['instances']:
50             for video_format in video['instances']:
51                 format_id = video_format['quality']
52
53                 if format_id == '240p':
54                     quality = 0
55                 elif format_id == '360p':
56                     quality = 1
57                 elif format_id == '480p':
58                     quality = 2
59                 elif format_id == '720p':
60                     quality = 3
61
62                 formats.append({
63                     'format_id': '%s-%s' % (video_format['type'].split('/')[1], format_id),
64                     'url': video_format['source'],
65                     'quality': quality,
66                 })
67
68         self._sort_formats(formats)
69
70         return {
71             'id': str(jsonData['episode_id']),
72             'title': self._og_search_title(webpage),
73             'thumbnail': jsonData['episode_image_original_url'].replace('//', 'http://'),
74             'formats': formats,
75             'description': self._og_search_description(webpage),
76             'duration': int_or_none(jsonData['duration']),
77             'view_count': int_or_none(jsonData['stats_total']),
78         }