OOP structure

This commit is contained in:
2026-08-20 10:27:29 -06:00
parent 01b51efa63
commit a33cab8326
7 changed files with 303 additions and 238 deletions
+99
View File
@@ -0,0 +1,99 @@
#include <curl/curl.h>
#include <curl/easy.h>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <filesystem>
#include <format>
#include <nlohmann/json.hpp>
#include <regex>
#include <string>
#include "config.hpp"
// This runs in shell, if put through a website interface it would allow for injection
auto ytdlp(const std::string& args) -> bool {
system(std::format("bin/linux/yt-dlp {}", args).c_str());
return true;
}
auto metadata(const std::string& file, const std::string& artist, const std::string& album, size_t index) -> void {
std::string name = std::filesystem::path(file).filename().string();
name = std::regex_replace(name, std::regex(".mp3"), "");
system(std::format("bin/linux/ffmpeg "
"-hide_banner -loglevel error "
"-i \"{}\" "
"-i \"{}/cover.jpg\" "
"-map 0:0 "
"-map 1:0 "
"-c copy "
"-id3v2_version 3 -metadata:s:v title=\"Album cover\" -metadata:s:v comment=\"Cover (front)\" "
"-metadata title=\"{}\" "
"-metadata artist=\"{}\" "
"-metadata album=\"{}\" "
"-metadata track=\"{}\" "
"\"{}/{}_{}.mp3\"",
file, TMP_DOWNLOAD_FOLDER, name, artist, album, index, TMP_DOWNLOAD_FOLDER, index, name)
.c_str());
}
auto cleanTmpDownloadFolder() -> bool {
for (const auto& entry : std::filesystem::directory_iterator(TMP_DOWNLOAD_FOLDER)) {
std::filesystem::remove_all(entry.path());
}
return true;
}
auto downloadImageToTmpFolder(const std::string& url) -> bool {
CURL* curl = curl_easy_init();
if (!curl) {
printf("ERROR: Failed to initialize curl.\n");
return false;
}
FILE* file = std::fopen(std::format("{}/cover.jpg", TMP_DOWNLOAD_FOLDER).c_str(), "wb");
if (!file) {
curl_easy_cleanup(curl);
printf("ERROR: Failed to create cover file.\n");
return false;
}
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, nullptr);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
CURLcode res = curl_easy_perform(curl);
std::fclose(file);
curl_easy_cleanup(curl);
if (res != CURLE_OK) {
printf("ERROR: %s\n", curl_easy_strerror(res));
return false;
}
return true;
}
auto downloadVideo(const std::string& url) -> bool {
if (url == "") {
printf("ERROR: Cannot download video with empty url\n");
return false;
}
ytdlp(
std::format("--js-runtimes \"deno:bin/linux/deno\" "
"--quiet "
"-f \"bestaudio/best\" "
"-x --audio-format mp3 "
"-o \"{}/%(title)s.%(ext)s\" "
"{}",
TMP_DOWNLOAD_FOLDER, url));
printf("Finished downloading video %s.\n", url.c_str());
return true;
}