[iprima] Comply with review
[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:db00b9bc10ffd26fb148fa6a3a67c40b',
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             'description': 'md5:db00b9bc10ffd26fb148fa6a3a67c40b',
40             'thumbnail': 're:^http:.*\.jpg$',
41         },
42         'params': {
43             'skip_download': True,  # requires rtmpdump
44         },
45         'skip': 'Do not have permission to access this page',
46     }, {
47         'url': 'http://play.iprima.cz/zpravy-ftv-prima-2752015',
48         'only_matching': True,
49     }]
50
51     def _real_extract(self, url):
52         mobj = re.match(self._VALID_URL, url)
53         video_id = mobj.group('id')
54
55         webpage = self._download_webpage(url, video_id)
56
57         if re.search(r'Nemáte oprávnění přistupovat na tuto stránku\.\s*</div>', webpage):
58             raise ExtractorError(
59                 '%s said: You do not have permission to access this page' % self.IE_NAME, expected=True)
60
61         player_url = (
62             'http://embed.livebox.cz/iprimaplay/player-embed-v2.js?__tok%s__=%s' %
63             (floor(random() * 1073741824), floor(random() * 1073741824))
64         )
65
66         req = compat_urllib_request.Request(player_url)
67         req.add_header('Referer', url)
68         playerpage = self._download_webpage(req, video_id)
69
70         base_url = ''.join(re.findall(r"embed\['stream'\] = '(.+?)'.+'(\?auth=)'.+'(.+?)';", playerpage)[1])
71
72         zoneGEO = self._html_search_regex(r'"zoneGEO":(.+?),', webpage, 'zoneGEO')
73         if zoneGEO != '0':
74             base_url = base_url.replace('token', 'token_' + zoneGEO)
75
76         formats = []
77         for format_id in ['lq', 'hq', 'hd']:
78             filename = self._html_search_regex(
79                 r'"%s_id":(.+?),' % format_id, webpage, 'filename')
80
81             if filename == 'null':
82                 continue
83
84             real_id = self._search_regex(
85                 r'Prima-(?:[0-9]{10}|WEB)-([0-9]+)[-_]',
86                 filename, 'real video id')
87
88             if format_id == 'lq':
89                 quality = 0
90             elif format_id == 'hq':
91                 quality = 1
92             elif format_id == 'hd':
93                 quality = 2
94                 filename = 'hq/' + filename
95
96             formats.append({
97                 'format_id': format_id,
98                 'url': base_url,
99                 'quality': quality,
100                 'play_path': 'mp4:' + filename.replace('"', '')[:-4],
101                 'rtmp_live': True,
102                 'ext': 'flv',
103             })
104
105         self._sort_formats(formats)
106
107         return {
108             'id': real_id,
109             'title': remove_end(self._og_search_title(webpage), ' | Prima PLAY'),
110             'thumbnail': self._og_search_thumbnail(webpage),
111             'formats': formats,
112             'description': self._og_search_description(webpage),
113         }