logo

youtube-dl

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

deezer.py (3280B)


  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': r'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'__DZR_APP_STATE__\s*=\s*({.+?})\s*</script>',
  37. r'naboo\.display\(\'[^\']+\',\s*(.*?)\);\n'),
  38. webpage, 'data JSON')
  39. data = json.loads(data_json)
  40. playlist_title = data.get('DATA', {}).get('TITLE')
  41. playlist_uploader = data.get('DATA', {}).get('PARENT_USERNAME')
  42. playlist_thumbnail = self._search_regex(
  43. r'<img id="naboo_playlist_image".*?src="([^"]+)"', webpage,
  44. 'playlist thumbnail')
  45. preview_pattern = self._search_regex(
  46. r"var SOUND_PREVIEW_GATEWAY\s*=\s*'([^']+)';", webpage,
  47. 'preview URL pattern', fatal=False)
  48. entries = []
  49. for s in data['SONGS']['data']:
  50. puid = s['MD5_ORIGIN']
  51. preview_video_url = preview_pattern.\
  52. replace('{0}', puid[0]).\
  53. replace('{1}', puid).\
  54. replace('{2}', s['MEDIA_VERSION'])
  55. formats = [{
  56. 'format_id': 'preview',
  57. 'url': preview_video_url,
  58. 'preference': -100, # Only the first 30 seconds
  59. 'ext': 'mp3',
  60. }]
  61. self._sort_formats(formats)
  62. artists = ', '.join(
  63. orderedSet(a['ART_NAME'] for a in s['ARTISTS']))
  64. entries.append({
  65. 'id': s['SNG_ID'],
  66. 'duration': int_or_none(s.get('DURATION')),
  67. 'title': '%s - %s' % (artists, s['SNG_TITLE']),
  68. 'uploader': s['ART_NAME'],
  69. 'uploader_id': s['ART_ID'],
  70. 'age_limit': 16 if s.get('EXPLICIT_LYRICS') == '1' else 0,
  71. 'formats': formats,
  72. })
  73. return {
  74. '_type': 'playlist',
  75. 'id': playlist_id,
  76. 'title': playlist_title,
  77. 'uploader': playlist_uploader,
  78. 'thumbnail': playlist_thumbnail,
  79. 'entries': entries,
  80. }