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.

127 lines
4.3 KiB

  1. # coding=utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. import hashlib
  5. from .common import InfoExtractor
  6. from ..compat import compat_str
  7. from ..utils import (
  8. int_or_none,
  9. float_or_none,
  10. )
  11. class YandexMusicBaseIE(InfoExtractor):
  12. def _get_track_url(self, storage_dir, track_id):
  13. data = self._download_json(
  14. 'http://music.yandex.ru/api/v1.5/handlers/api-jsonp.jsx?action=getTrackSrc&p=download-info/%s'
  15. % storage_dir,
  16. track_id, 'Downloading track location JSON')
  17. key = hashlib.md5(('XGRlBW9FXlekgbPrRHuSiA' + data['path'][1:] + data['s']).encode('utf-8')).hexdigest()
  18. storage = storage_dir.split('.')
  19. return ('http://%s/get-mp3/%s/%s?track-id=%s&from=service-10-track&similarities-experiment=default'
  20. % (data['host'], key, data['ts'] + data['path'], storage[1]))
  21. def _get_track_info(self, track):
  22. return {
  23. 'id': track['id'],
  24. 'ext': 'mp3',
  25. 'url': self._get_track_url(track['storageDir'], track['id']),
  26. 'title': '%s - %s' % (track['artists'][0]['name'], track['title']),
  27. 'filesize': int_or_none(track.get('fileSize')),
  28. 'duration': float_or_none(track.get('durationMs'), 1000),
  29. }
  30. class YandexMusicTrackIE(YandexMusicBaseIE):
  31. IE_NAME = 'yandexmusic:track'
  32. IE_DESC = 'Яндекс.Музыка - Трек'
  33. _VALID_URL = r'https?://music\.yandex\.(?:ru|kz|ua|by)/album/(?P<album_id>\d+)/track/(?P<id>\d+)'
  34. _TEST = {
  35. 'url': 'http://music.yandex.ru/album/540508/track/4878838',
  36. 'md5': 'f496818aa2f60b6c0062980d2e00dc20',
  37. 'info_dict': {
  38. 'id': '4878838',
  39. 'ext': 'mp3',
  40. 'title': 'Carlo Ambrosio - Gypsy Eyes 1',
  41. 'filesize': 4628061,
  42. 'duration': 193.04,
  43. }
  44. }
  45. def _real_extract(self, url):
  46. mobj = re.match(self._VALID_URL, url)
  47. album_id, track_id = mobj.group('album_id'), mobj.group('id')
  48. track = self._download_json(
  49. 'http://music.yandex.ru/handlers/track.jsx?track=%s:%s' % (track_id, album_id),
  50. track_id, 'Downloading track JSON')['track']
  51. return self._get_track_info(track)
  52. class YandexMusicAlbumIE(YandexMusicBaseIE):
  53. IE_NAME = 'yandexmusic:album'
  54. IE_DESC = 'Яндекс.Музыка - Альбом'
  55. _VALID_URL = r'https?://music\.yandex\.(?:ru|kz|ua|by)/album/(?P<id>\d+)/?(\?|$)'
  56. _TEST = {
  57. 'url': 'http://music.yandex.ru/album/540508',
  58. 'info_dict': {
  59. 'id': '540508',
  60. 'title': 'Carlo Ambrosio - Gypsy Soul (2009)',
  61. },
  62. 'playlist_count': 50,
  63. }
  64. def _real_extract(self, url):
  65. album_id = self._match_id(url)
  66. album = self._download_json(
  67. 'http://music.yandex.ru/handlers/album.jsx?album=%s' % album_id,
  68. album_id, 'Downloading album JSON')
  69. entries = [self._get_track_info(track) for track in album['volumes'][0]]
  70. title = '%s - %s' % (album['artists'][0]['name'], album['title'])
  71. year = album.get('year')
  72. if year:
  73. title += ' (%s)' % year
  74. return self.playlist_result(entries, compat_str(album['id']), title)
  75. class YandexMusicPlaylistIE(YandexMusicBaseIE):
  76. IE_NAME = 'yandexmusic:playlist'
  77. IE_DESC = 'Яндекс.Музыка - Плейлист'
  78. _VALID_URL = r'https?://music\.yandex\.(?:ru|kz|ua|by)/users/[^/]+/playlists/(?P<id>\d+)'
  79. _TEST = {
  80. 'url': 'http://music.yandex.ru/users/music.partners/playlists/1245',
  81. 'info_dict': {
  82. 'id': '1245',
  83. 'title': 'Что слушают Enter Shikari',
  84. 'description': 'md5:3b9f27b0efbe53f2ee1e844d07155cc9',
  85. },
  86. 'playlist_count': 6,
  87. }
  88. def _real_extract(self, url):
  89. playlist_id = self._match_id(url)
  90. webpage = self._download_webpage(url, playlist_id)
  91. playlist = self._parse_json(
  92. self._search_regex(
  93. r'var\s+Mu\s*=\s*({.+?});\s*</script>', webpage, 'player'),
  94. playlist_id)['pageData']['playlist']
  95. entries = [self._get_track_info(track) for track in playlist['tracks']]
  96. return self.playlist_result(
  97. entries, compat_str(playlist_id),
  98. playlist['title'], playlist.get('description'))