Merge pull request #8354 from remitamine/m3u8_metadata
[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 ACastIE(InfoExtractor):
12     IE_NAME = 'acast'
13     _VALID_URL = r'https?://(?:www\.)?acast\.com/(?P<channel>[^/]+)/(?P<id>[^/#?]+)'
14     _TEST = {
15         'url': 'https://www.acast.com/condenasttraveler/-where-are-you-taipei-101-taiwan',
16         'md5': 'ada3de5a1e3a2a381327d749854788bb',
17         'info_dict': {
18             'id': '57de3baa-4bb0-487e-9418-2692c1277a34',
19             'ext': 'mp3',
20             'title': '"Where Are You?": Taipei 101, Taiwan',
21             'timestamp': 1196172000000,
22             'description': 'md5:a0b4ef3634e63866b542e5b1199a1a0e',
23             'duration': 211,
24         }
25     }
26
27     def _real_extract(self, url):
28         channel, display_id = re.match(self._VALID_URL, url).groups()
29
30         embed_page = self._download_webpage(
31             re.sub('(?:www\.)?acast\.com', 'embedcdn.acast.com', url), display_id)
32         cast_data = self._parse_json(self._search_regex(
33             r'window\[\'acast/queries\'\]\s*=\s*([^;]+);', embed_page, 'acast data'),
34             display_id)['GetAcast/%s/%s' % (channel, display_id)]
35
36         return {
37             'id': compat_str(cast_data['id']),
38             'display_id': display_id,
39             'url': cast_data['blings'][0]['audio'],
40             'title': cast_data['name'],
41             'description': cast_data.get('description'),
42             'thumbnail': cast_data.get('image'),
43             'timestamp': int_or_none(cast_data.get('publishingDate')),
44             'duration': int_or_none(cast_data.get('duration')),
45         }
46
47
48 class ACastChannelIE(InfoExtractor):
49     IE_NAME = 'acast:channel'
50     _VALID_URL = r'https?://(?:www\.)?acast\.com/(?P<id>[^/#?]+)'
51     _TEST = {
52         'url': 'https://www.acast.com/condenasttraveler',
53         'info_dict': {
54             'id': '50544219-29bb-499e-a083-6087f4cb7797',
55             'title': 'Condé Nast Traveler Podcast',
56             'description': 'md5:98646dee22a5b386626ae31866638fbd',
57         },
58         'playlist_mincount': 20,
59     }
60     _API_BASE_URL = 'https://www.acast.com/api/'
61
62     @classmethod
63     def suitable(cls, url):
64         return False if ACastIE.suitable(url) else super(ACastChannelIE, cls).suitable(url)
65
66     def _real_extract(self, url):
67         display_id = self._match_id(url)
68         channel_data = self._download_json(self._API_BASE_URL + 'channels/%s' % display_id, display_id)
69         casts = self._download_json(self._API_BASE_URL + 'channels/%s/acasts' % display_id, display_id)
70         entries = [self.url_result('https://www.acast.com/%s/%s' % (display_id, cast['url']), 'ACast') for cast in casts]
71
72         return self.playlist_result(entries, compat_str(channel_data['id']), channel_data['name'], channel_data.get('description'))