logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

jamendo.py (6901B)


  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. 'title': 'Stories from Emona I',
  30. # 'artist': 'Maya Filipič',
  31. 'track': 'Stories from Emona I',
  32. 'duration': 210,
  33. 'thumbnail': r're:^https?://.*\.jpg',
  34. 'timestamp': 1217438117,
  35. 'upload_date': '20080730',
  36. 'license': 'by-nc-nd',
  37. 'view_count': int,
  38. 'like_count': int,
  39. 'average_rating': int,
  40. 'tags': ['piano', 'peaceful', 'newage', 'strings', 'upbeat'],
  41. }
  42. }, {
  43. 'url': 'https://licensing.jamendo.com/en/track/1496667/energetic-rock',
  44. 'only_matching': True,
  45. }]
  46. def _call_api(self, resource, resource_id):
  47. path = '/api/%ss' % resource
  48. rand = compat_str(random.random())
  49. return self._download_json(
  50. 'https://www.jamendo.com' + path, resource_id, query={
  51. 'id[]': resource_id,
  52. }, headers={
  53. 'X-Jam-Call': '$%s*%s~' % (hashlib.sha1((path + rand).encode()).hexdigest(), rand)
  54. })[0]
  55. def _real_extract(self, url):
  56. track_id, display_id = self._VALID_URL_RE.match(url).groups()
  57. # webpage = self._download_webpage(
  58. # 'https://www.jamendo.com/track/' + track_id, track_id)
  59. # models = self._parse_json(self._html_search_regex(
  60. # r"data-bundled-models='([^']+)",
  61. # webpage, 'bundled models'), track_id)
  62. # track = models['track']['models'][0]
  63. track = self._call_api('track', track_id)
  64. title = track_name = track['name']
  65. # get_model = lambda x: try_get(models, lambda y: y[x]['models'][0], dict) or {}
  66. # artist = get_model('artist')
  67. # artist_name = artist.get('name')
  68. # if artist_name:
  69. # title = '%s - %s' % (artist_name, title)
  70. # album = get_model('album')
  71. formats = [{
  72. 'url': 'https://%s.jamendo.com/?trackid=%s&format=%s&from=app-97dab294'
  73. % (sub_domain, track_id, format_id),
  74. 'format_id': format_id,
  75. 'ext': ext,
  76. 'quality': quality,
  77. } for quality, (format_id, sub_domain, ext) in enumerate((
  78. ('mp31', 'mp3l', 'mp3'),
  79. ('mp32', 'mp3d', 'mp3'),
  80. ('ogg1', 'ogg', 'ogg'),
  81. ('flac', 'flac', 'flac'),
  82. ))]
  83. self._sort_formats(formats)
  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_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):
  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': 'Shearer - Warmachine',
  139. 'artist': 'Shearer',
  140. 'track': 'Warmachine',
  141. 'timestamp': 1368089771,
  142. 'upload_date': '20130509',
  143. }
  144. }, {
  145. 'md5': '1f358d7b2f98edfe90fd55dac0799d50',
  146. 'info_dict': {
  147. 'id': '1032330',
  148. 'ext': 'flac',
  149. 'title': 'Shearer - Without Your Ghost',
  150. 'artist': 'Shearer',
  151. 'track': 'Without Your Ghost',
  152. 'timestamp': 1368089771,
  153. 'upload_date': '20130509',
  154. }
  155. }],
  156. 'params': {
  157. 'playlistend': 2
  158. }
  159. }]
  160. def _real_extract(self, url):
  161. album_id = self._match_id(url)
  162. album = self._call_api('album', album_id)
  163. album_name = album.get('name')
  164. entries = []
  165. for track in (album.get('tracks') or []):
  166. track_id = track.get('id')
  167. if not track_id:
  168. continue
  169. track_id = compat_str(track_id)
  170. entries.append({
  171. '_type': 'url_transparent',
  172. 'url': 'https://www.jamendo.com/track/' + track_id,
  173. 'ie_key': JamendoIE.ie_key(),
  174. 'id': track_id,
  175. 'album': album_name,
  176. })
  177. return self.playlist_result(
  178. entries, album_id, album_name,
  179. clean_html(try_get(album, lambda x: x['description']['en'], compat_str)))