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