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