summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorP. J. McDermott <pjm@nac.net>2013-07-21 23:11:35 (EDT)
committer P. J. McDermott <pjm@nac.net>2013-11-21 03:00:00 (EST)
commitaa96c05b73c7441c7b6cda950c80354a29222bb9 (patch)
tree74f5dc32cf4076e91de5ed41376f07e82fa1926b
downloadjamaldo-aa96c05b73c7441c7b6cda950c80354a29222bb9.zip
jamaldo-aa96c05b73c7441c7b6cda950c80354a29222bb9.tar.gz
jamaldo-aa96c05b73c7441c7b6cda950c80354a29222bb9.tar.bz2
Initial commit.
-rwxr-xr-xjamaldo.pl147
1 files changed, 147 insertions, 0 deletions
diff --git a/jamaldo.pl b/jamaldo.pl
new file mode 100755
index 0000000..2d00663
--- /dev/null
+++ b/jamaldo.pl
@@ -0,0 +1,147 @@
+#!/usr/bin/perl
+#
+# jamaldo - Jamendo Album Downloader
+#
+# Copyright (C) 2013 Patrick "P. J." McDermott
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+use strict;
+use warnings;
+
+use IO::Handle;
+use LWP::ConnCache;
+use LWP::UserAgent;
+use JSON::XS;
+
+use constant CLIENT_ID => "b6747d04";
+
+my $ua;
+
+sub main
+{
+ my $i;
+ my $album_id;
+
+ if ($#ARGV lt 0) {
+ print_usage(*STDERR);
+ exit(1);
+ }
+
+ STDOUT->autoflush();
+
+ $ua = LWP::UserAgent->new();
+ $ua->conn_cache(LWP::ConnCache->new());
+
+ $i = 0;
+ for $album_id (@ARGV) {
+ printf("Downloading album %d of %d...\n", ++$i, $#ARGV + 1);
+ get_album($album_id, "ogg");
+ }
+}
+
+sub print_usage
+{
+ my ($fh) = @_;
+
+ printf($fh "Usage: %s <album-id> ...\n", $0);
+}
+
+sub get_album
+{
+ my ($album_id, $audioformat) = @_;
+ my $url;
+ my $req;
+ my $res;
+ my $res_data;
+ my $artist_name;
+ my $album_name;
+ my $dir;
+ my $i;
+ my $control_fh;
+ my $track;
+
+ $url = "http://api.jamendo.com/v3.0/tracks/?client_id=" . CLIENT_ID
+ . "&format=json&limit=100&audioformat=" . $audioformat
+ . "&album_id=" . $album_id;
+ $req = HTTP::Request->new("GET" => $url);
+ $res = $ua->request($req);
+ if ($res->is_error()) {
+ printf(STDERR "Error: Server returned HTTP status code %s\n",
+ $res->code());
+ return undef;
+ }
+ $res_data = decode_json($res->content());
+ undef($url);
+ undef($req);
+ undef($res);
+
+ $artist_name = @{$res_data->{"results"}}[0]->{"artist_name"};
+ $album_name = @{$res_data->{"results"}}[0]->{"album_name"};
+ printf(" Artist: %s\n Album: %s\n", $artist_name, $album_name);
+
+ $artist_name =~ s|/|-|;
+ $album_name =~ s|/|-|;
+ $dir = $artist_name;
+ mkdir($dir) unless (-d $dir);
+ $dir .= "/" . $album_name;
+ mkdir($dir) unless (-d $dir);
+ undef($artist_name);
+ undef($album_name);
+
+ $i = 0;
+ open($control_fh, ">", $dir . "/control");
+ for $track (@{$res_data->{"results"}}) {
+ printf(" Downloading track %d of %d...\n", ++$i,
+ $res_data->{"headers"}->{"results_count"});
+ get_track($track, $i, $audioformat, $dir, $control_fh);
+ }
+ close($control_fh);
+}
+
+sub get_track
+{
+ my ($track, $track_number, $audioformat, $dir, $control_fh) = @_;
+ my $name;
+ my $req;
+ my $res;
+
+ $name = $track->{"name"};
+ printf(" Title: %s\n", $name);
+
+ $name =~ s|/|-|;
+ $name = sprintf("%s/%02d - %s.%s", $dir, $track_number, $name,
+ $audioformat);
+ $req = HTTP::Request->new("GET", $track->{"audio"});
+ $res = $ua->request($req, $name);
+ if ($res->is_error()) {
+ printf(STDERR "Error: Server returned HTTP status code %s\n",
+ $res->code());
+ return undef;
+ }
+ undef($name);
+ undef($req);
+ undef($res);
+
+ printf($control_fh "Title: %s\n", $track->{"name"});
+ printf($control_fh "Album: %s\n", $track->{"album_name"});
+ printf($control_fh "Track number: %d\n", $track_number);
+ printf($control_fh "Artist: %s\n", $track->{"artist_name"});
+ printf($control_fh "License: %s\n", $track->{"license_ccurl"});
+ printf($control_fh "Date: %s\n", $track->{"releasedate"});
+ printf($control_fh "\n");
+ $control_fh->flush();
+}
+
+main();