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