821c8ec109236b787b9afa2985e450ff8a647595
[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 ..compat import (
10     compat_urllib_request,
11 )
12 from ..utils import (
13     ExtractorError,
14     remove_end,
15 )
16
17
18 class IPrimaIE(InfoExtractor):
19     _VALID_URL = r'https?://play\.iprima\.cz/(?:[^/]+/)*(?P<id>[^?#]+)'
20
21     _TESTS = [{
22         'url': 'http://play.iprima.cz/particka/particka-92',
23         'info_dict': {
24             'id': '39152',
25             'ext': 'flv',
26             'title': 'Partička (92)',
27             'description': 'md5:74e9617e51bca67c3ecfb2c6f9766f45',
28             'thumbnail': 'http://play.iprima.cz/sites/default/files/image_crops/image_620x349/3/491483_particka-92_image_620x349.jpg',
29         },
30         'params': {
31             'skip_download': True,  # requires rtmpdump
32         },
33     }, {
34         'url': 'http://play.iprima.cz/particka/tchibo-particka-jarni-moda',
35         'info_dict': {
36             'id': '9718337',
37             'ext': 'flv',
38             'title': 'Tchibo Partička - Jarní móda',
39             'thumbnail': 're:^http:.*\.jpg$',
40         },
41         'params': {
42             'skip_download': True,  # requires rtmpdump
43         },
44     }, {
45         'url': 'http://play.iprima.cz/zpravy-ftv-prima-2752015',
46         'only_matching': True,
47     }]
48
49     def _real_extract(self, url):
50         mobj = re.match(self._VALID_URL, url)
51         video_id = mobj.group('id')
52
53         webpage = self._download_webpage(url, video_id)
54
55         if re.search(r'Nemáte oprávnění přistupovat na tuto stránku\.\s*</div>', webpage):
56             raise ExtractorError(
57                 '%s said: You do not have permission to access this page' % self.IE_NAME, expected=True)
58
59         player_url = (
60             'http://embed.livebox.cz/iprimaplay/player-embed-v2.js?__tok%s__=%s' %
61             (floor(random() * 1073741824), floor(random() * 1073741824))
62         )
63
64         req = compat_urllib_request.Request(player_url)
65         req.add_header('Referer', url)
66         playerpage = self._download_webpage(req, video_id)
67
68         base_url = ''.join(re.findall(r"embed\['stream'\] = '(.+?)'.+'(\?auth=)'.+'(.+?)';", playerpage)[1])
69
70         zoneGEO = self._html_search_regex(r'"zoneGEO":(.+?),', webpage, 'zoneGEO')
71         if zoneGEO != '0':
72             base_url = base_url.replace('token', 'token_' + zoneGEO)
73
74         formats = []
75         for format_id in ['lq', 'hq', 'hd']:
76             filename = self._html_search_regex(
77                 r'"%s_id":(.+?),' % format_id, webpage, 'filename')
78
79             if filename == 'null':
80                 continue
81
82             real_id = self._search_regex(
83                 r'Prima-(?:[0-9]{10}|WEB)-([0-9]+)[-_]',
84                 filename, 'real video id')
85
86             if format_id == 'lq':
87                 quality = 0
88             elif format_id == 'hq':
89                 quality = 1
90             elif format_id == 'hd':
91                 quality = 2
92                 filename = 'hq/' + filename
93
94             formats.append({
95                 'format_id': format_id,
96                 'url': base_url,
97                 'quality': quality,
98                 'play_path': 'mp4:' + filename.replace('"', '')[:-4],
99                 'rtmp_live': True,
100                 'ext': 'flv',
101             })
102
103         self._sort_formats(formats)
104
105         return {
106             'id': real_id,
107             'title': remove_end(self._og_search_title(webpage), ' | Prima PLAY'),
108             'thumbnail': self._og_search_thumbnail(webpage),
109             'formats': formats,
110             'description': self._search_regex(
111                 r'<p[^>]+itemprop="description"[^>]*>([^<]+)',
112                 webpage, 'description', default=None),
113         }