]> gitweb @ CieloNegro.org - youtube-dl.git/blob - youtube_dl/extractor/iprima.py
[iprima] Modernize
[youtube-dl.git] / youtube_dl / extractor / iprima.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5 import time
6
7 from .common import InfoExtractor
8 from ..utils import (
9     determine_ext,
10     js_to_json,
11 )
12
13
14 class IPrimaIE(InfoExtractor):
15     _VALID_URL = r'https?://play\.iprima\.cz/(?:.+/)?(?P<id>[^?#]+)'
16
17     _TESTS = [{
18         'url': 'http://play.iprima.cz/gondici-s-r-o-33',
19         'info_dict': {
20             'id': 'p136534',
21             'ext': 'mp4',
22             'title': 'Gondíci s. r. o. (34)',
23             'description': 'md5:16577c629d006aa91f59ca8d8e7f99bd',
24         },
25         'params': {
26             'skip_download': True,  # m3u8 download
27         },
28     }, {
29         'url': 'http://play.iprima.cz/particka/particka-92',
30         'only_matching': True,
31     }]
32
33     def _real_extract(self, url):
34         video_id = self._match_id(url)
35
36         webpage = self._download_webpage(url, video_id)
37
38         video_id = self._search_regex(r'data-product="([^"]+)">', webpage, 'real id')
39
40         playerpage = self._download_webpage(
41             'http://play.iprima.cz/prehravac/init',
42             video_id, note='Downloading player', query={
43                 '_infuse': 1,
44                 '_ts': round(time.time()),
45                 'productId': video_id,
46             }, headers={'Referer': url})
47
48         formats = []
49
50         def extract_formats(format_url, format_key=None, lang=None):
51             ext = determine_ext(format_url)
52             new_formats = []
53             if format_key == 'hls' or ext == 'm3u8':
54                 new_formats = self._extract_m3u8_formats(
55                     format_url, video_id, 'mp4', entry_protocol='m3u8_native',
56                     m3u8_id='hls', fatal=False)
57             elif format_key == 'dash' or ext == 'mpd':
58                 return
59                 new_formats = self._extract_mpd_formats(
60                     format_url, video_id, mpd_id='dash', fatal=False)
61             if lang:
62                 for f in new_formats:
63                     if not f.get('language'):
64                         f['language'] = lang
65             formats.extend(new_formats)
66
67         options = self._parse_json(
68             self._search_regex(
69                 r'(?s)(?:TDIPlayerOptions|playerOptions)\s*=\s*({.+?});\s*\]\]',
70                 playerpage, 'player options', default='{}'),
71             video_id, transform_source=js_to_json, fatal=False)
72         if options:
73             for key, tracks in options.get('tracks', {}).items():
74                 if not isinstance(tracks, list):
75                     continue
76                 for track in tracks:
77                     src = track.get('src')
78                     if src:
79                         extract_formats(src, key.lower(), track.get('lang'))
80
81         if not formats:
82             for _, src in re.findall(r'src["\']\s*:\s*(["\'])(.+?)\1', playerpage):
83                 extract_formats(src)
84
85         if not formats and '>GEO_IP_NOT_ALLOWED<' in playerpage:
86             self.raise_geo_restricted()
87
88         self._sort_formats(formats)
89
90         return {
91             'id': video_id,
92             'title': self._og_search_title(webpage),
93             'thumbnail': self._og_search_thumbnail(webpage),
94             'formats': formats,
95             'description': self._og_search_description(webpage),
96         }