Fully working album download

This commit is contained in:
2026-08-20 11:02:06 -06:00
parent a33cab8326
commit ee378b19a3
5 changed files with 97 additions and 66 deletions
+47 -57
View File
@@ -19,6 +19,32 @@ using json = nlohmann::json;
Playlist::Playlist(const std::string& playlistUrl) : playlistUrl(playlistUrl) {}
auto Playlist::rip(const std::string& musicFolder) -> bool {
printf("Writting to data.json\n");
json data = writeAndReturnDataJson(playlistUrl);
if (!download()) {
printf("ERROR: Failed to download playlsit\n");
return false;
}
if (!writeMetadata(data)) {
printf("ERROR: Failed to write metatdata for playlsit\n");
return false;
}
std::string artist;
std::string album;
aristAndAlbumName(data, artist, album);
if (!moveAndSortTmpFilesToFolder(musicFolder, artist, album)) {
printf("ERROR: Failed to move files to music folder\n");
return false;
}
return true;
}
auto Playlist::download() -> bool {
std::vector<std::string> videoUrls = urlsFromPlaylist(playlistUrl);
@@ -57,27 +83,14 @@ auto Playlist::download() -> bool {
return true;
}
auto Playlist::writeMetadata() -> bool {
std::string artist = channelNamePlaylist();
std::string album = playlistTitle();
auto Playlist::writeMetadata(const json& data) -> bool {
std::string artist;
std::string album;
aristAndAlbumName(data, artist, album);
printf("Starting to write metadata for %s by %s.\n", album.c_str(), artist.c_str());
printf("Finding album cover.\n");
std::filesystem::remove("tmp/data.json");
ytdlp(
std::format("--js-runtimes \"deno:bin/linux/deno\" "
"--dump-single-json "
"\"{}\" "
"> tmp/data.json",
playlistUrl));
std::ifstream f("tmp/data.json");
if (!f) {
printf("ERROR: Couldn't open data.json\n");
return false;
}
json data = json::parse(f);
std::string coverUrl = data["thumbnails"][1]["url"];
downloadImageToTmpFolder(coverUrl);
@@ -98,19 +111,31 @@ auto Playlist::writeMetadata() -> bool {
return true;
}
auto Playlist::aristAndAlbumName(const nlohmann::json& data, std::string& artist, std::string& album) -> bool {
artist = data["entries"][0]["uploader"];
artist = std::regex_replace(artist, std::regex(" - Topic"), "");
album = data["title"];
album = std::regex_replace(album, std::regex("Album - "), "");
return true;
}
auto Playlist::urlsFromPlaylist(const std::string& url) -> std::vector<std::string> {
std::vector<std::string> urls;
std::filesystem::remove("urls.txt");
std::string urlsFileLocation = "tmp/urls.txt";
std::filesystem::remove(urlsFileLocation);
ytdlp(
std::format("--js-runtimes \"deno:bin/linux/deno\" "
"--quiet "
"--flat-playlist "
"--print-to-file \"%(webpage_url)s\" "
"urls.txt \"{}\"",
url));
"{} \"{}\"",
urlsFileLocation, url));
std::ifstream f("urls.txt");
std::ifstream f(urlsFileLocation);
if (!f.is_open()) {
printf("ERROR: Couldn't open urls.txt\n");
return urls;
@@ -124,38 +149,3 @@ auto Playlist::urlsFromPlaylist(const std::string& url) -> std::vector<std::stri
if (urls.empty()) printf("ERROR: Couldn't find any urls for playlist %s\n", url.c_str());
return urls;
}
auto Playlist::dataFromYtdlpPlaylist(const std::string& data) -> std::string {
ytdlp(
std::format("--quiet "
"--no-warnings "
"--flat-playlist "
"--playlist-items 1 "
"--print-to-file \"%({})s\" data.txt \"{}\"",
data, playlistUrl));
std::ifstream f("data.txt");
if (!f.is_open()) {
printf("ERROR: Couldn't open data.txt\n");
}
std::string s;
std::string line;
while (std::getline(f, line)) {
s = line;
}
return s;
};
auto Playlist::playlistTitle() -> std::string {
std::string s = dataFromYtdlpPlaylist("playlist_title");
s = std::regex_replace(s, std::regex("Album - "), "");
return s;
}
auto Playlist::channelNamePlaylist() -> std::string {
std::string s = dataFromYtdlpPlaylist("channel");
s = std::regex_replace(s, std::regex(" - Topic"), "");
return s;
}