[acast] Fix broken audio URL and timestamp extraction
[youtube-dl] / youtube_dl / extractor / acast.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5 import functools
6
7 from .common import InfoExtractor
8 from ..compat import compat_str
9 from ..utils import (
10     int_or_none,
11     parse_iso8601,
12     OnDemandPagedList,
13 )
14
15
16 class ACastIE(InfoExtractor):
17     IE_NAME = 'acast'
18     _VALID_URL = r'https?://(?:www\.)?acast\.com/(?P<channel>[^/]+)/(?P<id>[^/#?]+)'
19     _TEST = {
20         'url': 'https://www.acast.com/condenasttraveler/-where-are-you-taipei-101-taiwan',
21         'md5': 'ada3de5a1e3a2a381327d749854788bb',
22         'info_dict': {
23             'id': '57de3baa-4bb0-487e-9418-2692c1277a34',
24             'ext': 'mp3',
25             'title': '"Where Are You?": Taipei 101, Taiwan',
26             'timestamp': 1196172000,
27             'upload_date': '20071127',
28             'description': 'md5:a0b4ef3634e63866b542e5b1199a1a0e',
29             'duration': 211,
30         }
31     }
32
33     def _real_extract(self, url):
34         channel, display_id = re.match(self._VALID_URL, url).groups()
35         cast_data = self._download_json(
36             'https://embed.acast.com/api/acasts/%s/%s' % (channel, display_id), display_id)
37         return {
38             'id': compat_str(cast_data['id']),
39             'display_id': display_id,
40             'url': [b['audio'] for b in cast_data['blings'] if b['type'] == 'BlingAudio'][0],
41             'title': cast_data['name'],
42             'description': cast_data.get('description'),
43             'thumbnail': cast_data.get('image'),
44             'timestamp': parse_iso8601(cast_data.get('publishingDate')),
45             'duration': int_or_none(cast_data.get('duration')),
46         }
47
48
49 class ACastChannelIE(InfoExtractor):
50     IE_NAME = 'acast:channel'
51     _VALID_URL = r'https?://(?:www\.)?acast\.com/(?P<id>[^/#?]+)'
52     _TEST = {
53         'url': 'https://www.acast.com/condenasttraveler',
54         'info_dict': {
55             'id': '50544219-29bb-499e-a083-6087f4cb7797',
56             'title': 'Condé Nast Traveler Podcast',
57             'description': 'md5:98646dee22a5b386626ae31866638fbd',
58         },
59         'playlist_mincount': 20,
60     }
61     _API_BASE_URL = 'https://www.acast.com/api/'
62     _PAGE_SIZE = 10
63
64     @classmethod
65     def suitable(cls, url):
66         return False if ACastIE.suitable(url) else super(ACastChannelIE, cls).suitable(url)
67
68     def _fetch_page(self, channel_slug, page):
69         casts = self._download_json(
70             self._API_BASE_URL + 'channels/%s/acasts?page=%s' % (channel_slug, page),
71             channel_slug, note='Download page %d of channel data' % page)
72         for cast in casts:
73             yield self.url_result(
74                 'https://www.acast.com/%s/%s' % (channel_slug, cast['url']),
75                 'ACast', cast['id'])
76
77     def _real_extract(self, url):
78         channel_slug = self._match_id(url)
79         channel_data = self._download_json(
80             self._API_BASE_URL + 'channels/%s' % channel_slug, channel_slug)
81         entries = OnDemandPagedList(functools.partial(
82             self._fetch_page, channel_slug), self._PAGE_SIZE)
83         return self.playlist_result(entries, compat_str(
84             channel_data['id']), channel_data['name'], channel_data.get('description'))