[jamendo] Add extractor
[youtube-dl] / youtube_dl / extractor / jamendo.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5 from collections import namedtuple
6
7 from ..compat import compat_urlparse
8 from .common import InfoExtractor
9
10 FormatData = namedtuple('FormatData', [
11     'format_id', 'sub_domain', 'ext', 'quality'])
12
13
14 class JamendoIE(InfoExtractor):
15     _VALID_URL = r'https?://(?:www\.)?jamendo\.com/track/(?P<id>[0-9]+)/(?P<display_id>[\w-]+)'
16     _TEST = {
17         'url': 'https://www.jamendo.com/track/196219/stories-from-emona-i',
18         'md5': '6e9e82ed6db98678f171c25a8ed09ffd',
19         'info_dict': {
20             'id': '196219',
21             'display_id': 'stories-from-emona-i',
22             'ext': 'flac',
23             'title': 'Stories from Emona I',
24             'thumbnail': 're:^https?://.*\.jpg'
25         }
26     }
27
28     def _real_extract(self, url):
29         url_data = self._VALID_URL_RE.match(url)
30         track_id = url_data.group('id')
31         display_id = url_data.group('display_id')
32         webpage = self._download_webpage(url, display_id)
33
34         thumbnail = self._html_search_meta(
35             'image', webpage, 'thumbnail', fatal=False)
36         title = self._html_search_meta('name', webpage, 'title')
37
38         url_template = 'https://%s.jamendo.com/?trackid=%s&format=%s&from=app-97dab294'
39         format_data = [
40             FormatData(
41                 format_id='mp31', sub_domain='mp3l', ext='mp3', quality=0),
42             FormatData(
43                 format_id='mp32', sub_domain='mp3d', ext='mp3', quality=1),
44             FormatData(
45                 format_id='ogg1', sub_domain='ogg', ext='ogg', quality=2),
46             FormatData(
47                 format_id='flac', sub_domain='flac', ext='flac', quality=3),
48         ]
49         formats = [
50             {
51                 'format_id': fd.format_id,
52                 'url': url_template % (fd.sub_domain, track_id, fd.format_id),
53                 'ext': fd.ext,
54                 'quality': fd.quality
55             }
56             for fd in format_data
57         ]
58         self._check_formats(formats, video_id=display_id)
59         return {
60             'id': track_id,
61             'display_id': display_id,
62             'thumbnail': thumbnail,
63             'title': title,
64             'formats': formats
65         }
66
67
68 class JamendoAlbumIE(InfoExtractor):
69     _VALID_URL = r'https?://(?:www\.)?jamendo\.com/album/(?P<id>[0-9]+)/(?P<display_id>[\w-]+)'
70     _TEST = {
71         'url': 'https://www.jamendo.com/album/121486/duck-on-cover',
72         'info_dict': {
73             'id': '121486',
74             'title': 'Duck On Cover'
75         },
76         'playlist_mincount': 2,
77         'playlist': [
78             {
79                 'md5': 'e1a2fcb42bda30dfac990212924149a8',
80                 'info_dict': {
81                     'id': '1032333',
82                     'ext': 'flac',
83                     'title': 'Warmachine'
84                 }
85             },
86             {
87                 'md5': '1f358d7b2f98edfe90fd55dac0799d50',
88                 'info_dict': {
89                     'id': '1032330',
90                     'ext': 'flac',
91                     'title': 'Without Your Ghost'
92                 }
93             }
94         ],
95         'params': {
96             'playlistend': 2
97         }
98     }
99
100     def _real_extract(self, url):
101         url_data = self._VALID_URL_RE.match(url)
102         album_id = url_data.group('id')
103         webpage = self._download_webpage(url, url_data.group('display_id'))
104
105         title = self._html_search_meta('name', webpage, 'title')
106
107         track_paths = re.findall(r'<a href="(.+)" class="link-wrap js-trackrow-albumpage-link" itemprop="url">', webpage)
108         entries = [
109             self.url_result(compat_urlparse.urljoin(url, path), ie=JamendoIE.ie_key())
110             for path in track_paths
111         ]
112         return {
113             '_type': 'playlist',
114             'id': album_id,
115             'title': title,
116             'entries': entries
117         }