2017-05-16 15:53:21 +02:00
|
|
|
import std.stdio, std.regex, std.json, std.file, std.datetime, std.conv, std.process, std.net.curl, std.string;
|
2017-05-16 01:09:54 +02:00
|
|
|
import qobuz.api;
|
2018-01-04 14:25:53 +01:00
|
|
|
import etc.c.curl;
|
2017-05-16 01:09:54 +02:00
|
|
|
|
|
|
|
int main(string[] args)
|
|
|
|
{
|
2018-01-04 14:25:53 +01:00
|
|
|
string VERSION = "1.4";
|
2017-05-16 12:28:09 +02:00
|
|
|
|
2017-05-16 01:09:54 +02:00
|
|
|
if (args.length != 2) {
|
|
|
|
writefln("Usage: %s <album id or url>", args[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto path = thisExePath();
|
2017-05-16 20:31:34 +02:00
|
|
|
path = path.replaceFirst(regex("qobuz-get(\\.exe)?$"), "magic.json"); // HACK
|
2017-05-16 01:09:54 +02:00
|
|
|
string json;
|
|
|
|
try {
|
|
|
|
json = readText(path);
|
|
|
|
} catch (Exception e) {
|
|
|
|
writeln("Could not open magic.json!");
|
|
|
|
}
|
|
|
|
auto magic = parseJSON(json);
|
|
|
|
|
|
|
|
// strip url part if we have it
|
|
|
|
string id;
|
|
|
|
auto urlPart = regex("^https?://play.qobuz.com/album/");
|
|
|
|
if (args[1].matchFirst(urlPart)) {
|
|
|
|
id = args[1].replaceFirst(urlPart, "");
|
|
|
|
} else {
|
|
|
|
id = args[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
writeln("Looking up album...");
|
|
|
|
auto album = getAlbum(magic, id);
|
|
|
|
|
|
|
|
string title, artist, genre, year;
|
|
|
|
JSONValue[] tracks;
|
|
|
|
|
|
|
|
try {
|
|
|
|
title = album["title"].str;
|
|
|
|
artist = album["artist"]["name"].str;
|
2017-05-17 22:40:44 +02:00
|
|
|
genre = album["genre"]["name"].str;
|
2017-05-16 01:09:54 +02:00
|
|
|
auto releaseTime = SysTime.fromUnixTime(album["released_at"].integer, UTC());
|
|
|
|
year = releaseTime.year.text;
|
|
|
|
|
|
|
|
writefln("[ %s - %s (%s, %s) ]", artist, title, genre, year);
|
|
|
|
|
|
|
|
tracks = album["tracks"]["items"].array();
|
|
|
|
} catch (Exception e) {
|
|
|
|
writeln("Could not parse album data!");
|
|
|
|
return -4;
|
|
|
|
}
|
|
|
|
|
|
|
|
string dirName = artist~" - "~title~" ("~year~") [WEB FLAC]";
|
2017-12-06 17:23:28 +01:00
|
|
|
dirName = dirName.replaceAll(regex("[\\?<>:\"/\\\\|\\*]"), "");
|
2017-05-17 23:24:59 +02:00
|
|
|
|
2017-05-16 12:28:09 +02:00
|
|
|
try {
|
|
|
|
mkdir(dirName);
|
|
|
|
} catch (Exception e) {
|
|
|
|
writeln("Could not create directory: `"~dirName~"`. Does it exist already?");
|
|
|
|
return -9;
|
|
|
|
}
|
2017-05-16 01:09:54 +02:00
|
|
|
|
2017-05-17 22:40:44 +02:00
|
|
|
auto discs = tracks[tracks.length - 1]["media_number"].integer;
|
|
|
|
|
|
|
|
foreach (track; tracks) {
|
|
|
|
string url, num, discNum, trackName, trackArtist;
|
2017-05-16 01:09:54 +02:00
|
|
|
try {
|
2017-05-17 22:40:44 +02:00
|
|
|
num = track["track_number"].integer.text;
|
|
|
|
discNum = track["media_number"].integer.text;
|
2017-05-16 01:09:54 +02:00
|
|
|
trackName = track["title"].str;
|
2017-12-06 18:45:41 +01:00
|
|
|
try {
|
|
|
|
trackArtist = track["performer"]["name"].str;
|
|
|
|
} catch (Exception e) {
|
|
|
|
// Qobuz doesn't return a "performer" for all albums, and I'm not sure about
|
|
|
|
// the best way to deal with this. Leaving blank for now.A
|
|
|
|
trackArtist = "";
|
|
|
|
}
|
2017-05-16 01:09:54 +02:00
|
|
|
if (num.length < 2)
|
|
|
|
num = "0"~num;
|
2017-05-17 22:40:44 +02:00
|
|
|
writef(" [%s/%s] %s... ", discNum, num, trackName);
|
2017-05-16 01:09:54 +02:00
|
|
|
stdout.flush;
|
|
|
|
url = getDownloadUrl(magic, track["id"].integer.text);
|
|
|
|
} catch (Exception e) {
|
|
|
|
writeln("Failed to parse track data!");
|
|
|
|
return -7;
|
|
|
|
}
|
|
|
|
|
2017-05-17 22:40:44 +02:00
|
|
|
string discDir;
|
|
|
|
if (discs > 1)
|
|
|
|
discDir = dirName~"/Disc "~discNum;
|
|
|
|
else
|
|
|
|
discDir = dirName;
|
|
|
|
|
|
|
|
if (!discDir.exists || !discDir.isDir) {
|
|
|
|
try {
|
|
|
|
mkdir(discDir);
|
|
|
|
} catch (Exception e) {
|
|
|
|
writeln("Failed to create directory `"~discDir~"`.");
|
|
|
|
return -11;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-16 01:09:54 +02:00
|
|
|
try {
|
2017-05-17 23:24:59 +02:00
|
|
|
auto fileName = trackName;
|
2017-12-06 17:23:28 +01:00
|
|
|
fileName = fileName.replaceAll(regex("[\\?<>:\"/\\\\|\\*]"), "");
|
2017-12-08 19:03:39 +01:00
|
|
|
auto relPath = discDir~"/"~num~" - "~fileName~".flac";
|
|
|
|
|
|
|
|
version (Windows) {
|
|
|
|
// making up for NTFS/Windows inadequacy
|
|
|
|
// can't really do much better than truncating, sorry.
|
|
|
|
auto totalPath = getcwd() ~ relPath;
|
|
|
|
if (totalPath.length > 255) {
|
|
|
|
totalPath = totalPath[0..(totalPath.length - 4)];
|
|
|
|
relPath = relPath[0..(relPath.length - 4)];
|
|
|
|
while (totalPath.length > 250) {
|
|
|
|
totalPath = totalPath[0..(totalPath.length - 1)];
|
|
|
|
relPath = relPath[0..(relPath.length - 1)];
|
|
|
|
}
|
|
|
|
totalPath ~= ".flac";
|
|
|
|
relPath ~= ".flac";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 22:40:44 +02:00
|
|
|
auto pipes = pipeProcess([magic["ffmpeg"].str, "-i", "-", "-metadata", "title="~trackName, "-metadata", "artist="~trackArtist,
|
2017-12-06 23:58:27 +01:00
|
|
|
"-metadata", "album="~title, "-metadata", "date="~year, "-metadata", "track="~num, "-metadata", "genre="~genre,
|
2017-05-17 22:40:44 +02:00
|
|
|
"-metadata", "albumartist="~artist, "-metadata", "discnumber="~discNum, "-metadata", "tracktotal="~tracks.length.text,
|
2017-12-08 19:03:39 +01:00
|
|
|
"-metadata", "disctotal="~discs.text, relPath],
|
2017-05-17 22:40:44 +02:00
|
|
|
Redirect.stdin | Redirect.stderr | Redirect.stdout);
|
2018-01-04 14:25:53 +01:00
|
|
|
|
|
|
|
extern(C) static size_t writefunc(const ubyte* data, size_t size, size_t nmemb, void* p) {
|
|
|
|
auto pp = *(cast(ProcessPipes*) p);
|
|
|
|
pp.stdin.rawWrite(data[0..size*nmemb]);
|
|
|
|
pp.stdin.flush();
|
|
|
|
return size*nmemb;
|
2017-05-16 01:09:54 +02:00
|
|
|
}
|
2018-01-04 14:25:53 +01:00
|
|
|
|
|
|
|
CURL* curl;
|
|
|
|
CURLcode res;
|
|
|
|
curl = curl_easy_init();
|
|
|
|
curl_easy_setopt(curl, CurlOption.url, toStringz(url));
|
|
|
|
curl_easy_setopt(curl, CurlOption.followlocation, 1L);
|
|
|
|
curl_easy_setopt(curl, CurlOption.writefunction, cast(void*) &writefunc);
|
|
|
|
curl_easy_setopt(curl, CurlOption.writedata, cast(void*) &pipes);
|
|
|
|
res = curl_easy_perform(curl);
|
|
|
|
assert(res == CurlError.ok);
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
|
2017-05-16 01:09:54 +02:00
|
|
|
pipes.stdin.close;
|
|
|
|
wait(pipes.pid);
|
|
|
|
} catch (Exception e) {
|
|
|
|
writeln("Failed to download track! Check that ffmpeg is properly configured.");
|
2017-12-08 19:03:39 +01:00
|
|
|
writeln(e.msg);
|
2017-05-16 01:09:54 +02:00
|
|
|
return -8;
|
|
|
|
}
|
|
|
|
writeln("Done!");
|
|
|
|
}
|
|
|
|
|
2017-05-17 22:40:44 +02:00
|
|
|
string firstDisc;
|
|
|
|
if (discs > 1)
|
|
|
|
firstDisc = dirName~"/Disc 1";
|
|
|
|
else
|
|
|
|
firstDisc = dirName;
|
|
|
|
|
2017-05-16 12:28:09 +02:00
|
|
|
// Get album art
|
|
|
|
write("Getting album art... ");
|
|
|
|
stdout.flush;
|
2017-05-17 22:40:44 +02:00
|
|
|
download(id.getArtUrl, firstDisc~"/cover.jpg");
|
|
|
|
for (int i = 2; i <= discs; i++) {
|
|
|
|
copy(firstDisc~"/cover.jpg", dirName~"/Disc "~i.text~"/cover.jpg");
|
|
|
|
}
|
2017-05-16 12:28:09 +02:00
|
|
|
writeln("Done!");
|
|
|
|
|
2017-05-16 15:53:21 +02:00
|
|
|
string choice;
|
|
|
|
while (choice != "n" && choice != "y") {
|
|
|
|
write("Generate spectrals? [y/n] ");
|
2017-05-16 17:49:53 +02:00
|
|
|
stdout.flush;
|
2017-05-16 15:53:21 +02:00
|
|
|
choice = readln().chomp;
|
|
|
|
}
|
|
|
|
if (choice == "y") {
|
2017-05-16 18:11:56 +02:00
|
|
|
try {
|
2017-05-17 23:24:59 +02:00
|
|
|
auto trackName = tracks[0]["title"].str;
|
2017-12-06 17:23:28 +01:00
|
|
|
trackName = trackName.replaceAll(regex("[\\?<>:\"/\\\\|\\*]"), "");
|
2017-05-17 23:24:59 +02:00
|
|
|
|
|
|
|
auto full = execute([magic["sox"].str, firstDisc~"/01 - "~trackName~".flac", "-n", "remix", "1", "spectrogram",
|
2017-05-16 18:11:56 +02:00
|
|
|
"-x", "3000", "-y", "513", "-z", "120", "-w", "Kaiser", "-o", "SpecFull.png"]);
|
2017-05-17 23:24:59 +02:00
|
|
|
auto zoom = execute([magic["sox"].str, firstDisc~"/01 - "~trackName~".flac", "-n", "remix", "1", "spectrogram",
|
2017-05-16 18:11:56 +02:00
|
|
|
"-X", "500", "-y", "1025", "-z", "120", "-w", "Kaiser", "-S", "0:30", "-d", "0:04", "-o", "SpecZoom.png"]);
|
|
|
|
if (full.status != 0 || zoom.status != 0)
|
2017-05-17 22:40:44 +02:00
|
|
|
throw new Exception("sox failed");
|
2017-05-16 15:53:21 +02:00
|
|
|
writeln("SpecFull.png and SpecZoom.png written.");
|
2017-05-16 18:11:56 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
writeln("Generating spectrals failed! Is sox configured properly?");
|
|
|
|
}
|
2017-05-16 17:49:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
choice = null;
|
|
|
|
while (choice != "n" && choice != "y") {
|
|
|
|
write("Create .torrent file? [y/n] ");
|
|
|
|
stdout.flush;
|
|
|
|
choice = readln().chomp;
|
|
|
|
}
|
|
|
|
if (choice == "y") {
|
|
|
|
write("Announce URL: ");
|
|
|
|
stdout.flush;
|
|
|
|
string announce = readln().chomp;
|
|
|
|
|
2017-05-16 18:11:56 +02:00
|
|
|
try {
|
2017-05-16 17:49:53 +02:00
|
|
|
auto t = execute([magic["mktorrent"].str, "-l", "20", "-a", announce, dirName]);
|
|
|
|
if (t.status != 0)
|
2017-05-16 18:16:14 +02:00
|
|
|
throw new Exception("mktorrent failed");
|
2017-05-16 18:11:56 +02:00
|
|
|
writeln("'"~dirName~".torrent' created.");
|
|
|
|
} catch (Exception e) {
|
2017-05-16 17:49:53 +02:00
|
|
|
writeln("Creating .torrent file failed! Is mktorrent configured properly?");
|
2017-05-16 18:11:56 +02:00
|
|
|
}
|
2017-05-16 15:53:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
writeln("All done, exiting.");
|
|
|
|
|
2017-05-16 01:09:54 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2017-12-06 17:23:28 +01:00
|
|
|
|
|
|
|
// ex: set tabstop=2 expandtab:
|