From dff1df418b2f64cf9e23b504c11c343f1bbca464 Mon Sep 17 00:00:00 2001 From: Kaimi Date: Sun, 24 Jun 2018 19:54:18 +0300 Subject: [PATCH] Preliminary HQ support, some fixes --- src/ya.pl | 64 +++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 43 insertions(+), 21 deletions(-) diff --git a/src/ya.pl b/src/ya.pl index d365c9d..87728c6 100755 --- a/src/ya.pl +++ b/src/ya.pl @@ -18,7 +18,7 @@ use constant YANDEX_BASE => 'https://music.yandex.ru', MOBILE_YANDEX_BASE => 'https://api.music.yandex.net', MD5_SALT => 'XGRlBW9FXlekgbPrRHuSiA', - MUSIC_INFO_REGEX => qr{var\s+Mu\s+=\s+(.+?);\s+}is, + MUSIC_INFO_REGEX => qr{var\s+Mu\s*=\s*(.+?);\s*}is, DOWNLOAD_INFO_MASK => '/api/v1.5/handlers/api-jsonp.jsx?requestId=2&nc=%d&action=getTrackSrc&p=download-info/%s/2.mp3', MOBILE_DOWNLOAD_INFO_MASK => '/tracks/%d/download-info', DOWNLOAD_PATH_MASK => 'http://%s/get-mp3/%s/%s?track-id=%s&from=service-10-track&similarities-experiment=default', @@ -167,20 +167,25 @@ else my ($opt, $usage) = Getopt::Long::Descriptive::describe_options ( basename(__FILE__).' %o', - ['playlist|p:i', 'playlist id to download'], - ['kind|k:s', 'playlist kind (eg. ya-playlist, music-blog, music-partners, etc.)'], - ['album|a:i', 'album to download'], - ['track|t:i', 'track to download (album id must be specified)'], - ['url|u:s', 'download by URL'], - ['dir|d:s', 'download path (current direcotry will be used by default)', {default => '.'}], - ['proxy=s', 'HTTP-proxy (format: 1.2.3.4:8888)'], - ['exclude=s', 'skip tracks specified in file'], - ['include=s', 'download only tracks specified in file'], - ['delay=i', 'delay between downloads (in seconds)', {default => 5}], - ['mobile=i', 'use mobile API', {default => 1}], + ['playlist|p:i', 'playlist id to download'], + ['kind|k:s', 'playlist kind (eg. ya-playlist, music-blog, music-partners, etc.)'], + ['album|a:i', 'album to download'], + ['track|t:i', 'track to download (album id must be specified)'], + ['url|u:s', 'download by URL'], + ['dir|d:s', 'download path (current direcotry will be used by default)', {default => '.'}], + ['proxy=s', 'HTTP-proxy (format: 1.2.3.4:8888)'], + ['exclude=s', 'skip tracks specified in file'], + ['include=s', 'download only tracks specified in file'], + ['delay=i', 'delay between downloads (in seconds)', {default => 5}], + ['mobile=i', 'use mobile API', {default => 1}], + ['auth=s', 'authorization header (for HQ music if subscription is active)'], + ['bitrate=i', 'bitrate (eg. 64, 128, 192, 320)', {default => 192}], [], - ['debug', 'print debug info during work'], - ['help', 'print usage'], + ['Bitrate 320 is available only when subscription is active'], + ['and only via mobile API for now (be sure to specify Authorization header value)'], + [], + ['debug', 'print debug info during work'], + ['help', 'print usage'], [], ['--include and --exclude options use weak match i.e. ~/$term/'], [], @@ -208,6 +213,20 @@ if($opt{dir} && !-d $opt{dir}) exit(1); } +if($opt{bitrate} && $opt{bitrate} == 320) +{ + if(!$opt{auth}) + { + info(ERROR, 'Please, specify Authorization header value for downloading HQ music'); + exit(1); + } + if($opt{mobile} == 0) + { + info(ERROR, 'Specified bitrate is only available via mobile API'); + exit(1); + } +} + MP3::Tag->config('id3v23_unsync', 0); my ($whole_file, $total_size); my $ua = LWP::UserAgent->new @@ -476,6 +495,8 @@ sub get_track_url MOBILE_YANDEX_BASE.sprintf(MOBILE_DOWNLOAD_INFO_MASK, $track_id) : YANDEX_BASE.sprintf(DOWNLOAD_INFO_MASK, time, $storage_dir) + , + Authorization => ($opt{mobile} && $opt{auth}) ? $opt{auth} : '' ); if(!$request->is_success) { @@ -503,15 +524,16 @@ sub get_track_url if($opt{mobile}) { my ($idx, $target_idx) = (0, -1); - my $bitrate = 0; + my $bitrate = $opt{bitrate}; for my $track_info(@{$json->{result}}) { if($track_info->{codec} eq 'mp3') { - if($track_info->{bitrateInKbps} > $bitrate) + if($track_info->{bitrateInKbps} == $bitrate) { $bitrate = $track_info->{bitrateInKbps}; $target_idx = $idx; + last; } } @@ -539,11 +561,11 @@ sub get_track_url { %fields = ( - host => $json->{host}, - path => $json->{path}, - ts => $json->{ts}, - region => $json->{region}, - s => $json->{s} + host => $json->{host}[0], + path => $json->{path}[0], + ts => $json->{ts}[0], + region => $json->{region}[0], + s => $json->{s}[0] ); }