jamendo.py 7.7 KB

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