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.

89 lines
3.1 KiB

  1. from __future__ import unicode_literals
  2. import json
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. ExtractorError,
  7. int_or_none,
  8. orderedSet,
  9. )
  10. class DeezerPlaylistIE(InfoExtractor):
  11. _VALID_URL = r'https?://(?:www\.)?deezer\.com/playlist/(?P<id>[0-9]+)'
  12. _TEST = {
  13. 'url': 'http://www.deezer.com/playlist/176747451',
  14. 'info_dict': {
  15. 'id': '176747451',
  16. 'title': 'Best!',
  17. 'uploader': 'Anonymous',
  18. 'thumbnail': 're:^https?://cdn-images.deezer.com/images/cover/.*\.jpg$',
  19. },
  20. 'playlist_count': 30,
  21. 'skip': 'Only available in .de',
  22. }
  23. def _real_extract(self, url):
  24. if 'test' not in self._downloader.params:
  25. self._downloader.report_warning('For now, this extractor only supports the 30 second previews. Patches welcome!')
  26. mobj = re.match(self._VALID_URL, url)
  27. playlist_id = mobj.group('id')
  28. webpage = self._download_webpage(url, playlist_id)
  29. geoblocking_msg = self._html_search_regex(
  30. r'<p class="soon-txt">(.*?)</p>', webpage, 'geoblocking message',
  31. default=None)
  32. if geoblocking_msg is not None:
  33. raise ExtractorError(
  34. 'Deezer said: %s' % geoblocking_msg, expected=True)
  35. data_json = self._search_regex(
  36. r'naboo\.display\(\'[^\']+\',\s*(.*?)\);\n', webpage, 'data JSON')
  37. data = json.loads(data_json)
  38. playlist_title = data.get('DATA', {}).get('TITLE')
  39. playlist_uploader = data.get('DATA', {}).get('PARENT_USERNAME')
  40. playlist_thumbnail = self._search_regex(
  41. r'<img id="naboo_playlist_image".*?src="([^"]+)"', webpage,
  42. 'playlist thumbnail')
  43. preview_pattern = self._search_regex(
  44. r"var SOUND_PREVIEW_GATEWAY\s*=\s*'([^']+)';", webpage,
  45. 'preview URL pattern', fatal=False)
  46. entries = []
  47. for s in data['SONGS']['data']:
  48. puid = s['MD5_ORIGIN']
  49. preview_video_url = preview_pattern.\
  50. replace('{0}', puid[0]).\
  51. replace('{1}', puid).\
  52. replace('{2}', s['MEDIA_VERSION'])
  53. formats = [{
  54. 'format_id': 'preview',
  55. 'url': preview_video_url,
  56. 'preference': -100, # Only the first 30 seconds
  57. 'ext': 'mp3',
  58. }]
  59. self._sort_formats(formats)
  60. artists = ', '.join(
  61. orderedSet(a['ART_NAME'] for a in s['ARTISTS']))
  62. entries.append({
  63. 'id': s['SNG_ID'],
  64. 'duration': int_or_none(s.get('DURATION')),
  65. 'title': '%s - %s' % (artists, s['SNG_TITLE']),
  66. 'uploader': s['ART_NAME'],
  67. 'uploader_id': s['ART_ID'],
  68. 'age_limit': 16 if s.get('EXPLICIT_LYRICS') == '1' else 0,
  69. 'formats': formats,
  70. })
  71. return {
  72. '_type': 'playlist',
  73. 'id': playlist_id,
  74. 'title': playlist_title,
  75. 'uploader': playlist_uploader,
  76. 'thumbnail': playlist_thumbnail,
  77. 'entries': entries,
  78. }