#!/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 . 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 ...\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 $aid_fh; my $i; 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); open($aid_fh, ">", $dir . "/album-id"); printf($aid_fh "%d\n", $album_id); close($aid_fh); $i = 0; for $track (@{$res_data->{"results"}}) { printf(" Downloading track %d of %d...\n", ++$i, $res_data->{"headers"}->{"results_count"}); get_track($track, $audioformat, $dir); } } sub get_track { my ($track, $audioformat, $dir) = @_; my $name; my $audio_fname; my $req; my $res; my %fields; my $comments_fname; my $comments_fh; my $vorbis_field; my $jamendo_field; $name = $track->{"name"}; printf(" Title: %s\n", $name); $name =~ s|/|-|g; $audio_fname = sprintf("%s/%02d - %s.%s", $dir, $track->{"position"}, $name, $audioformat); $req = HTTP::Request->new("GET", $track->{"audio"}); $res = $ua->request($req, $audio_fname); if ($res->is_error()) { printf(STDERR "Error: Server returned HTTP status code %s\n", $res->code()); return undef; } undef($req); undef($res); %fields = ( "TITLE" => "name", "ALBUM" => "album_name", "TRACKNUMBER" => "position", "ARTIST" => "artist_name", "LICENSE" => "license_ccurl", "DATE" => "releasedate", ); $comments_fname = sprintf("%s/%02d - %s.comments", $dir, $track->{"position"}, $name); open($comments_fh, ">", $comments_fname); binmode($comments_fh, ":encoding(UTF-8)"); while (($vorbis_field, $jamendo_field) = each(%fields)) { $jamendo_field = $track->{$jamendo_field}; $jamendo_field =~ s/\n/\\n/g; $jamendo_field =~ s/\r/\\r/g; $jamendo_field =~ s/\\/\\\\/g; $jamendo_field =~ s/\0/\\0/g; printf($comments_fh "%s=%s\n", $vorbis_field, $jamendo_field); } close($comments_fh); system("vorbiscomment", "-w", "-R", "-e", "-c", $comments_fname, $audio_fname); } main();