Last active
September 12, 2015 08:01
-
-
Save LogIN-/bd1f8a807d0aba870eec to your computer and use it in GitHub Desktop.
download music from http://music.yandex.ru
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class YandexMusic { | |
public static function getAlbumInfo($url) { | |
$req = file_get_contents($url); | |
$data = preg_match('/var\s+Mu\s+=\s+(.+?);\s+<\/script>/is', $req, $matches); | |
$data = json_decode($matches[1], true); | |
return $data['pageData']; | |
} | |
public static function getPlaylistInfo($url) { | |
$data = static::getAlbumInfo($url); | |
return $data['playlist']; | |
} | |
public static function getTrackUrl($storage_dir) { | |
$req = file_get_contents(sprintf('http://music.yandex.ru/api/v1.5/handlers/api-jsonp.jsx?requestId=2&nc=%d&action=getTrackSrc&p=download-info/%s/2.mp3', time(), $storage_dir)); | |
$data = json_decode($req, true); | |
$hash = md5('XGRlBW9FXlekgbPrRHuSiA' . substr($data['path'], 1) . $data['s']); | |
$storage_dir = explode('.', $storage_dir); | |
$url = sprintf('http://%s/get-mp3/%s/%s?track-id=%s&from=service-10-track&similarities-experiment=default', $data['host'], $hash, $data['ts'] . $data['path'], $storage_dir[1]); | |
return $url; | |
} | |
public static function download($url, $path) { | |
$response = file_get_contents($url); | |
file_put_contents($path, $response); | |
} | |
public static function downloadAlbum($url, $path) { | |
$data = static::getAlbumInfo($url); | |
$playlist_dir = rtrim($path, '\/') . '/' . $data['title']; | |
if (!is_dir($playlist_dir)) | |
mkdir($playlist_dir); | |
$count = count($data['volumes'][0]); | |
$i = 1; | |
foreach ($data['volumes'][0] as $track) { | |
$mp3_path = $playlist_dir . '/' . str_pad($i, strlen((string)$count), '0', STR_PAD_LEFT) . '. ' . $track['artists'][0]['name'] . ' - ' . $track['title'] . '.mp3'; | |
$i++; | |
static::download(static::getTrackUrl($track['storageDir']), $mp3_path); | |
sleep(5); | |
} | |
} | |
public static function downloadTrack($url, $path) { | |
$data = static::getAlbumInfo($url); | |
$playlist_dir = rtrim($path, '\/') . '/'; | |
if (!is_dir($playlist_dir)) | |
mkdir($playlist_dir); | |
$track_id = substr($url, strrpos($url, '/') + 1); | |
var_dump($data['volumes'][0]); | |
foreach ($data['volumes'][0] as $track) { | |
if ($track['id'] == $track_id) { | |
$mp3_path = $playlist_dir . '/' . $track['artists'][0]['name'] . ' - ' . $track['title'] . '.mp3'; | |
static::download(static::getTrackUrl($track['storageDir']), $mp3_path); | |
return true; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment