[youtube] Skip unsupported adaptive stream type (#18804)
[youtube-dl] / youtube_dl / extractor / jamendo.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from ..compat import compat_urlparse
7 from .common import InfoExtractor
8 from ..utils import parse_duration
9
10
11 class JamendoBaseIE(InfoExtractor):
12     def _extract_meta(self, webpage, fatal=True):
13         title = self._og_search_title(
14             webpage, default=None) or self._search_regex(
15             r'<title>([^<]+)', webpage,
16             'title', default=None)
17         if title:
18             title = self._search_regex(
19                 r'(.+?)\s*\|\s*Jamendo Music', title, 'title', default=None)
20         if not title:
21             title = self._html_search_meta(
22                 'name', webpage, 'title', fatal=fatal)
23         mobj = re.search(r'(.+) - (.+)', title or '')
24         artist, second = mobj.groups() if mobj else [None] * 2
25         return title, artist, second
26
27
28 class JamendoIE(JamendoBaseIE):
29     _VALID_URL = r'''(?x)
30                     https?://
31                         (?:
32                             licensing\.jamendo\.com/[^/]+|
33                             (?:www\.)?jamendo\.com
34                         )
35                         /track/(?P<id>[0-9]+)/(?P<display_id>[^/?#&]+)
36                     '''
37     _TESTS = [{
38         'url': 'https://www.jamendo.com/track/196219/stories-from-emona-i',
39         'md5': '6e9e82ed6db98678f171c25a8ed09ffd',
40         'info_dict': {
41             'id': '196219',
42             'display_id': 'stories-from-emona-i',
43             'ext': 'flac',
44             'title': 'Maya Filipič - Stories from Emona I',
45             'artist': 'Maya Filipič',
46             'track': 'Stories from Emona I',
47             'duration': 210,
48             'thumbnail': r're:^https?://.*\.jpg'
49         }
50     }, {
51         'url': 'https://licensing.jamendo.com/en/track/1496667/energetic-rock',
52         'only_matching': True,
53     }]
54
55     def _real_extract(self, url):
56         mobj = self._VALID_URL_RE.match(url)
57         track_id = mobj.group('id')
58         display_id = mobj.group('display_id')
59
60         webpage = self._download_webpage(
61             'https://www.jamendo.com/track/%s/%s' % (track_id, display_id),
62             display_id)
63
64         title, artist, track = self._extract_meta(webpage)
65
66         formats = [{
67             'url': 'https://%s.jamendo.com/?trackid=%s&format=%s&from=app-97dab294'
68                    % (sub_domain, track_id, format_id),
69             'format_id': format_id,
70             'ext': ext,
71             'quality': quality,
72         } for quality, (format_id, sub_domain, ext) in enumerate((
73             ('mp31', 'mp3l', 'mp3'),
74             ('mp32', 'mp3d', 'mp3'),
75             ('ogg1', 'ogg', 'ogg'),
76             ('flac', 'flac', 'flac'),
77         ))]
78         self._sort_formats(formats)
79
80         thumbnail = self._html_search_meta(
81             'image', webpage, 'thumbnail', fatal=False)
82         duration = parse_duration(self._search_regex(
83             r'<span[^>]+itemprop=["\']duration["\'][^>]+content=["\'](.+?)["\']',
84             webpage, 'duration', fatal=False))
85
86         return {
87             'id': track_id,
88             'display_id': display_id,
89             'thumbnail': thumbnail,
90             'title': title,
91             'duration': duration,
92             'artist': artist,
93             'track': track,
94             'formats': formats
95         }
96
97
98 class JamendoAlbumIE(JamendoBaseIE):
99     _VALID_URL = r'https?://(?:www\.)?jamendo\.com/album/(?P<id>[0-9]+)/(?P<display_id>[\w-]+)'
100     _TEST = {
101         'url': 'https://www.jamendo.com/album/121486/duck-on-cover',
102         'info_dict': {
103             'id': '121486',
104             'title': 'Shearer - Duck On Cover'
105         },
106         'playlist': [{
107             'md5': 'e1a2fcb42bda30dfac990212924149a8',
108             'info_dict': {
109                 'id': '1032333',
110                 'ext': 'flac',
111                 'title': 'Shearer - Warmachine',
112                 'artist': 'Shearer',
113                 'track': 'Warmachine',
114             }
115         }, {
116             'md5': '1f358d7b2f98edfe90fd55dac0799d50',
117             'info_dict': {
118                 'id': '1032330',
119                 'ext': 'flac',
120                 'title': 'Shearer - Without Your Ghost',
121                 'artist': 'Shearer',
122                 'track': 'Without Your Ghost',
123             }
124         }],
125         'params': {
126             'playlistend': 2
127         }
128     }
129
130     def _real_extract(self, url):
131         mobj = self._VALID_URL_RE.match(url)
132         album_id = mobj.group('id')
133
134         webpage = self._download_webpage(url, mobj.group('display_id'))
135
136         title, artist, album = self._extract_meta(webpage, fatal=False)
137
138         entries = [{
139             '_type': 'url_transparent',
140             'url': compat_urlparse.urljoin(url, m.group('path')),
141             'ie_key': JamendoIE.ie_key(),
142             'id': self._search_regex(
143                 r'/track/(\d+)', m.group('path'), 'track id', default=None),
144             'artist': artist,
145             'album': album,
146         } for m in re.finditer(
147             r'<a[^>]+href=(["\'])(?P<path>(?:(?!\1).)+)\1[^>]+class=["\'][^>]*js-trackrow-albumpage-link',
148             webpage)]
149
150         return self.playlist_result(entries, album_id, title)