Merge pull request #3431 from sehaas/fm4
[youtube-dl] / youtube_dl / extractor / fm4.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import calendar
5 import datetime
6 import re
7
8 from .common import InfoExtractor
9
10 # audios on fm4.orf.at are only available for 7 days, so we can't
11 # add tests.
12
13
14 class FM4IE(InfoExtractor):
15     IE_DESC = 'fm4.orf.at'
16     _VALID_URL = r'http://fm4\.orf\.at/7tage#(?P<date>[0-9]+)/(?P<show>[\w]+)'
17
18     def _extract_entry_dict(self, info, title, subtitle):
19         result = {
20             'id': info['loopStreamId'].replace('.mp3', ''),
21             'url': 'http://loopstream01.apa.at/?channel=fm4&id=%s' % info['loopStreamId'],
22             'title': title,
23             'description': subtitle,
24             'duration': (info['end'] - info['start']) / 1000,
25             'timestamp': info['start'] / 1000,
26             'ext': 'mp3'
27         }
28
29         return result
30
31     def _real_extract(self, url):
32         mobj = re.match(self._VALID_URL, url)
33         show_date = mobj.group('date')
34         show_id = mobj.group('show')
35
36         data = self._download_json(
37             'http://audioapi.orf.at/fm4/json/2.0/broadcasts/%s/4%s' % (show_date, show_id),
38             show_id
39         )
40
41         entries = [ self._extract_entry_dict(t, data['title'], data['subtitle']) for t in data['streams']]
42
43         return {
44             '_type': 'playlist',
45             'id': show_id,
46             'title': data['title'],
47             'description': data['subtitle'],
48             'entries': entries
49         }