[acast] Add support for embed.acast.com
[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     int_or_none,
12     try_get,
13     unified_timestamp,
14     OnDemandPagedList,
15 )
16
17
18 class ACastIE(InfoExtractor):
19     IE_NAME = 'acast'
20     _VALID_URL = r'https?://(?:(?:embed|www)\.)?acast\.com/(?P<channel>[^/]+)/(?P<id>[^/#?]+)'
21     _TESTS = [{
22         'url': 'https://www.acast.com/sparpodcast/2.raggarmordet-rosterurdetforflutna',
23         'md5': 'a02393c74f3bdb1801c3ec2695577ce0',
24         'info_dict': {
25             'id': '2a92b283-1a75-4ad8-8396-499c641de0d9',
26             'ext': 'mp3',
27             'title': '2. Raggarmordet - Röster ur det förflutna',
28             'description': 'md5:4f81f6d8cf2e12ee21a321d8bca32db4',
29             'timestamp': 1477346700,
30             'upload_date': '20161024',
31             'duration': 2766.602563,
32             'creator': 'Anton Berg & Martin Johnson',
33             'series': 'Spår',
34             'episode': '2. Raggarmordet - Röster ur det förflutna',
35         }
36     }, {
37         'url': 'http://embed.acast.com/adambuxton/ep.12-adam-joeschristmaspodcast2015',
38         'only_matching': True,
39     }]
40
41     def _real_extract(self, url):
42         channel, display_id = re.match(self._VALID_URL, url).groups()
43         s = self._download_json(
44             'https://play-api.acast.com/stitch/%s/%s' % (channel, display_id),
45             display_id)['result']
46         media_url = s['url']
47         cast_data = self._download_json(
48             'https://play-api.acast.com/splash/%s/%s' % (channel, display_id),
49             display_id)['result']
50         e = cast_data['episode']
51         title = e['name']
52         return {
53             'id': compat_str(e['id']),
54             'display_id': display_id,
55             'url': media_url,
56             'title': title,
57             'description': e.get('description') or e.get('summary'),
58             'thumbnail': e.get('image'),
59             'timestamp': unified_timestamp(e.get('publishingDate')),
60             'duration': float_or_none(s.get('duration') or e.get('duration')),
61             'filesize': int_or_none(e.get('contentLength')),
62             'creator': try_get(cast_data, lambda x: x['show']['author'], compat_str),
63             'series': try_get(cast_data, lambda x: x['show']['name'], compat_str),
64             'season_number': int_or_none(e.get('seasonNumber')),
65             'episode': title,
66             'episode_number': int_or_none(e.get('episodeNumber')),
67         }
68
69
70 class ACastChannelIE(InfoExtractor):
71     IE_NAME = 'acast:channel'
72     _VALID_URL = r'https?://(?:www\.)?acast\.com/(?P<id>[^/#?]+)'
73     _TEST = {
74         'url': 'https://www.acast.com/condenasttraveler',
75         'info_dict': {
76             'id': '50544219-29bb-499e-a083-6087f4cb7797',
77             'title': 'Condé Nast Traveler Podcast',
78             'description': 'md5:98646dee22a5b386626ae31866638fbd',
79         },
80         'playlist_mincount': 20,
81     }
82     _API_BASE_URL = 'https://www.acast.com/api/'
83     _PAGE_SIZE = 10
84
85     @classmethod
86     def suitable(cls, url):
87         return False if ACastIE.suitable(url) else super(ACastChannelIE, cls).suitable(url)
88
89     def _fetch_page(self, channel_slug, page):
90         casts = self._download_json(
91             self._API_BASE_URL + 'channels/%s/acasts?page=%s' % (channel_slug, page),
92             channel_slug, note='Download page %d of channel data' % page)
93         for cast in casts:
94             yield self.url_result(
95                 'https://www.acast.com/%s/%s' % (channel_slug, cast['url']),
96                 'ACast', cast['id'])
97
98     def _real_extract(self, url):
99         channel_slug = self._match_id(url)
100         channel_data = self._download_json(
101             self._API_BASE_URL + 'channels/%s' % channel_slug, channel_slug)
102         entries = OnDemandPagedList(functools.partial(
103             self._fetch_page, channel_slug), self._PAGE_SIZE)
104         return self.playlist_result(entries, compat_str(
105             channel_data['id']), channel_data['name'], channel_data.get('description'))