Fix "invalid escape sequences" error on Python 3.6
[youtube-dl] / youtube_dl / extractor / playtvak.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..compat import (
6     compat_urlparse,
7     compat_urllib_parse_urlencode,
8 )
9 from ..utils import (
10     ExtractorError,
11     int_or_none,
12     parse_iso8601,
13     qualities,
14 )
15
16
17 class PlaytvakIE(InfoExtractor):
18     IE_DESC = 'Playtvak.cz, iDNES.cz and Lidovky.cz'
19     _VALID_URL = r'https?://(?:.+?\.)?(?:playtvak|idnes|lidovky|metro)\.cz/.*\?(?:c|idvideo)=(?P<id>[^&]+)'
20     _TESTS = [{
21         'url': 'http://www.playtvak.cz/vyzente-vosy-a-srsne-ze-zahrady-dn5-/hodinovy-manzel.aspx?c=A150730_150323_hodinovy-manzel_kuko',
22         'md5': '4525ae312c324b4be2f4603cc78ceb4a',
23         'info_dict': {
24             'id': 'A150730_150323_hodinovy-manzel_kuko',
25             'ext': 'mp4',
26             'title': 'Vyžeňte vosy a sršně ze zahrady',
27             'description': 'md5:f93d398691044d303bc4a3de62f3e976',
28             'thumbnail': r're:(?i)^https?://.*\.(?:jpg|png)$',
29             'duration': 279,
30             'timestamp': 1438732860,
31             'upload_date': '20150805',
32             'is_live': False,
33         }
34     }, {  # live video test
35         'url': 'http://slowtv.playtvak.cz/planespotting-0pr-/planespotting.aspx?c=A150624_164934_planespotting_cat',
36         'info_dict': {
37             'id': 'A150624_164934_planespotting_cat',
38             'ext': 'flv',
39             'title': 're:^Přímý přenos iDNES.cz [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
40             'description': 'Sledujte provoz na ranveji Letiště Václava Havla v Praze',
41             'thumbnail': r're:(?i)^https?://.*\.(?:jpg|png)$',
42             'is_live': True,
43         },
44         'params': {
45             'skip_download': True,  # requires rtmpdump
46         },
47     }, {  # idnes.cz
48         'url': 'http://zpravy.idnes.cz/pes-zavreny-v-aute-rozbijeni-okynek-v-aute-fj5-/domaci.aspx?c=A150809_104116_domaci_pku',
49         'md5': '819832ba33cd7016e58a6658577fe289',
50         'info_dict': {
51             'id': 'A150809_104116_domaci_pku',
52             'ext': 'mp4',
53             'title': 'Zavřeli jsme mraženou pizzu do auta. Upekla se',
54             'description': 'md5:01e73f02329e2e5760bd5eed4d42e3c2',
55             'thumbnail': r're:(?i)^https?://.*\.(?:jpg|png)$',
56             'duration': 39,
57             'timestamp': 1438969140,
58             'upload_date': '20150807',
59             'is_live': False,
60         }
61     }, {  # lidovky.cz
62         'url': 'http://www.lidovky.cz/dalsi-demonstrace-v-praze-o-migraci-duq-/video.aspx?c=A150808_214044_ln-video_ELE',
63         'md5': 'c7209ac4ba9d234d4ad5bab7485bcee8',
64         'info_dict': {
65             'id': 'A150808_214044_ln-video_ELE',
66             'ext': 'mp4',
67             'title': 'Táhni! Demonstrace proti imigrantům budila emoce',
68             'description': 'md5:97c81d589a9491fbfa323c9fa3cca72c',
69             'thumbnail': r're:(?i)^https?://.*\.(?:jpg|png)$',
70             'timestamp': 1439052180,
71             'upload_date': '20150808',
72             'is_live': False,
73         }
74     }, {  # metro.cz
75         'url': 'http://www.metro.cz/video-pod-billboardem-se-na-vltavske-roztocil-kolotoc-deti-vozil-jen-par-hodin-1hx-/metro-extra.aspx?c=A141111_173251_metro-extra_row',
76         'md5': '84fc1deedcac37b7d4a6ccae7c716668',
77         'info_dict': {
78             'id': 'A141111_173251_metro-extra_row',
79             'ext': 'mp4',
80             'title': 'Recesisté udělali z billboardu kolotoč',
81             'description': 'md5:7369926049588c3989a66c9c1a043c4c',
82             'thumbnail': r're:(?i)^https?://.*\.(?:jpg|png)$',
83             'timestamp': 1415725500,
84             'upload_date': '20141111',
85             'is_live': False,
86         }
87     }, {
88         'url': 'http://www.playtvak.cz/embed.aspx?idvideo=V150729_141549_play-porad_kuko',
89         'only_matching': True,
90     }]
91
92     def _real_extract(self, url):
93         video_id = self._match_id(url)
94
95         webpage = self._download_webpage(url, video_id)
96
97         info_url = self._html_search_regex(
98             r'Misc\.videoFLV\(\s*{\s*data\s*:\s*"([^"]+)"', webpage, 'info url')
99
100         parsed_url = compat_urlparse.urlparse(info_url)
101
102         qs = compat_urlparse.parse_qs(parsed_url.query)
103         qs.update({
104             'reklama': ['0'],
105             'type': ['js'],
106         })
107
108         info_url = compat_urlparse.urlunparse(
109             parsed_url._replace(query=compat_urllib_parse_urlencode(qs, True)))
110
111         json_info = self._download_json(
112             info_url, video_id,
113             transform_source=lambda s: s[s.index('{'):s.rindex('}') + 1])
114
115         item = None
116         for i in json_info['items']:
117             if i.get('type') == 'video' or i.get('type') == 'stream':
118                 item = i
119                 break
120         if not item:
121             raise ExtractorError('No suitable stream found')
122
123         quality = qualities(('low', 'middle', 'high'))
124
125         formats = []
126         for fmt in item['video']:
127             video_url = fmt.get('file')
128             if not video_url:
129                 continue
130
131             format_ = fmt['format']
132             format_id = '%s_%s' % (format_, fmt['quality'])
133             preference = None
134
135             if format_ in ('mp4', 'webm'):
136                 ext = format_
137             elif format_ == 'rtmp':
138                 ext = 'flv'
139             elif format_ == 'apple':
140                 ext = 'mp4'
141                 # Some streams have mp3 audio which does not play
142                 # well with ffmpeg filter aac_adtstoasc
143                 preference = -1
144             elif format_ == 'adobe':  # f4m manifest fails with 404 in 80% of requests
145                 continue
146             else:  # Other formats not supported yet
147                 continue
148
149             formats.append({
150                 'url': video_url,
151                 'ext': ext,
152                 'format_id': format_id,
153                 'quality': quality(fmt.get('quality')),
154                 'preference': preference,
155             })
156         self._sort_formats(formats)
157
158         title = item['title']
159         is_live = item['type'] == 'stream'
160         if is_live:
161             title = self._live_title(title)
162         description = self._og_search_description(webpage, default=None) or self._html_search_meta(
163             'description', webpage, 'description')
164         timestamp = None
165         duration = None
166         if not is_live:
167             duration = int_or_none(item.get('length'))
168             timestamp = item.get('published')
169             if timestamp:
170                 timestamp = parse_iso8601(timestamp[:-5])
171
172         return {
173             'id': video_id,
174             'title': title,
175             'description': description,
176             'thumbnail': item.get('image'),
177             'duration': duration,
178             'timestamp': timestamp,
179             'is_live': is_live,
180             'formats': formats,
181         }