[youtube] Fix 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     clean_html,
11     float_or_none,
12     int_or_none,
13     try_get,
14     unified_timestamp,
15     OnDemandPagedList,
16 )
17
18
19 class ACastIE(InfoExtractor):
20     IE_NAME = 'acast'
21     _VALID_URL = r'''(?x)
22                     https?://
23                         (?:
24                             (?:(?:embed|www)\.)?acast\.com/|
25                             play\.acast\.com/s/
26                         )
27                         (?P<channel>[^/]+)/(?P<id>[^/#?]+)
28                     '''
29     _TESTS = [{
30         'url': 'https://www.acast.com/sparpodcast/2.raggarmordet-rosterurdetforflutna',
31         'md5': '16d936099ec5ca2d5869e3a813ee8dc4',
32         'info_dict': {
33             'id': '2a92b283-1a75-4ad8-8396-499c641de0d9',
34             'ext': 'mp3',
35             'title': '2. Raggarmordet - Röster ur det förflutna',
36             'description': 'md5:4f81f6d8cf2e12ee21a321d8bca32db4',
37             'timestamp': 1477346700,
38             'upload_date': '20161024',
39             'duration': 2766.602563,
40             'creator': 'Anton Berg & Martin Johnson',
41             'series': 'Spår',
42             'episode': '2. Raggarmordet - Röster ur det förflutna',
43         }
44     }, {
45         'url': 'http://embed.acast.com/adambuxton/ep.12-adam-joeschristmaspodcast2015',
46         'only_matching': True,
47     }, {
48         'url': 'https://play.acast.com/s/rattegangspodden/s04e09-styckmordet-i-helenelund-del-22',
49         'only_matching': True,
50     }, {
51         'url': 'https://play.acast.com/s/sparpodcast/2a92b283-1a75-4ad8-8396-499c641de0d9',
52         'only_matching': True,
53     }]
54
55     def _real_extract(self, url):
56         channel, display_id = re.match(self._VALID_URL, url).groups()
57         s = self._download_json(
58             'https://feeder.acast.com/api/v1/shows/%s/episodes/%s' % (channel, display_id),
59             display_id)
60         media_url = s['url']
61         if re.search(r'[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}', display_id):
62             episode_url = s.get('episodeUrl')
63             if episode_url:
64                 display_id = episode_url
65             else:
66                 channel, display_id = re.match(self._VALID_URL, s['link']).groups()
67         cast_data = self._download_json(
68             'https://play-api.acast.com/splash/%s/%s' % (channel, display_id),
69             display_id)['result']
70         e = cast_data['episode']
71         title = e.get('name') or s['title']
72         return {
73             'id': compat_str(e['id']),
74             'display_id': display_id,
75             'url': media_url,
76             'title': title,
77             'description': e.get('summary') or clean_html(e.get('description') or s.get('description')),
78             'thumbnail': e.get('image'),
79             'timestamp': unified_timestamp(e.get('publishingDate') or s.get('publishDate')),
80             'duration': float_or_none(e.get('duration') or s.get('duration')),
81             'filesize': int_or_none(e.get('contentLength')),
82             'creator': try_get(cast_data, lambda x: x['show']['author'], compat_str),
83             'series': try_get(cast_data, lambda x: x['show']['name'], compat_str),
84             'season_number': int_or_none(e.get('seasonNumber')),
85             'episode': title,
86             'episode_number': int_or_none(e.get('episodeNumber')),
87         }
88
89
90 class ACastChannelIE(InfoExtractor):
91     IE_NAME = 'acast:channel'
92     _VALID_URL = r'''(?x)
93                     https?://
94                         (?:
95                             (?:www\.)?acast\.com/|
96                             play\.acast\.com/s/
97                         )
98                         (?P<id>[^/#?]+)
99                     '''
100     _TESTS = [{
101         'url': 'https://www.acast.com/todayinfocus',
102         'info_dict': {
103             'id': '4efc5294-5385-4847-98bd-519799ce5786',
104             'title': 'Today in Focus',
105             'description': 'md5:9ba5564de5ce897faeb12963f4537a64',
106         },
107         'playlist_mincount': 35,
108     }, {
109         'url': 'http://play.acast.com/s/ft-banking-weekly',
110         'only_matching': True,
111     }]
112     _API_BASE_URL = 'https://play.acast.com/api/'
113     _PAGE_SIZE = 10
114
115     @classmethod
116     def suitable(cls, url):
117         return False if ACastIE.suitable(url) else super(ACastChannelIE, cls).suitable(url)
118
119     def _fetch_page(self, channel_slug, page):
120         casts = self._download_json(
121             self._API_BASE_URL + 'channels/%s/acasts?page=%s' % (channel_slug, page),
122             channel_slug, note='Download page %d of channel data' % page)
123         for cast in casts:
124             yield self.url_result(
125                 'https://play.acast.com/s/%s/%s' % (channel_slug, cast['url']),
126                 'ACast', cast['id'])
127
128     def _real_extract(self, url):
129         channel_slug = self._match_id(url)
130         channel_data = self._download_json(
131             self._API_BASE_URL + 'channels/%s' % channel_slug, channel_slug)
132         entries = OnDemandPagedList(functools.partial(
133             self._fetch_page, channel_slug), self._PAGE_SIZE)
134         return self.playlist_result(entries, compat_str(
135             channel_data['id']), channel_data['name'], channel_data.get('description'))