You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
6.4 KiB

  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(
  43. 'https://www.jamendo.com/track/' + track_id, track_id)
  44. models = self._parse_json(self._html_search_regex(
  45. r"data-bundled-models='([^']+)",
  46. webpage, 'bundled models'), track_id)
  47. track = models['track']['models'][0]
  48. title = track_name = track['name']
  49. get_model = lambda x: try_get(models, lambda y: y[x]['models'][0], dict) or {}
  50. artist = get_model('artist')
  51. artist_name = artist.get('name')
  52. if artist_name:
  53. title = '%s - %s' % (artist_name, title)
  54. album = get_model('album')
  55. formats = [{
  56. 'url': 'https://%s.jamendo.com/?trackid=%s&format=%s&from=app-97dab294'
  57. % (sub_domain, track_id, format_id),
  58. 'format_id': format_id,
  59. 'ext': ext,
  60. 'quality': quality,
  61. } for quality, (format_id, sub_domain, ext) in enumerate((
  62. ('mp31', 'mp3l', 'mp3'),
  63. ('mp32', 'mp3d', 'mp3'),
  64. ('ogg1', 'ogg', 'ogg'),
  65. ('flac', 'flac', 'flac'),
  66. ))]
  67. self._sort_formats(formats)
  68. urls = []
  69. thumbnails = []
  70. for _, covers in track.get('cover', {}).items():
  71. for cover_id, cover_url in covers.items():
  72. if not cover_url or cover_url in urls:
  73. continue
  74. urls.append(cover_url)
  75. size = int_or_none(cover_id.lstrip('size'))
  76. thumbnails.append({
  77. 'id': cover_id,
  78. 'url': cover_url,
  79. 'width': size,
  80. 'height': size,
  81. })
  82. tags = []
  83. for tag in track.get('tags', []):
  84. tag_name = tag.get('name')
  85. if not tag_name:
  86. continue
  87. tags.append(tag_name)
  88. stats = track.get('stats') or {}
  89. return {
  90. 'id': track_id,
  91. 'display_id': display_id,
  92. 'thumbnails': thumbnails,
  93. 'title': title,
  94. 'description': track.get('description'),
  95. 'duration': int_or_none(track.get('duration')),
  96. 'artist': artist_name,
  97. 'track': track_name,
  98. 'album': album.get('name'),
  99. 'formats': formats,
  100. 'license': '-'.join(track.get('licenseCC', [])) or None,
  101. 'timestamp': int_or_none(track.get('dateCreated')),
  102. 'view_count': int_or_none(stats.get('listenedAll')),
  103. 'like_count': int_or_none(stats.get('favorited')),
  104. 'average_rating': int_or_none(stats.get('averageNote')),
  105. 'tags': tags,
  106. }
  107. class JamendoAlbumIE(InfoExtractor):
  108. _VALID_URL = r'https?://(?:www\.)?jamendo\.com/album/(?P<id>[0-9]+)'
  109. _TEST = {
  110. 'url': 'https://www.jamendo.com/album/121486/duck-on-cover',
  111. 'info_dict': {
  112. 'id': '121486',
  113. 'title': 'Duck On Cover',
  114. 'description': 'md5:c2920eaeef07d7af5b96d7c64daf1239',
  115. },
  116. 'playlist': [{
  117. 'md5': 'e1a2fcb42bda30dfac990212924149a8',
  118. 'info_dict': {
  119. 'id': '1032333',
  120. 'ext': 'flac',
  121. 'title': 'Shearer - Warmachine',
  122. 'artist': 'Shearer',
  123. 'track': 'Warmachine',
  124. 'timestamp': 1368089771,
  125. 'upload_date': '20130509',
  126. }
  127. }, {
  128. 'md5': '1f358d7b2f98edfe90fd55dac0799d50',
  129. 'info_dict': {
  130. 'id': '1032330',
  131. 'ext': 'flac',
  132. 'title': 'Shearer - Without Your Ghost',
  133. 'artist': 'Shearer',
  134. 'track': 'Without Your Ghost',
  135. 'timestamp': 1368089771,
  136. 'upload_date': '20130509',
  137. }
  138. }],
  139. 'params': {
  140. 'playlistend': 2
  141. }
  142. }
  143. def _call_api(self, resource, resource_id):
  144. path = '/api/%ss' % resource
  145. rand = compat_str(random.random())
  146. return self._download_json(
  147. 'https://www.jamendo.com' + path, resource_id, query={
  148. 'id[]': resource_id,
  149. }, headers={
  150. 'X-Jam-Call': '$%s*%s~' % (hashlib.sha1((path + rand).encode()).hexdigest(), rand)
  151. })[0]
  152. def _real_extract(self, url):
  153. album_id = self._match_id(url)
  154. album = self._call_api('album', album_id)
  155. album_name = album.get('name')
  156. entries = []
  157. for track in album.get('tracks', []):
  158. track_id = track.get('id')
  159. if not track_id:
  160. continue
  161. track_id = compat_str(track_id)
  162. entries.append({
  163. '_type': 'url_transparent',
  164. 'url': 'https://www.jamendo.com/track/' + track_id,
  165. 'ie_key': JamendoIE.ie_key(),
  166. 'id': track_id,
  167. 'album': album_name,
  168. })
  169. return self.playlist_result(
  170. entries, album_id, album_name,
  171. clean_html(try_get(album, lambda x: x['description']['en'], compat_str)))