Skip to content

Commit

Permalink
- New '--skip-existing' option (fixes #37)
Browse files Browse the repository at this point in the history
- Version bump
  • Loading branch information
kaimi- committed Aug 12, 2022
1 parent 925a80b commit 85c3a40
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 18 deletions.
24 changes: 15 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ docker run --init --rm -v ${PWD}:/root/ --name yamusic yandex-music-downloader:1

## Usage
```bat
Yandex Music Downloader v1.2
Yandex Music Downloader v1.4
ya.pl [-adhklpstu] [long options...]
-p[=INT] --playlist[=INT] playlist id to download
Expand All @@ -127,6 +127,8 @@ ya.pl [-adhklpstu] [long options...]
-u[=STR] --url[=STR] download by URL
-d[=STR] --dir[=STR] download path (current direcotry will be
used by default)
--skip-existing skip downloading tracks that already exist
on the specified path
--proxy STR HTTP-proxy (format: 1.2.3.4:8888)
--exclude STR skip tracks specified in file
--include STR download only tracks specified in file
Expand All @@ -138,7 +140,7 @@ ya.pl [-adhklpstu] [long options...]
(Session_id=...)
--bitrate INT bitrate (eg. 64, 128, 192, 320)
--pattern STR track naming pattern
--path STR path pattern
--path STR path saving pattern
Available placeholders: #number, #artist,
#title, #album, #year
Expand All @@ -155,15 +157,19 @@ ya.pl [-adhklpstu] [long options...]
-h --help print usage
--include and --exclude options use weak
match i.e. ~/$term/
match i.e. ~/$term/
Example:
Example:
ya.pl -p 123 -k ya-playlist
ya.pl -a 123
ya.pl -a 123 -t 321
ya.pl -u https://music.yandex.ru/album/215690 --cookie ...
ya.pl -u https://music.yandex.ru/album/215688/track/1710808 --auth ...
ya.pl -u https://music.yandex.ru/users/ya.playlist/playlists/1257 --cookie ...
ya.pl -a 123
ya.pl -a 123 -t 321
ya.pl -u
https://music.yandex.ru/album/215690
--cookie ...
ya.pl -u
https://music.yandex.ru/album/215688/track/1710808 --auth ...
ya.pl -u
https://music.yandex.ru/users/ya.playlist/playlists/1257 --cookie ...
© 2013-2022 by Kaimi (https://kaimi.io)
```
Expand Down
58 changes: 49 additions & 9 deletions src/ya.pl
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
HQ_BITRATE => '320',
DEFAULT_CODEC => 'mp3',
PODCAST_TYPE => 'podcast',
VERSION => '1.3',
VERSION => '1.4',
COPYRIGHT => '© 2013-2022 by Kaimi (https://kaimi.io)',
};
use constant
Expand Down Expand Up @@ -197,6 +197,7 @@
['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 => '.'}],
['skip-existing', 'skip downloading tracks that already exist on the specified path'],
['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'],
Expand Down Expand Up @@ -416,6 +417,12 @@
last;
}
}

if($opt{skip_existing} && track_file_exists($track_info_ref))
{
$skip = 1;
}

if($skip)
{
info(INFO, 'Skipping: ' . $track_info_ref->{title});
Expand Down Expand Up @@ -503,20 +510,15 @@ sub fetch_track
info(ERROR, 'Failed to add MP3 tags for ' . $file_path);
}

my $target_path = $opt{dir};
if($opt{path})
{
$target_path = File::Spec->catdir($target_path, $track_info_ref->{storage_path});
}

my $file_util = File::Util->new();
if(!-d $file_util->make_dir($target_path => oct DEFAULT_PERMISSIONS => {if_not_exists => 1}))
my $target_path = create_storage_path($track_info_ref);
if(!$target_path)
{
info(ERROR, 'Failed to create: ' . $target_path);
return;
}

$target_path = File::Spec->catfile($target_path, $track_info_ref->{title} . FILE_SAVE_EXT);

if(rename_track($file_path, $target_path))
{
info(INFO, $file_path . ' -> ' . $target_path);
Expand All @@ -527,6 +529,44 @@ sub fetch_track
}
}

sub create_storage_path
{
my $track_info_ref = shift;

my $target_path = get_storage_path($track_info_ref);

my $file_util = File::Util->new();
if(!-d $file_util->make_dir($target_path => oct DEFAULT_PERMISSIONS => {if_not_exists => 1}))
{
return;
}

return $target_path;
}

sub track_file_exists
{
my $track_info_ref = shift;

my $target_path = get_storage_path($track_info_ref);
$target_path = File::Spec->catfile($target_path, $track_info_ref->{title} . FILE_SAVE_EXT);

return -e $target_path;
}

sub get_storage_path
{
my $track_info_ref = shift;

my $target_path = $opt{dir};
if($opt{path})
{
$target_path = File::Spec->catdir($target_path, $track_info_ref->{storage_path});
}

return $target_path;
}

sub download_track
{
my ($url) = @_;
Expand Down

0 comments on commit 85c3a40

Please sign in to comment.