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.

138 lines
4.6 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'https?://(?:www\.)?jamendo\.com/track/(?P<id>[0-9]+)/(?P<display_id>[^/?#&]+)'
  24. _TEST = {
  25. 'url': 'https://www.jamendo.com/track/196219/stories-from-emona-i',
  26. 'md5': '6e9e82ed6db98678f171c25a8ed09ffd',
  27. 'info_dict': {
  28. 'id': '196219',
  29. 'display_id': 'stories-from-emona-i',
  30. 'ext': 'flac',
  31. 'title': 'Maya Filipič - Stories from Emona I',
  32. 'artist': 'Maya Filipič',
  33. 'track': 'Stories from Emona I',
  34. 'duration': 210,
  35. 'thumbnail': r're:^https?://.*\.jpg'
  36. }
  37. }
  38. def _real_extract(self, url):
  39. mobj = self._VALID_URL_RE.match(url)
  40. track_id = mobj.group('id')
  41. display_id = mobj.group('display_id')
  42. webpage = self._download_webpage(url, display_id)
  43. title, artist, track = self._extract_meta(webpage)
  44. formats = [{
  45. 'url': 'https://%s.jamendo.com/?trackid=%s&format=%s&from=app-97dab294'
  46. % (sub_domain, track_id, format_id),
  47. 'format_id': format_id,
  48. 'ext': ext,
  49. 'quality': quality,
  50. } for quality, (format_id, sub_domain, ext) in enumerate((
  51. ('mp31', 'mp3l', 'mp3'),
  52. ('mp32', 'mp3d', 'mp3'),
  53. ('ogg1', 'ogg', 'ogg'),
  54. ('flac', 'flac', 'flac'),
  55. ))]
  56. self._sort_formats(formats)
  57. thumbnail = self._html_search_meta(
  58. 'image', webpage, 'thumbnail', fatal=False)
  59. duration = parse_duration(self._search_regex(
  60. r'<span[^>]+itemprop=["\']duration["\'][^>]+content=["\'](.+?)["\']',
  61. webpage, 'duration', fatal=False))
  62. return {
  63. 'id': track_id,
  64. 'display_id': display_id,
  65. 'thumbnail': thumbnail,
  66. 'title': title,
  67. 'duration': duration,
  68. 'artist': artist,
  69. 'track': track,
  70. 'formats': formats
  71. }
  72. class JamendoAlbumIE(JamendoBaseIE):
  73. _VALID_URL = r'https?://(?:www\.)?jamendo\.com/album/(?P<id>[0-9]+)/(?P<display_id>[\w-]+)'
  74. _TEST = {
  75. 'url': 'https://www.jamendo.com/album/121486/duck-on-cover',
  76. 'info_dict': {
  77. 'id': '121486',
  78. 'title': 'Shearer - Duck On Cover'
  79. },
  80. 'playlist': [{
  81. 'md5': 'e1a2fcb42bda30dfac990212924149a8',
  82. 'info_dict': {
  83. 'id': '1032333',
  84. 'ext': 'flac',
  85. 'title': 'Shearer - Warmachine',
  86. 'artist': 'Shearer',
  87. 'track': 'Warmachine',
  88. }
  89. }, {
  90. 'md5': '1f358d7b2f98edfe90fd55dac0799d50',
  91. 'info_dict': {
  92. 'id': '1032330',
  93. 'ext': 'flac',
  94. 'title': 'Shearer - Without Your Ghost',
  95. 'artist': 'Shearer',
  96. 'track': 'Without Your Ghost',
  97. }
  98. }],
  99. 'params': {
  100. 'playlistend': 2
  101. }
  102. }
  103. def _real_extract(self, url):
  104. mobj = self._VALID_URL_RE.match(url)
  105. album_id = mobj.group('id')
  106. webpage = self._download_webpage(url, mobj.group('display_id'))
  107. title, artist, album = self._extract_meta(webpage, fatal=False)
  108. entries = [{
  109. '_type': 'url_transparent',
  110. 'url': compat_urlparse.urljoin(url, m.group('path')),
  111. 'ie_key': JamendoIE.ie_key(),
  112. 'id': self._search_regex(
  113. r'/track/(\d+)', m.group('path'), 'track id', default=None),
  114. 'artist': artist,
  115. 'album': album,
  116. } for m in re.finditer(
  117. r'<a[^>]+href=(["\'])(?P<path>(?:(?!\1).)+)\1[^>]+class=["\'][^>]*js-trackrow-albumpage-link',
  118. webpage)]
  119. return self.playlist_result(entries, album_id, title)