module sites.basesite; import config.downloaderconfig; import sites.basesiteintf; /++ + This is the baseclass which all + other site classes inherit from +/ class BaseSite : BaseSiteIntf { protected: import std.stdio : writeln, writefln; import std.file : exists, rmdirRecurse, mkdir; import std.json : parseJSON, JSONValue; import std.array : replace, split; import std.string : indexOf; import std.net.curl : download; import std.parallelism : parallel; Config _config; // This function needs to be implemented by each derived site class abstract string getNameFromUrl(string url); // This function needs to be implemented by each derived site class abstract string[] getImageUrlsFromBase(string url); /++ + This function creates a folder with the supplied name. + If the folder already exists the folder will get deleted!! +/ void createOuputFolder(string foldername) { // Check if foler exits already if(exists(foldername)) { writefln(`[!] Folder with the name "%s" exists already...`, foldername); writeln("[!] Deleting it now!"); rmdirRecurse(foldername); } writefln(`[*] Creating folder "%s"`, foldername); mkdir(foldername); } /++ + This function extracts the name of a file from the supplied url +/ string extractFileNameFromUrl(string url) { string[] tmpString = url.split("/"); return tmpString[tmpString.length-1]; } /++ + This function downloads the images over the + url supplied in the `imageUrls` into the `outputPath` +/ void downloadImages(string[] imageUrls, string outputPath) { foreach(string url; parallel(imageUrls)) { // Extract the filename from the url string filepath = outputPath ~ extractFileNameFromUrl(url); if(_config.enable_debug_output) writefln("[i] Downloading from %s ==> %s", url, filepath); // Download the image download(url, filepath); } } /++ + Downloads a doujin from `url` into the `outputPath` +/ void downloadDoujinFromUrl(string url, string outputPath) { // Create a folder with the name of the managa createOuputFolder(outputPath); // Extract the urls of the managa images string[] urls = getImageUrlsFromBase(url); // Download the images over the extracted urls downloadImages(urls, outputPath); writeln("[*] Done downloading..."); } public: /++ + This constructor is to setup the site class with the + supplied `Config` +/ this(Config config) { // Set the config _config = config; } /++ + This function downloads a doujin from the supplied url +/ void downloadDoujin(string url) { // Get the name of the doujin string _foldername = _config.standard_download_folder ~ getNameFromUrl(url) ~ "/"; // Check if the folder already exists and `redownload_mangas_regardless` is set to false if(exists(_foldername) && !_config.redownload_mangas_regardless) { // Then stop downloading return; } if(_config.enable_debug_output) writefln("[i] _foldername is ----> %s", _foldername); // Download the doujin into a folder with the name of the doujin downloadDoujinFromUrl(url, _foldername); } /++ + This function downloads multiple doujins +/ void downloadDoujin(string[] urls) { foreach(string url; urls) { // Get the name of the doujin string _foldername = _config.standard_download_folder ~ getNameFromUrl(url) ~ "/"; if(_config.enable_debug_output) writefln("[i] _foldername is :s%", _foldername); // Check if the folder already exists and `redownload_mangas_regardless` is set to false if(exists(_foldername) && !_config.redownload_mangas_regardless) { // Then continue to the next url in the list continue; } // Download the doujin into a folder with the name of the doujin downloadDoujinFromUrl(url, _foldername); } } }