7956e7624db36d74cd4120c1764d0190f8d1dff5
[youtube-dl] / youtube_dl / extractor / iprima.py
1 # -*- coding: utf-8 -*-
2 from __future__ import unicode_literals
3
4 import re
5 from random import random
6 from math import floor
7
8 from .common import InfoExtractor
9 from ..utils import compat_urllib_request
10
11
12 class IPrimaIE(InfoExtractor):
13     _VALID_URL = r'https?://play\.iprima\.cz/[^?#]+/(?P<id>[^?#]+)'
14
15     _TESTS = [{
16         'url': 'http://play.iprima.cz/particka/particka-92',
17         'info_dict': {
18             'id': '39152',
19             'ext': 'flv',
20             'title': 'Partička (92)',
21             'description': 'md5:3740fda51464da35a2d4d0670b8e4fd6',
22             'thumbnail': 'http://play.iprima.cz/sites/default/files/image_crops/image_620x349/3/491483_particka-92_image_620x349.jpg',
23         },
24         'params': {
25             'skip_download': True,  # requires rtmpdump
26         },
27     }, {
28         'url': 'http://play.iprima.cz/particka/tchibo-particka-jarni-moda',
29         'info_dict': {
30             'id': '9718337',
31             'ext': 'flv',
32             'title': 'Tchibo Partička - Jarní móda',
33             'description': 'md5:589f8f59f414220621ff8882eb3ce7be',
34             'thumbnail': 're:^http:.*\.jpg$',
35         },
36         'params': {
37             'skip_download': True,  # requires rtmpdump
38         },
39     }]
40
41     def _real_extract(self, url):
42         mobj = re.match(self._VALID_URL, url)
43         video_id = mobj.group('id')
44
45         webpage = self._download_webpage(url, video_id)
46
47         player_url = (
48             'http://embed.livebox.cz/iprimaplay/player-embed-v2.js?__tok%s__=%s' %
49             (floor(random()*1073741824), floor(random()*1073741824))
50         )
51
52         req = compat_urllib_request.Request(player_url)
53         req.add_header('Referer', url)
54         playerpage = self._download_webpage(req, video_id)
55
56         base_url = ''.join(re.findall(r"embed\['stream'\] = '(.+?)'.+'(\?auth=)'.+'(.+?)';", playerpage)[1])
57
58         zoneGEO = self._html_search_regex(r'"zoneGEO":(.+?),', webpage, 'zoneGEO')
59         if zoneGEO != '0':
60             base_url = base_url.replace('token', 'token_' + zoneGEO)
61
62         formats = []
63         for format_id in ['lq', 'hq', 'hd']:
64             filename = self._html_search_regex(
65                 r'"%s_id":(.+?),' % format_id, webpage, 'filename')
66
67             if filename == 'null':
68                 continue
69
70             real_id = self._search_regex(
71                 r'Prima-(?:[0-9]{10}|WEB)-([0-9]+)[-_]',
72                 filename, 'real video id')
73
74             if format_id == 'lq':
75                 quality = 0
76             elif format_id == 'hq':
77                 quality = 1
78             elif format_id == 'hd':
79                 quality = 2
80                 filename = 'hq/' + filename
81
82             formats.append({
83                 'format_id': format_id,
84                 'url': base_url,
85                 'quality': quality,
86                 'play_path': 'mp4:' + filename.replace('"', '')[:-4],
87                 'rtmp_live': True,
88                 'ext': 'flv',
89             })
90
91         self._sort_formats(formats)
92
93         return {
94             'id': real_id,
95             'title': self._og_search_title(webpage),
96             'thumbnail': self._og_search_thumbnail(webpage),
97             'formats': formats,
98             'description': self._og_search_description(webpage),
99         }