Merge pull request #7686 from remitamine/acast
[youtube-dl] / youtube_dl / extractor / acast.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import compat_str
8 from ..utils import int_or_none
9
10
11 class ACastBaseIE(InfoExtractor):
12     _API_BASE_URL = 'https://www.acast.com/api/'
13
14
15 class ACastIE(ACastBaseIE):
16     IE_NAME = 'acast'
17     _VALID_URL = r'https?://(?:www\.)?acast\.com/(?P<channel>[^/]+)/(?P<id>[^/#?]+)'
18     _TEST = {
19         'url': 'https://www.acast.com/gardenersquestiontime/liverpool',
20         'md5': '9e9cd59c3a8a7d8d5407605f51093050',
21         'info_dict': {
22             'id': '43da2262-ade7-420c-8564-f6367da7c010',
23             'ext': 'mp3',
24             'title': 'Liverpool',
25             'timestamp': 1446163200000,
26             'description': 'md5:170432c9956eec0670d7080a75000d5b',
27             'duration': 2520,
28         }
29     }
30
31     def _real_extract(self, url):
32         channel, display_id = re.match(self._VALID_URL, url).groups()
33         cast_data = self._download_json(self._API_BASE_URL + 'channels/%s/acasts/%s/playback' % (channel, display_id), display_id)
34
35         return {
36             'id': compat_str(cast_data['id']),
37             'display_id': display_id,
38             'url': cast_data['blings'][0]['audio'],
39             'title': cast_data['name'],
40             'description': cast_data.get('description'),
41             'thumbnail': cast_data.get('image'),
42             'timestamp': int_or_none(cast_data.get('publishingDate')),
43             'duration': int_or_none(cast_data.get('duration')),
44         }
45
46
47 class ACastChannelIE(ACastBaseIE):
48     IE_NAME = 'acast:channel'
49     _VALID_URL = r'https?://(?:www\.)?acast\.com/(?P<id>[^/#?]+)'
50     _TEST = {
51         'url': 'https://www.acast.com/gardenersquestiontime',
52         'info_dict': {
53             'id': '9d8f6f73-6b9d-4d16-9399-52bf88f8f611',
54             'title': 'Gardeners\' Question Time',
55             'description': 'md5:c7ef18049da6a52b63d371b3edccce90',
56         },
57         'playlist_mincount': 5,
58     }
59
60     def _real_extract(self, url):
61         display_id = self._match_id(url)
62         channel_data = self._download_json(self._API_BASE_URL + 'channels/%s' % display_id, display_id)
63         casts = self._download_json(self._API_BASE_URL + 'channels/%s/acasts' % display_id, display_id)
64         entries = [self.url_result('https://www.acast.com/%s/%s' % (display_id, cast['url']), 'ACast') for cast in casts]
65
66         return self.playlist_result(entries, compat_str(channel_data['id']), channel_data['name'], channel_data.get('description'))