[acast] Fix extraction (closes #16118)
[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     float_or_none,
11     unified_timestamp,
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     _TESTS = [{
20         # test with one bling
21         'url': 'https://www.acast.com/condenasttraveler/-where-are-you-taipei-101-taiwan',
22         'md5': 'ada3de5a1e3a2a381327d749854788bb',
23         'info_dict': {
24             'id': '57de3baa-4bb0-487e-9418-2692c1277a34',
25             'ext': 'mp3',
26             'title': '"Where Are You?": Taipei 101, Taiwan',
27             'timestamp': 1196172000,
28             'upload_date': '20071127',
29             'description': 'md5:a0b4ef3634e63866b542e5b1199a1a0e',
30             'duration': 211,
31         }
32     }, {
33         # test with multiple blings
34         'url': 'https://www.acast.com/sparpodcast/2.raggarmordet-rosterurdetforflutna',
35         'md5': 'e87d5b8516cd04c0d81b6ee1caca28d0',
36         'info_dict': {
37             'id': '2a92b283-1a75-4ad8-8396-499c641de0d9',
38             'ext': 'mp3',
39             'title': '2. Raggarmordet - Röster ur det förflutna',
40             'timestamp': 1477346700,
41             'upload_date': '20161024',
42             'description': 'md5:4f81f6d8cf2e12ee21a321d8bca32db4',
43             'duration': 2766,
44         }
45     }]
46
47     def _real_extract(self, url):
48         channel, display_id = re.match(self._VALID_URL, url).groups()
49         s = self._download_json(
50             'https://play-api.acast.com/stitch/%s/%s' % (channel, display_id),
51             display_id)['result']
52         media_url = s['url']
53         cast_data = self._download_json(
54             'https://play-api.acast.com/splash/%s/%s' % (channel, display_id), display_id)
55         e = cast_data['result']['episode']
56         return {
57             'id': compat_str(e['id']),
58             'display_id': display_id,
59             'url': media_url,
60             'title': e['name'],
61             'description': e.get('description'),
62             'thumbnail': e.get('image'),
63             'timestamp': unified_timestamp(e.get('publishingDate')),
64             'duration': float_or_none(s.get('duration') or e.get('duration')),
65         }
66
67
68 class ACastChannelIE(InfoExtractor):
69     IE_NAME = 'acast:channel'
70     _VALID_URL = r'https?://(?:www\.)?acast\.com/(?P<id>[^/#?]+)'
71     _TEST = {
72         'url': 'https://www.acast.com/condenasttraveler',
73         'info_dict': {
74             'id': '50544219-29bb-499e-a083-6087f4cb7797',
75             'title': 'Condé Nast Traveler Podcast',
76             'description': 'md5:98646dee22a5b386626ae31866638fbd',
77         },
78         'playlist_mincount': 20,
79     }
80     _API_BASE_URL = 'https://www.acast.com/api/'
81     _PAGE_SIZE = 10
82
83     @classmethod
84     def suitable(cls, url):
85         return False if ACastIE.suitable(url) else super(ACastChannelIE, cls).suitable(url)
86
87     def _fetch_page(self, channel_slug, page):
88         casts = self._download_json(
89             self._API_BASE_URL + 'channels/%s/acasts?page=%s' % (channel_slug, page),
90             channel_slug, note='Download page %d of channel data' % page)
91         for cast in casts:
92             yield self.url_result(
93                 'https://www.acast.com/%s/%s' % (channel_slug, cast['url']),
94                 'ACast', cast['id'])
95
96     def _real_extract(self, url):
97         channel_slug = self._match_id(url)
98         channel_data = self._download_json(
99             self._API_BASE_URL + 'channels/%s' % channel_slug, channel_slug)
100         entries = OnDemandPagedList(functools.partial(
101             self._fetch_page, channel_slug), self._PAGE_SIZE)
102         return self.playlist_result(entries, compat_str(
103             channel_data['id']), channel_data['name'], channel_data.get('description'))