[iprima] Add support for -WEB URLs (Closes #2449)
[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         req = compat_urllib_request.Request(player_url)
52         req.add_header('Referer', url)
53         playerpage = self._download_webpage(req, video_id)
54
55         base_url = ''.join(re.findall(r"embed\['stream'\] = '(.+?)'.+'(\?auth=)'.+'(.+?)';", playerpage)[1])
56
57         zoneGEO = self._html_search_regex(r'"zoneGEO":(.+?),', webpage, 'zoneGEO')
58         if zoneGEO != '0':
59             base_url = base_url.replace('token', 'token_' + zoneGEO)
60
61         formats = []
62         for format_id in ['lq', 'hq', 'hd']:
63             filename = self._html_search_regex(
64                 r'"%s_id":(.+?),' % format_id, webpage, 'filename')
65
66             if filename == 'null':
67                 continue
68
69             real_id = self._search_regex(
70                 r'Prima-(?:[0-9]{10}|WEB)-([0-9]+)[-_]',
71                 filename, 'real video id')
72
73             if format_id == 'lq':
74                 quality = 0
75             elif format_id == 'hq':
76                 quality = 1
77             elif format_id == 'hd':
78                 quality = 2
79                 filename = 'hq/' + filename
80
81             formats.append({
82                 'format_id': format_id,
83                 'url': base_url,
84                 'quality': quality,
85                 'play_path': 'mp4:' + filename.replace('"', '')[:-4],
86                 'rtmp_live': True,
87                 'ext': 'flv',
88             })
89
90         self._sort_formats(formats)
91
92         return {
93             'id': real_id,
94             'title': self._og_search_title(webpage),
95             'thumbnail': self._og_search_thumbnail(webpage),
96             'formats': formats,
97             'description': self._og_search_description(webpage),
98         }