diff --git a/CMakeLists.txt b/CMakeLists.txt index 3a64412..3de3781 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,7 @@ project(YTMusicDL26) add_executable(YTMusicDL26 src/main.cpp src/playlist.cpp + src/video.cpp src/utils.cpp ) diff --git a/src/main.cpp b/src/main.cpp index 27be95f..e2dea76 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,14 +9,20 @@ #include "config.hpp" #include "playlist.hpp" #include "utils.hpp" +#include "video.hpp" auto main(int argc, char* argv[]) -> int { auto musicDir = "/home/ethanscharlie/Downloads/tmp"; std::string url = "https://music.youtube.com/playlist?list=OLAK5uy_kwG9Ln_fS_R-Oa8F-JIgs8PPBoRgDe8ok"; + std::string videoUrl = "https://www.youtube.com/watch?v=WpmOb-Xanlg"; std::filesystem::create_directories(TMP_DOWNLOAD_FOLDER); - url = std::regex_replace(url, std::regex("music.youtube"), "www.youtube"); - auto playlist = Playlist(url); - playlist.rip(musicDir); + // url = std::regex_replace(url, std::regex("music.youtube"), "www.youtube"); + // auto playlist = Playlist(url); + // playlist.rip(musicDir); + + videoUrl = std::regex_replace(videoUrl, std::regex("music.youtube"), "www.youtube"); + auto video = Video(videoUrl); + video.rip(musicDir); } diff --git a/src/playlist.cpp b/src/playlist.cpp index ebc152f..accfe82 100644 --- a/src/playlist.cpp +++ b/src/playlist.cpp @@ -17,7 +17,9 @@ using json = nlohmann::json; -Playlist::Playlist(const std::string& playlistUrl) : playlistUrl(playlistUrl) {} +Playlist::Playlist(const std::string& playlistUrl) { + this->playlistUrl = std::regex_replace(playlistUrl, std::regex("music.youtube"), "www.youtube"); +} auto Playlist::rip(const std::string& musicFolder) -> bool { printf("Writting to data.json\n"); diff --git a/src/video.cpp b/src/video.cpp new file mode 100644 index 0000000..dddcebd --- /dev/null +++ b/src/video.cpp @@ -0,0 +1,106 @@ +#include "video.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "config.hpp" +#include "utils.hpp" + +using json = nlohmann::json; + +Video::Video(const std::string& videoUrl) { + this->videoUrl = std::regex_replace(videoUrl, std::regex("music.youtube"), "www.youtube"); +} + +auto Video::rip(const std::string& musicFolder) -> bool { + printf("Writting to data.json\n"); + json data = writeAndReturnDataJson(videoUrl); + + 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 Video::download() -> bool { + if (videoUrl == "") { + printf("ERROR: Tried to download empty video, %s\n", videoUrl.c_str()); + return false; + } + + if (!cleanTmpDownloadFolder()) { + printf("ERROR: Video download couldn't clean tmp download folder\n"); + return false; + } + + if (!downloadVideo(videoUrl)) { + printf("ERROR: Failed to download video\n"); + return false; + } + + printf("Finished downloading video %s\n", videoUrl.c_str()); + return true; +} + +auto Video::writeMetadata(const nlohmann::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()).filename().string() == "cover.jpg") continue; + + metadata(entry.path(), artist, album, index); + printf("Finished writting metadata for %s.\n", entry.path().c_str()); + std::filesystem::remove(entry.path()); + index++; + } + + printf("Finished writting metadata for %s by %s.\n", album.c_str(), artist.c_str()); + + return true; +} + +auto Video::aristAndAlbumName(const nlohmann::json& data, std::string& artist, std::string& album) -> bool { + artist = data["uploader"]; + artist = std::regex_replace(artist, std::regex(" - Topic"), ""); + + album = data["title"]; + album = std::regex_replace(album, std::regex("Album - "), ""); + + return true; +} diff --git a/src/video.hpp b/src/video.hpp new file mode 100644 index 0000000..d28d01f --- /dev/null +++ b/src/video.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include +#include + +class Video { + std::string videoUrl; + + public: + Video(const std::string& videoUrl); + auto rip(const std::string& musicFolder) -> bool; + + private: + auto download() -> bool; + auto writeMetadata(const nlohmann::json& data) -> bool; + auto aristAndAlbumName(const nlohmann::json& data, std::string& artist, std::string& album) -> bool; +};