diff --git a/src/playlist.cpp b/src/playlist.cpp index 6ed07f4..51f94d9 100644 --- a/src/playlist.cpp +++ b/src/playlist.cpp @@ -25,25 +25,37 @@ auto Playlist::rip(const std::string& musicFolder) -> bool { printf("Writting to data.json\n"); json data = writeAndReturnDataJson(playlistUrl); + // Download the content as mp3 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; - } + // Get the name of the artist and album from data.json + std::string artist = data["entries"][0]["uploader"]; + artist = std::regex_replace(artist, std::regex(" - Topic"), ""); + std::string album = data["title"]; + album = std::regex_replace(album, std::regex("Album - "), ""); - 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()); + // Find the album cover from data.json + printf("Finding album cover.\n"); + std::string coverUrl = data["thumbnails"][1]["url"]; + + printf("Writting data to files.\n"); + writeMetadataToTmpFolder(artist, album, coverUrl); + + printf("Finished writting metadata for %s by %s.\n", album.c_str(), artist.c_str()); + + // Move the files into the users Music folder if (!moveAndSortTmpFilesToFolder(musicFolder, artist, album)) { printf("ERROR: Failed to move files to music folder\n"); return false; } + printf("Finished moving files for %s by %s.\n", album.c_str(), artist.c_str()); + return true; } @@ -85,44 +97,6 @@ auto Playlist::download() -> bool { return true; } -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::string coverUrl = data["thumbnails"][1]["url"]; - downloadImageToTmpFolder(coverUrl); - - printf("Writting data to files.\n"); - - size_t index = 1; - for (const auto& entry : std::filesystem::directory_iterator(TMP_DOWNLOAD_FOLDER)) { - if (std::filesystem::path(entry.path().string()).filename().string() == "cover.jpg") continue; - - metadata(entry.path().string(), artist, album, index); - printf("Finished writting metadata for %s.\n", entry.path().string().c_str()); - std::filesystem::remove(entry.path().string()); - index++; - } - - printf("Finished writting metadata for %s by %s.\n", album.c_str(), artist.c_str()); - - 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::vector urls; diff --git a/src/playlist.hpp b/src/playlist.hpp index e4570fe..4512663 100644 --- a/src/playlist.hpp +++ b/src/playlist.hpp @@ -13,7 +13,5 @@ class Playlist { private: auto download() -> bool; - auto writeMetadata(const nlohmann::json& data) -> bool; - auto aristAndAlbumName(const nlohmann::json& data, std::string& artist, std::string& album) -> bool; auto urlsFromPlaylist(const std::string& url) -> std::vector; }; diff --git a/src/utils.cpp b/src/utils.cpp index 6b04dd8..d49b187 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -39,9 +39,8 @@ auto metadata(const std::string& file, const std::string& artist, const std::str "-metadata artist=\"{}\" " "-metadata album=\"{}\" " "-metadata track=\"{}\" " - "\"{}/{}_{}.mp3\"", - BIN_FFMPEG, file, TMP_DOWNLOAD_FOLDER, name, artist, album, index, TMP_DOWNLOAD_FOLDER, index, - name) + "\"{}/_{}.mp3\"", + BIN_FFMPEG, file, TMP_DOWNLOAD_FOLDER, name, artist, album, index, TMP_DOWNLOAD_FOLDER, name) .c_str()); } @@ -54,13 +53,16 @@ auto cleanTmpDownloadFolder() -> bool { } auto downloadImageToTmpFolder(std::string url) -> bool { + printf("Downloading image with url %s\n", url.c_str()); + CURL* curl = curl_easy_init(); if (!curl) { printf("ERROR: Failed to initialize curl.\n"); return false; } - url = std::regex_replace(url, std::regex("https"), "http"); + // TODO Breaks album covers but this is needed for windows + // url = std::regex_replace(url, std::regex("https"), "http"); auto outputPath = std::filesystem::path(TMP_DOWNLOAD_FOLDER) / "cover.jpg"; FILE* file = std::fopen(outputPath.string().c_str(), "wb"); if (!file) { @@ -164,3 +166,19 @@ auto musicFolder() -> std::string { return ""; } + +auto writeMetadataToTmpFolder(const std::string& artist, const std::string& album, const std::string& coverUrl) + -> void { + downloadImageToTmpFolder(coverUrl); + + size_t index = 1; + for (const auto& entry : std::filesystem::directory_iterator(TMP_DOWNLOAD_FOLDER)) { + if (std::filesystem::path(entry.path().string()).filename().string() == "cover.jpg") continue; + + metadata(entry.path().string(), artist, album, index); + printf("Finished writting metadata for %s.\n", entry.path().string().c_str()); + std::filesystem::remove(entry.path().string()); + // TODO rename without underscore + index++; + } +} diff --git a/src/utils.hpp b/src/utils.hpp index 34743fb..8171754 100644 --- a/src/utils.hpp +++ b/src/utils.hpp @@ -20,3 +20,5 @@ auto moveAndSortTmpFilesToFolder(const std::string& folder, const std::string& a auto autoDetermineRip(const std::string& musicFolder, const std::string& url) -> bool; auto musicFolder() -> std::string; + +auto writeMetadataToTmpFolder(const std::string& artist, const std::string& album, const std::string& coverUrl) -> void; diff --git a/src/video.cpp b/src/video.cpp index 62eab37..0708c6d 100644 --- a/src/video.cpp +++ b/src/video.cpp @@ -74,21 +74,11 @@ auto Video::writeMetadata(const nlohmann::json& data) -> bool { printf("Starting to write metadata for %s by %s.\n", album.c_str(), artist.c_str()); - printf("Finding album cover.\n"); + printf("Finding thumbnail.\n"); std::string coverUrl = data["thumbnails"][1]["url"]; - downloadImageToTmpFolder(coverUrl); printf("Writting data to files.\n"); - - size_t index = 1; - for (const auto& entry : std::filesystem::directory_iterator(TMP_DOWNLOAD_FOLDER)) { - if (std::filesystem::path(entry.path().string()).filename().string() == "cover.jpg") continue; - - metadata(entry.path().string(), artist, album, index); - printf("Finished writting metadata for %s.\n", entry.path().string().c_str()); - std::filesystem::remove(entry.path().string()); - index++; - } + writeMetadataToTmpFolder(artist, album, coverUrl); printf("Finished writting metadata for %s by %s.\n", album.c_str(), artist.c_str());