#pragma once #include #include #include #include #include #include "FileSystem.h" #include "XGConfig.h" #include "Logger.h" #include "LogHistory.h" namespace Downloader { enum class DOWNLOAD_MODE { VIDEO, AUDIO }; enum class DOWNLOAD_STATUS { QUEUED, DOWNLOADING, FINISHED, FAILED }; class DownloadEntry { public: DownloadEntry(); JasonPP::JsonBlock GetAsJson(); std::string title; std::string description; std::string uploader; int duration; std::string tubio_id; std::string webpage_url; std::string thumbnail_url; std::string downloaded_filename; std::string download_url; DOWNLOAD_STATUS status; DOWNLOAD_MODE mode; int download_progress; time_t queued_timestamp; }; class DownloadManager { public: static void PreInit(); static void Update(); static void OnExit(); static void PostExit(); /// /// Queues a video for download. Returns its tubio download id /// /// /// If video or audio /// Tubio download id static std::string QueueDownload(std::string url, DOWNLOAD_MODE mode); /// /// Returns the number of videos queued /// /// static std::size_t GetQueueLength(); /// /// Will return the whole cache in json format /// /// /// Maximum age of the entry in seconds. -1 = infinite /// /// Maximum of entries to fetch. -1 = infinite /// static JasonPP::JsonArray GetAlltimeCacheAsJson(time_t max_age, std::size_t max_num); /// /// Returns whether or not a tubio id exists /// /// The id to check static bool DoesTubioIDExist(std::string tubioId); /// /// Returns a reference to a DownloadEntry by its tubio id /// /// The corresponding tubio id static DownloadEntry& GetDownloadEntryByTubioID(std::string tubioId); /// /// Will delete all cached downloads! /// If downloads are currently active, tubio will wait for them to finish and return false! /// If no downloads are active it will clear immediately and return true /// static bool ClearDownloadCache(); /// /// Will remove an individual download entry by its tubio id /// /// static bool RemoveFromCacheByID(std::string id); private: static void Save(); static void Load(); static std::vector ParseJsonArrayToEntries(const JasonPP::JsonArray& arr); static void FetchInformation(std::string url, std::string tubId); static std::string CreateNewTubioID(); /// /// Will check for missing dependencies (windows only) and warn if missing /// static void WarnIfMissingDependenciesWIN(); static std::size_t GetNumActiveDownloads(); /// /// Will start a download-thread for the next queue-entry with status "queued" /// static void DownloadNext(); static void UpdateDownloadProgressPercentages(); static std::vector unfinishedCache; static std::vector downloadThreads; static JasonPP::JsonArray saveFileCache; // Content of the save-file static std::vector saveFileCache_Atomic; // Content of the save-file static Logging::Logger* log; // This gets set by other threads static time_t lastProgressCheck; static bool shouldSave; static bool shouldClearCacheASAP; }; }