aboutsummaryrefslogtreecommitdiff
path: root/youtube_dl/extractor/yamusic.py
blob: 5af6df89db21e23e4f6aeb58ec5b9ca2307bb671 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# coding=utf-8
from __future__ import unicode_literals

import re
import hashlib
import time

from .common import InfoExtractor

class YandexMusicAlbumIE(InfoExtractor):
    _VALID_URL = r'http://music.yandex.ru/album/(?P<id>\d+)'

    def _get_track_url(self, storage_dir, track_id):
        data = self._download_json('http://music.yandex.ru/api/v1.5/handlers/api-jsonp.jsx?requestId=2&nc=%d&action=getTrackSrc&p=download-info/%s/2.mp3' % (time.time(), storage_dir), track_id)

        hsh = hashlib.md5()
        hsh.update('XGRlBW9FXlekgbPrRHuSiA' + data['path'][1:] + data['s'])
        hash = hsh.hexdigest()
        storage = storage_dir.split('.')

        return 'http://%s/get-mp3/%s/%s?track-id=%s&from=service-10-track&similarities-experiment=default' % (data['host'], hash, data['ts'] + data['path'], storage[1])

    def _get_album_id_and_data(self, url):
        matched = re.match(self._VALID_URL, url)
        id = matched.group('id')

        webpage = self._download_webpage(url, id)
        data = self._parse_json(
            self._search_regex(
                r'var\s+Mu\s+=\s+(.+?);\s+<\/script>', webpage, 'player'),
            id)
        return id, data['pageData']

    def _real_extract(self, url):

        id, data = self._get_album_id_and_data(url)

        entries = []

        for track in data['volumes'][0]:
            entries.append({
                'id': track['id'],
                'ext': 'mp3',
                'url': self._get_track_url(track['storageDir'], track['id']),
                'title': track['artists'][0]['name'] + ' - ' + track['title'],
            })

        return {
            '_type': 'playlist',
            'entries': entries,
            'id': id,
            'title': data['title'],
        }

class YandexMusicPlaylistIE(YandexMusicAlbumIE):
    _VALID_URL = r'http://music.yandex.ru/users/(?P<user_name>[^/]+)/playlists/(?P<id>\d+)'

    def _real_extract(self, url):
        id, data = self._get_album_id_and_data(url)
        data = data['playlist']

        entries = []

        for track in data['tracks']:
            entries.append({
                'id': track['id'],
                'ext': 'mp3',
                'url': self._get_track_url(track['storageDir'], track['id']),
                'title': track['artists'][0]['name'] + ' - ' + track['title'],
            })

        return {
            '_type': 'playlist',
            'entries': entries,
            'id': id,
            'title': data['title'],
        }

class YandexMusicTrackIE(YandexMusicAlbumIE):
    _VALID_URL = r'http://music.yandex.ru/album/(?P<album_id>\d+)/track/(?P<id>\d+)'
    _TEST = {
        'url': 'http://music.yandex.ru/album/540508/track/4878838',
        'info_dict': {
            'id': '4878838',
            'ext': 'mp3',
            'title': 'Carlo Ambrosio - Gypsy Eyes 1',
        }
    }

    def _real_extract(self, url):

        id, data = self._get_album_id_and_data(url)

        for track in data['volumes'][0]:
            if track['id'] == id:
                track_url = self._get_track_url(track['storageDir'], id)
                break

        return {
            'id': id,
            'ext': 'mp3',
            'url': track_url,
            'title': track['artists'][0]['name'] + ' - ' + track['title'],
        }