]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/orf.py
Prepare for release.
[youtubedl] / 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 class ORFOE1IE(InfoExtractor):
132 IE_NAME = 'orf:oe1'
133 IE_DESC = 'Radio Österreich 1'
134 _VALID_URL = r'http://oe1\.orf\.at/(?:programm/|konsole.*?#\?track_id=)(?P<id>[0-9]+)'
135
136 # Audios on ORF radio are only available for 7 days, so we can't add tests.
137 _TEST = {
138 'url': 'http://oe1.orf.at/konsole?show=on_demand#?track_id=394211',
139 'only_matching': True,
140 }
141
142 def _real_extract(self, url):
143 show_id = self._match_id(url)
144 data = self._download_json(
145 'http://oe1.orf.at/programm/%s/konsole' % show_id,
146 show_id
147 )
148
149 timestamp = datetime.datetime.strptime('%s %s' % (
150 data['item']['day_label'],
151 data['item']['time']
152 ), '%d.%m.%Y %H:%M')
153 unix_timestamp = calendar.timegm(timestamp.utctimetuple())
154
155 return {
156 'id': show_id,
157 'title': data['item']['title'],
158 'url': data['item']['url_stream'],
159 'ext': 'mp3',
160 'description': data['item'].get('info'),
161 'timestamp': unix_timestamp
162 }
163
164
165 class ORFFM4IE(InfoExtractor):
166 IE_NAME = 'orf:fm4'
167 IE_DESC = 'radio FM4'
168 _VALID_URL = r'http://fm4\.orf\.at/7tage/?#(?P<date>[0-9]+)/(?P<show>\w+)'
169
170 def _real_extract(self, url):
171 mobj = re.match(self._VALID_URL, url)
172 show_date = mobj.group('date')
173 show_id = mobj.group('show')
174
175 data = self._download_json(
176 'http://audioapi.orf.at/fm4/json/2.0/broadcasts/%s/4%s' % (show_date, show_id),
177 show_id
178 )
179
180 def extract_entry_dict(info, title, subtitle):
181 return {
182 'id': info['loopStreamId'].replace('.mp3', ''),
183 'url': 'http://loopstream01.apa.at/?channel=fm4&id=%s' % info['loopStreamId'],
184 'title': title,
185 'description': subtitle,
186 'duration': (info['end'] - info['start']) / 1000,
187 'timestamp': info['start'] / 1000,
188 'ext': 'mp3'
189 }
190
191 entries = [extract_entry_dict(t, data['title'], data['subtitle']) for t in data['streams']]
192
193 return {
194 '_type': 'playlist',
195 'id': show_id,
196 'title': data['title'],
197 'description': data['subtitle'],
198 'entries': entries
199 }