]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/orf.py
[orf:tvthek] Add support for topic URLs (Fixes #4474)
[yt-dlp.git] / youtube_dl / extractor / orf.py
CommitLineData
89284910 1# coding: utf-8
5d73273f 2from __future__ import unicode_literals
89284910 3
54543467 4import json
5d73273f 5import re
eb368012
S
6import calendar
7import datetime
54543467
JMF
8
9from .common import InfoExtractor
10from ..utils import (
5d73273f
PH
11 HEADRequest,
12 unified_strdate,
2acc1f8f 13 ExtractorError,
54543467
JMF
14)
15
5d73273f 16
eb368012
S
17class ORFTVthekIE(InfoExtractor):
18 IE_NAME = 'orf:tvthek'
19 IE_DESC = 'ORF TVthek'
13d27a42 20 _VALID_URL = r'https?://tvthek\.orf\.at/(?:programs/.+?/episodes|topics?/.+?|program/[^/]+)/(?P<id>\d+)'
5d73273f 21
13d27a42 22 _TESTS = [{
a6620ac2
PH
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 }],
13d27a42
PH
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 }]
54543467 50
54543467 51 def _real_extract(self, url):
a6620ac2 52 playlist_id = self._match_id(url)
54543467
JMF
53 webpage = self._download_webpage(url, playlist_id)
54
5d73273f
PH
55 data_json = self._search_regex(
56 r'initializeAdworx\((.+?)\);\n', webpage, 'video info')
57 all_data = json.loads(data_json)
2acc1f8f
S
58
59 def get_segments(all_data):
60 for data in all_data:
13d27a42
PH
61 if data['name'] in (
62 'Tracker::EPISODE_DETAIL_PAGE_OVER_PROGRAM',
63 'Tracker::EPISODE_DETAIL_PAGE_OVER_TOPIC'):
2acc1f8f
S
64 return data['values']['segments']
65
66 sdata = get_segments(all_data)
67 if not sdata:
68 raise ExtractorError('Unable to extract segments')
5d73273f
PH
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)
4f81667d 101 self._request_webpage(
5d73273f
PH
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({
54543467 114 '_type': 'video',
5d73273f
PH
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 }
eb368012
S
129
130
131# Audios on ORF radio are only available for 7 days, so we can't add tests.
132
133
134class 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):
a6620ac2 140 show_id = self._match_id(url)
eb368012
S
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
162class ORFFM4IE(InfoExtractor):
163 IE_DESC = '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
5f6a1245 196 }