1
0

jamendo.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import hashlib
  4. import random
  5. from ..compat import compat_str
  6. from .common import InfoExtractor
  7. from ..utils import (
  8. clean_html,
  9. int_or_none,
  10. try_get,
  11. )
  12. class JamendoIE(InfoExtractor):
  13. _VALID_URL = r'''(?x)
  14. https?://
  15. (?:
  16. licensing\.jamendo\.com/[^/]+|
  17. (?:www\.)?jamendo\.com
  18. )
  19. /track/(?P<id>[0-9]+)(?:/(?P<display_id>[^/?#&]+))?
  20. '''
  21. _TESTS = [{
  22. 'url': 'https://www.jamendo.com/track/196219/stories-from-emona-i',
  23. 'md5': '6e9e82ed6db98678f171c25a8ed09ffd',
  24. 'info_dict': {
  25. 'id': '196219',
  26. 'display_id': 'stories-from-emona-i',
  27. 'ext': 'flac',
  28. 'title': 'Maya Filipič - Stories from Emona I',
  29. 'artist': 'Maya Filipič',
  30. 'track': 'Stories from Emona I',
  31. 'duration': 210,
  32. 'thumbnail': r're:^https?://.*\.jpg',
  33. 'timestamp': 1217438117,
  34. 'upload_date': '20080730',
  35. }
  36. }, {
  37. 'url': 'https://licensing.jamendo.com/en/track/1496667/energetic-rock',
  38. 'only_matching': True,
  39. }]
  40. def _real_extract(self, url):
  41. track_id, display_id = self._VALID_URL_RE.match(url).groups()
  42. webpage = self._download_webpage(url, track_id)
  43. models = self._parse_json(self._html_search_regex(
  44. r"data-bundled-models='([^']+)",
  45. webpage, 'bundled models'), track_id)
  46. track = models['track']['models'][0]
  47. title = track_name = track['name']
  48. get_model = lambda x: try_get(models, lambda y: y[x]['models'][0], dict) or {}
  49. artist = get_model('artist')
  50. artist_name = artist.get('name')
  51. if artist_name:
  52. title = '%s - %s' % (artist_name, title)
  53. album = get_model('album')
  54. formats = [{
  55. 'url': 'https://%s.jamendo.com/?trackid=%s&format=%s&from=app-97dab294'
  56. % (sub_domain, track_id, format_id),
  57. 'format_id': format_id,
  58. 'ext': ext,
  59. 'quality': quality,
  60. } for quality, (format_id, sub_domain, ext) in enumerate((
  61. ('mp31', 'mp3l', 'mp3'),
  62. ('mp32', 'mp3d', 'mp3'),
  63. ('ogg1', 'ogg', 'ogg'),
  64. ('flac', 'flac', 'flac'),
  65. ))]
  66. self._sort_formats(formats)
  67. urls = []
  68. thumbnails = []
  69. for _, covers in track.get('cover', {}).items():
  70. for cover_id, cover_url in covers.items():
  71. if not cover_url or cover_url in urls:
  72. continue
  73. urls.append(cover_url)
  74. size = int_or_none(cover_id.lstrip('size'))
  75. thumbnails.append({
  76. 'id': cover_id,
  77. 'url': cover_url,
  78. 'width': size,
  79. 'height': size,
  80. })
  81. tags = []
  82. for tag in track.get('tags', []):
  83. tag_name = tag.get('name')
  84. if not tag_name:
  85. continue
  86. tags.append(tag_name)
  87. stats = track.get('stats') or {}
  88. return {
  89. 'id': track_id,
  90. 'display_id': display_id,
  91. 'thumbnails': thumbnails,
  92. 'title': title,
  93. 'description': track.get('description'),
  94. 'duration': int_or_none(track.get('duration')),
  95. 'artist': artist_name,
  96. 'track': track_name,
  97. 'album': album.get('name'),
  98. 'formats': formats,
  99. 'license': '-'.join(track.get('licenseCC', [])) or None,
  100. 'timestamp': int_or_none(track.get('dateCreated')),
  101. 'view_count': int_or_none(stats.get('listenedAll')),
  102. 'like_count': int_or_none(stats.get('favorited')),
  103. 'average_rating': int_or_none(stats.get('averageNote')),
  104. 'tags': tags,
  105. }
  106. class JamendoAlbumIE(InfoExtractor):
  107. _VALID_URL = r'https?://(?:www\.)?jamendo\.com/album/(?P<id>[0-9]+)'
  108. _TEST = {
  109. 'url': 'https://www.jamendo.com/album/121486/duck-on-cover',
  110. 'info_dict': {
  111. 'id': '121486',
  112. 'title': 'Duck On Cover',
  113. 'description': 'md5:c2920eaeef07d7af5b96d7c64daf1239',
  114. },
  115. 'playlist': [{
  116. 'md5': 'e1a2fcb42bda30dfac990212924149a8',
  117. 'info_dict': {
  118. 'id': '1032333',
  119. 'ext': 'flac',
  120. 'title': 'Shearer - Warmachine',
  121. 'artist': 'Shearer',
  122. 'track': 'Warmachine',
  123. 'timestamp': 1368089771,
  124. 'upload_date': '20130509',
  125. }
  126. }, {
  127. 'md5': '1f358d7b2f98edfe90fd55dac0799d50',
  128. 'info_dict': {
  129. 'id': '1032330',
  130. 'ext': 'flac',
  131. 'title': 'Shearer - Without Your Ghost',
  132. 'artist': 'Shearer',
  133. 'track': 'Without Your Ghost',
  134. 'timestamp': 1368089771,
  135. 'upload_date': '20130509',
  136. }
  137. }],
  138. 'params': {
  139. 'playlistend': 2
  140. }
  141. }
  142. def _call_api(self, resource, resource_id):
  143. path = '/api/%ss' % resource
  144. rand = compat_str(random.random())
  145. return self._download_json(
  146. 'https://www.jamendo.com' + path, resource_id, query={
  147. 'id[]': resource_id,
  148. }, headers={
  149. 'X-Jam-Call': '$%s*%s~' % (hashlib.sha1((path + rand).encode()).hexdigest(), rand)
  150. })[0]
  151. def _real_extract(self, url):
  152. album_id = self._match_id(url)
  153. album = self._call_api('album', album_id)
  154. album_name = album.get('name')
  155. entries = []
  156. for track in album.get('tracks', []):
  157. track_id = track.get('id')
  158. if not track_id:
  159. continue
  160. track_id = compat_str(track_id)
  161. entries.append({
  162. '_type': 'url_transparent',
  163. 'url': 'https://www.jamendo.com/track/' + track_id,
  164. 'ie_key': JamendoIE.ie_key(),
  165. 'id': track_id,
  166. 'album': album_name,
  167. })
  168. return self.playlist_result(
  169. entries, album_id, album_name,
  170. clean_html(try_get(album, lambda x: x['description']['en'], compat_str)))