jamendo.py 7.7 KB

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