]> jfr.im git - yt-dlp.git/blob - youtube_dl/extractor/orf.py
[eighttracks] PEP8
[yt-dlp.git] / youtube_dl / extractor / orf.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import json
5 import re
6 import calendar
7 import datetime
8
9 from .common import InfoExtractor
10 from ..utils import (
11 HEADRequest,
12 unified_strdate,
13 ExtractorError,
14 )
15
16
17 class ORFTVthekIE(InfoExtractor):
18 IE_NAME = 'orf:tvthek'
19 IE_DESC = 'ORF TVthek'
20 _VALID_URL = r'https?://tvthek\.orf\.at/(?:programs/.+?/episodes|topics?/.+?|program/[^/]+)/(?P<id>\d+)'
21
22 _TESTS = [{
23 'url': 'http://tvthek.orf.at/program/Aufgetischt/2745173/Aufgetischt-Mit-der-Steirischen-Tafelrunde/8891389',
24 'playlist': [{
25 'md5': '2942210346ed779588f428a92db88712',
26 'info_dict': {
27 'id': '8896777',
28 'ext': 'mp4',
29 'title': 'Aufgetischt: Mit der Steirischen Tafelrunde',
30 'description': 'md5:c1272f0245537812d4e36419c207b67d',
31 'duration': 2668,
32 'upload_date': '20141208',
33 },
34 }],
35 'skip': 'Blocked outside of Austria / Germany',
36 }, {
37 'url': 'http://tvthek.orf.at/topic/Im-Wandel-der-Zeit/8002126/Best-of-Ingrid-Thurnher/7982256',
38 'playlist': [{
39 'md5': '68f543909aea49d621dfc7703a11cfaf',
40 'info_dict': {
41 'id': '7982259',
42 'ext': 'mp4',
43 'title': 'Best of Ingrid Thurnher',
44 'upload_date': '20140527',
45 'description': 'Viele Jahre war Ingrid Thurnher das "Gesicht" der ZIB 2. Vor ihrem Wechsel zur ZIB 2 im jahr 1995 moderierte sie unter anderem "Land und Leute", "Österreich-Bild" und "Niederösterreich heute".',
46 }
47 }],
48 '_skip': 'Blocked outside of Austria / Germany',
49 }]
50
51 def _real_extract(self, url):
52 playlist_id = self._match_id(url)
53 webpage = self._download_webpage(url, playlist_id)
54
55 data_json = self._search_regex(
56 r'initializeAdworx\((.+?)\);\n', webpage, 'video info')
57 all_data = json.loads(data_json)
58
59 def get_segments(all_data):
60 for data in all_data:
61 if data['name'] in (
62 'Tracker::EPISODE_DETAIL_PAGE_OVER_PROGRAM',
63 'Tracker::EPISODE_DETAIL_PAGE_OVER_TOPIC'):
64 return data['values']['segments']
65
66 sdata = get_segments(all_data)
67 if not sdata:
68 raise ExtractorError('Unable to extract segments')
69
70 def quality_to_int(s):
71 m = re.search('([0-9]+)', s)
72 if m is None:
73 return -1
74 return int(m.group(1))
75
76 entries = []
77 for sd in sdata:
78 video_id = sd['id']
79 formats = [{
80 'preference': -10 if fd['delivery'] == 'hls' else None,
81 'format_id': '%s-%s-%s' % (
82 fd['delivery'], fd['quality'], fd['quality_string']),
83 'url': fd['src'],
84 'protocol': fd['protocol'],
85 'quality': quality_to_int(fd['quality']),
86 } for fd in sd['playlist_item_array']['sources']]
87
88 # Check for geoblocking.
89 # There is a property is_geoprotection, but that's always false
90 geo_str = sd.get('geoprotection_string')
91 if geo_str:
92 try:
93 http_url = next(
94 f['url']
95 for f in formats
96 if re.match(r'^https?://.*\.mp4$', f['url']))
97 except StopIteration:
98 pass
99 else:
100 req = HEADRequest(http_url)
101 self._request_webpage(
102 req, video_id,
103 note='Testing for geoblocking',
104 errnote=((
105 'This video seems to be blocked outside of %s. '
106 'You may want to try the streaming-* formats.')
107 % geo_str),
108 fatal=False)
109
110 self._sort_formats(formats)
111
112 upload_date = unified_strdate(sd['created_date'])
113 entries.append({
114 '_type': 'video',
115 'id': video_id,
116 'title': sd['header'],
117 'formats': formats,
118 'description': sd.get('description'),
119 'duration': int(sd['duration_in_seconds']),
120 'upload_date': upload_date,
121 'thumbnail': sd.get('image_full_url'),
122 })
123
124 return {
125 '_type': 'playlist',
126 'entries': entries,
127 'id': playlist_id,
128 }
129
130
131 # Audios on ORF radio are only available for 7 days, so we can't add tests.
132
133
134 class ORFOE1IE(InfoExtractor):
135 IE_NAME = 'orf:oe1'
136 IE_DESC = 'Radio Österreich 1'
137 _VALID_URL = r'http://oe1\.orf\.at/programm/(?P<id>[0-9]+)'
138
139 def _real_extract(self, url):
140 show_id = self._match_id(url)
141 data = self._download_json(
142 'http://oe1.orf.at/programm/%s/konsole' % show_id,
143 show_id
144 )
145
146 timestamp = datetime.datetime.strptime('%s %s' % (
147 data['item']['day_label'],
148 data['item']['time']
149 ), '%d.%m.%Y %H:%M')
150 unix_timestamp = calendar.timegm(timestamp.utctimetuple())
151
152 return {
153 'id': show_id,
154 'title': data['item']['title'],
155 'url': data['item']['url_stream'],
156 'ext': 'mp3',
157 'description': data['item'].get('info'),
158 'timestamp': unix_timestamp
159 }
160
161
162 class ORFFM4IE(InfoExtractor):
163 IE_NAME = 'orf:fm4'
164 IE_DESC = 'radio FM4'
165 _VALID_URL = r'http://fm4\.orf\.at/7tage/?#(?P<date>[0-9]+)/(?P<show>\w+)'
166
167 def _real_extract(self, url):
168 mobj = re.match(self._VALID_URL, url)
169 show_date = mobj.group('date')
170 show_id = mobj.group('show')
171
172 data = self._download_json(
173 'http://audioapi.orf.at/fm4/json/2.0/broadcasts/%s/4%s' % (show_date, show_id),
174 show_id
175 )
176
177 def extract_entry_dict(info, title, subtitle):
178 return {
179 'id': info['loopStreamId'].replace('.mp3', ''),
180 'url': 'http://loopstream01.apa.at/?channel=fm4&id=%s' % info['loopStreamId'],
181 'title': title,
182 'description': subtitle,
183 'duration': (info['end'] - info['start']) / 1000,
184 'timestamp': info['start'] / 1000,
185 'ext': 'mp3'
186 }
187
188 entries = [extract_entry_dict(t, data['title'], data['subtitle']) for t in data['streams']]
189
190 return {
191 '_type': 'playlist',
192 'id': show_id,
193 'title': data['title'],
194 'description': data['subtitle'],
195 'entries': entries
196 }