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.

150 lines
5.0 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from ..compat import compat_urlparse
  5. from .common import InfoExtractor
  6. from ..utils import parse_duration
  7. class JamendoBaseIE(InfoExtractor):
  8. def _extract_meta(self, webpage, fatal=True):
  9. title = self._og_search_title(
  10. webpage, default=None) or self._search_regex(
  11. r'<title>([^<]+)', webpage,
  12. 'title', default=None)
  13. if title:
  14. title = self._search_regex(
  15. r'(.+?)\s*\|\s*Jamendo Music', title, 'title', default=None)
  16. if not title:
  17. title = self._html_search_meta(
  18. 'name', webpage, 'title', fatal=fatal)
  19. mobj = re.search(r'(.+) - (.+)', title or '')
  20. artist, second = mobj.groups() if mobj else [None] * 2
  21. return title, artist, second
  22. class JamendoIE(JamendoBaseIE):
  23. _VALID_URL = r'''(?x)
  24. https?://
  25. (?:
  26. licensing\.jamendo\.com/[^/]+|
  27. (?:www\.)?jamendo\.com
  28. )
  29. /track/(?P<id>[0-9]+)/(?P<display_id>[^/?#&]+)
  30. '''
  31. _TESTS = [{
  32. 'url': 'https://www.jamendo.com/track/196219/stories-from-emona-i',
  33. 'md5': '6e9e82ed6db98678f171c25a8ed09ffd',
  34. 'info_dict': {
  35. 'id': '196219',
  36. 'display_id': 'stories-from-emona-i',
  37. 'ext': 'flac',
  38. 'title': 'Maya Filipič - Stories from Emona I',
  39. 'artist': 'Maya Filipič',
  40. 'track': 'Stories from Emona I',
  41. 'duration': 210,
  42. 'thumbnail': r're:^https?://.*\.jpg'
  43. }
  44. }, {
  45. 'url': 'https://licensing.jamendo.com/en/track/1496667/energetic-rock',
  46. 'only_matching': True,
  47. }]
  48. def _real_extract(self, url):
  49. mobj = self._VALID_URL_RE.match(url)
  50. track_id = mobj.group('id')
  51. display_id = mobj.group('display_id')
  52. webpage = self._download_webpage(
  53. 'https://www.jamendo.com/track/%s/%s' % (track_id, display_id),
  54. display_id)
  55. title, artist, track = self._extract_meta(webpage)
  56. formats = [{
  57. 'url': 'https://%s.jamendo.com/?trackid=%s&format=%s&from=app-97dab294'
  58. % (sub_domain, track_id, format_id),
  59. 'format_id': format_id,
  60. 'ext': ext,
  61. 'quality': quality,
  62. } for quality, (format_id, sub_domain, ext) in enumerate((
  63. ('mp31', 'mp3l', 'mp3'),
  64. ('mp32', 'mp3d', 'mp3'),
  65. ('ogg1', 'ogg', 'ogg'),
  66. ('flac', 'flac', 'flac'),
  67. ))]
  68. self._sort_formats(formats)
  69. thumbnail = self._html_search_meta(
  70. 'image', webpage, 'thumbnail', fatal=False)
  71. duration = parse_duration(self._search_regex(
  72. r'<span[^>]+itemprop=["\']duration["\'][^>]+content=["\'](.+?)["\']',
  73. webpage, 'duration', fatal=False))
  74. return {
  75. 'id': track_id,
  76. 'display_id': display_id,
  77. 'thumbnail': thumbnail,
  78. 'title': title,
  79. 'duration': duration,
  80. 'artist': artist,
  81. 'track': track,
  82. 'formats': formats
  83. }
  84. class JamendoAlbumIE(JamendoBaseIE):
  85. _VALID_URL = r'https?://(?:www\.)?jamendo\.com/album/(?P<id>[0-9]+)/(?P<display_id>[\w-]+)'
  86. _TEST = {
  87. 'url': 'https://www.jamendo.com/album/121486/duck-on-cover',
  88. 'info_dict': {
  89. 'id': '121486',
  90. 'title': 'Shearer - Duck On Cover'
  91. },
  92. 'playlist': [{
  93. 'md5': 'e1a2fcb42bda30dfac990212924149a8',
  94. 'info_dict': {
  95. 'id': '1032333',
  96. 'ext': 'flac',
  97. 'title': 'Shearer - Warmachine',
  98. 'artist': 'Shearer',
  99. 'track': 'Warmachine',
  100. }
  101. }, {
  102. 'md5': '1f358d7b2f98edfe90fd55dac0799d50',
  103. 'info_dict': {
  104. 'id': '1032330',
  105. 'ext': 'flac',
  106. 'title': 'Shearer - Without Your Ghost',
  107. 'artist': 'Shearer',
  108. 'track': 'Without Your Ghost',
  109. }
  110. }],
  111. 'params': {
  112. 'playlistend': 2
  113. }
  114. }
  115. def _real_extract(self, url):
  116. mobj = self._VALID_URL_RE.match(url)
  117. album_id = mobj.group('id')
  118. webpage = self._download_webpage(url, mobj.group('display_id'))
  119. title, artist, album = self._extract_meta(webpage, fatal=False)
  120. entries = [{
  121. '_type': 'url_transparent',
  122. 'url': compat_urlparse.urljoin(url, m.group('path')),
  123. 'ie_key': JamendoIE.ie_key(),
  124. 'id': self._search_regex(
  125. r'/track/(\d+)', m.group('path'), 'track id', default=None),
  126. 'artist': artist,
  127. 'album': album,
  128. } for m in re.finditer(
  129. r'<a[^>]+href=(["\'])(?P<path>(?:(?!\1).)+)\1[^>]+class=["\'][^>]*js-trackrow-albumpage-link',
  130. webpage)]
  131. return self.playlist_result(entries, album_id, title)