mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-04-05 18:09:04 +00:00
Reorganize directory structure
This commit is contained in:
46
pipeline/src/data_generation/data_downloader.cpp
Normal file
46
pipeline/src/data_generation/data_downloader.cpp
Normal file
@@ -0,0 +1,46 @@
|
||||
#include "data_generation/data_downloader.h"
|
||||
#include "web_client/web_client.h"
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
|
||||
DataDownloader::DataDownloader(std::shared_ptr<IWebClient> webClient)
|
||||
: m_webClient(std::move(webClient)) {}
|
||||
|
||||
DataDownloader::~DataDownloader() {}
|
||||
|
||||
bool DataDownloader::FileExists(const std::string &filePath) {
|
||||
return std::filesystem::exists(filePath);
|
||||
}
|
||||
|
||||
std::string
|
||||
DataDownloader::DownloadCountriesDatabase(const std::string &cachePath,
|
||||
const std::string &commit) {
|
||||
if (FileExists(cachePath)) {
|
||||
spdlog::info("[DataDownloader] Cache hit: {}", cachePath);
|
||||
return cachePath;
|
||||
}
|
||||
|
||||
std::string shortCommit = commit;
|
||||
if (commit.length() > 7) {
|
||||
shortCommit = commit.substr(0, 7);
|
||||
}
|
||||
|
||||
std::string url = "https://raw.githubusercontent.com/dr5hn/"
|
||||
"countries-states-cities-database/" +
|
||||
shortCommit + "/json/countries+states+cities.json";
|
||||
|
||||
spdlog::info("[DataDownloader] Downloading: {}", url);
|
||||
|
||||
m_webClient->DownloadToFile(url, cachePath);
|
||||
|
||||
std::ifstream fileCheck(cachePath, std::ios::binary | std::ios::ate);
|
||||
std::streamsize size = fileCheck.tellg();
|
||||
fileCheck.close();
|
||||
|
||||
spdlog::info("[DataDownloader] OK: Download complete: {} ({:.2f} MB)",
|
||||
cachePath, (size / (1024.0 * 1024.0)));
|
||||
return cachePath;
|
||||
}
|
||||
Reference in New Issue
Block a user