[iprima] Relax _VALID_URL (closes #18453)
[youtube-dl] / 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|prima|www)\.iprima\.cz/(?:[^/]+/)*(?P<id>[^/?#&]+)'
16     _GEO_BYPASS = False
17
18     _TESTS = [{
19         'url': 'http://play.iprima.cz/gondici-s-r-o-33',
20         'info_dict': {
21             'id': 'p136534',
22             'ext': 'mp4',
23             'title': 'Gondíci s. r. o. (34)',
24             'description': 'md5:16577c629d006aa91f59ca8d8e7f99bd',
25         },
26         'params': {
27             'skip_download': True,  # m3u8 download
28         },
29     }, {
30         'url': 'http://play.iprima.cz/particka/particka-92',
31         'only_matching': True,
32     }, {
33         # geo restricted
34         'url': 'http://play.iprima.cz/closer-nove-pripady/closer-nove-pripady-iv-1',
35         'only_matching': True,
36     }, {
37         # iframe api.play-backend.iprima.cz
38         'url': 'https://prima.iprima.cz/my-little-pony/mapa-znameni-2-2',
39         'only_matching': True,
40     }, {
41         # iframe prima.iprima.cz
42         'url': 'https://prima.iprima.cz/porady/jak-se-stavi-sen/rodina-rathousova-praha',
43         'only_matching': True,
44     }, {
45         'url': 'http://www.iprima.cz/filmy/desne-rande',
46         'only_matching': True,
47     }]
48
49     def _real_extract(self, url):
50         video_id = self._match_id(url)
51
52         self._set_cookie('play.iprima.cz', 'ott_adult_confirmed', '1')
53
54         webpage = self._download_webpage(url, video_id)
55
56         video_id = self._search_regex(
57             (r'<iframe[^>]+\bsrc=["\'](?:https?:)?//(?:api\.play-backend\.iprima\.cz/prehravac/embedded|prima\.iprima\.cz/[^/]+/[^/]+)\?.*?\bid=(p\d+)',
58              r'data-product="([^"]+)">'),
59             webpage, 'real id')
60
61         playerpage = self._download_webpage(
62             'http://play.iprima.cz/prehravac/init',
63             video_id, note='Downloading player', query={
64                 '_infuse': 1,
65                 '_ts': round(time.time()),
66                 'productId': video_id,
67             }, headers={'Referer': url})
68
69         formats = []
70
71         def extract_formats(format_url, format_key=None, lang=None):
72             ext = determine_ext(format_url)
73             new_formats = []
74             if format_key == 'hls' or ext == 'm3u8':
75                 new_formats = self._extract_m3u8_formats(
76                     format_url, video_id, 'mp4', entry_protocol='m3u8_native',
77                     m3u8_id='hls', fatal=False)
78             elif format_key == 'dash' or ext == 'mpd':
79                 return
80                 new_formats = self._extract_mpd_formats(
81                     format_url, video_id, mpd_id='dash', fatal=False)
82             if lang:
83                 for f in new_formats:
84                     if not f.get('language'):
85                         f['language'] = lang
86             formats.extend(new_formats)
87
88         options = self._parse_json(
89             self._search_regex(
90                 r'(?s)(?:TDIPlayerOptions|playerOptions)\s*=\s*({.+?});\s*\]\]',
91                 playerpage, 'player options', default='{}'),
92             video_id, transform_source=js_to_json, fatal=False)
93         if options:
94             for key, tracks in options.get('tracks', {}).items():
95                 if not isinstance(tracks, list):
96                     continue
97                 for track in tracks:
98                     src = track.get('src')
99                     if src:
100                         extract_formats(src, key.lower(), track.get('lang'))
101
102         if not formats:
103             for _, src in re.findall(r'src["\']\s*:\s*(["\'])(.+?)\1', playerpage):
104                 extract_formats(src)
105
106         if not formats and '>GEO_IP_NOT_ALLOWED<' in playerpage:
107             self.raise_geo_restricted(countries=['CZ'])
108
109         self._sort_formats(formats)
110
111         return {
112             'id': video_id,
113             'title': self._og_search_title(webpage),
114             'thumbnail': self._og_search_thumbnail(webpage),
115             'formats': formats,
116             'description': self._og_search_description(webpage),
117         }