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.

80 lines
2.8 KiB

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