This commit is contained in:
Mars 2024-06-02 06:03:21 -04:00
parent 6e5045f1f4
commit 693fa17d10
Signed by: pupbrained
GPG key ID: 0FF5B8826803F895
266 changed files with 60543 additions and 1000 deletions

View file

@ -1,9 +1,10 @@
#include "config.h"
#include <fmt/core.h>
#include <toml++/toml.h>
#include <unistd.h>
#include <rfl.hpp>
#include <rfl/toml.hpp>
#include <toml++/toml.h>
#include <unistd.h>
#include "config.h"
#define DEFINE_GETTER(class_name, type, name) \
type class_name::get##name() const { \
@ -79,126 +80,3 @@ ConfigImpl ConfigImpl::from_class(const Config& config) noexcept {
Config ConfigImpl::to_class() const {
return {general, now_playing, weather};
}
boost::json::object Weather::getWeatherInfo() const {
using namespace std;
using namespace cpr;
using namespace boost;
using namespace std::chrono;
const Location loc = this->m_Location;
const string apiKey = this->m_ApiKey;
const string units = this->m_Units;
// Define cache file and cache duration
const string cacheFile = "/tmp/weather_cache.json";
constexpr minutes cacheDuration = minutes(10);
logi("Cache file: {}", cacheFile);
logi("Cache duration: {} minutes",
duration_cast<minutes>(cacheDuration).count());
// Function to read cache from file
auto readCacheFromFile =
[&]() -> optional<pair<json::object, system_clock::time_point>> {
ifstream ifs(cacheFile);
if (!ifs.is_open()) {
logi("Cache file not found.");
return nullopt;
}
logi("Reading from cache file...");
json::object cachedData;
system_clock::time_point timestamp;
try {
json::value val;
ifs >> val;
cachedData = val.as_object();
string tsStr = cachedData["timestamp"].as_string().c_str();
timestamp = system_clock::time_point(milliseconds(stoll(tsStr)));
cachedData.erase("timestamp");
} catch (...) {
loge("Failed to read from cache file.");
return nullopt;
}
logi("Successfully read from cache file.");
return make_pair(cachedData, timestamp);
};
// Function to write cache to file
auto writeCacheToFile = [&](const json::object& data) {
fmt::println("Writing to cache file...");
ofstream ofs(cacheFile);
if (!ofs.is_open()) {
loge("Failed to open cache file for writing.");
return;
}
json::object dataToWrite = data;
dataToWrite["timestamp"] = to_string(
duration_cast<milliseconds>(system_clock::now().time_since_epoch())
.count());
ofs << json::serialize(dataToWrite);
logi("Successfully wrote to cache file.");
};
// Check if cache is valid
if (auto cachedData = readCacheFromFile()) {
auto [data, timestamp] = *cachedData;
if (system_clock::now() - timestamp < cacheDuration) {
logi("Cache is valid. Returning cached data.");
return data;
}
logi("Cache is expired.");
} else {
logi("No valid cache found.");
}
json::object result;
if (holds_alternative<string>(loc)) {
const string city = get<string>(loc);
const char* location = curl_easy_escape(nullptr, city.c_str(),
static_cast<int>(city.length()));
logi("City: {}", location);
logi("Making API request for city: {}", city);
const Response res =
Get(Url {fmt::format("https://api.openweathermap.org/data/2.5/"
"weather?q={}&appid={}&units={}",
location, apiKey, units)});
logi("Received response from API.");
json::value json = json::parse(res.text);
result = json.as_object();
} else {
const auto [lat, lon] = get<Coords>(loc);
logi("Coordinates: lat = {:.3f}, lon = {:.3f}", lat, lon);
logi("Making API request for coordinates.");
const Response res =
Get(Url {fmt::format("https://api.openweathermap.org/data/2.5/"
"weather?lat={:.3f}&lon={:.3f}&appid={}&units={}",
lat, lon, apiKey, units)});
logi("Received response from API.");
json::value json = json::parse(res.text);
result = json.as_object();
}
// Update the cache with the new data
writeCacheToFile(result);
logi("Returning new data.");
return result;
}