weh
This commit is contained in:
parent
693fa17d10
commit
500138ce67
331 changed files with 12348 additions and 60593 deletions
|
@ -1,83 +1,77 @@
|
|||
#include <rfl.hpp>
|
||||
#include <rfl/toml.hpp>
|
||||
#include <toml++/toml.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "config.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace chrono;
|
||||
using namespace boost;
|
||||
|
||||
// Function to read cache from file
|
||||
optional<pair<json::object, system_clock::time_point>> ReadCacheFromFile() {
|
||||
std::optional<std::pair<co::Json, std::chrono::system_clock::time_point>>
|
||||
ReadCacheFromFile() {
|
||||
const string cacheFile = "/tmp/weather_cache.json";
|
||||
ifstream ifs(cacheFile);
|
||||
std::ifstream ifs(cacheFile);
|
||||
|
||||
if (!ifs.is_open()) {
|
||||
fmt::println("Cache file not found.");
|
||||
return nullopt;
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
fmt::println("Reading from cache file...");
|
||||
|
||||
json::object cachedData;
|
||||
system_clock::time_point timestamp;
|
||||
co::Json val;
|
||||
std::chrono::system_clock::time_point timestamp;
|
||||
|
||||
try {
|
||||
json::value val;
|
||||
ifs >> val;
|
||||
cachedData = val.as_object();
|
||||
std::stringstream buf;
|
||||
buf << ifs.rdbuf();
|
||||
|
||||
string tsStr = cachedData["timestamp"].as_string().c_str();
|
||||
timestamp = system_clock::time_point(milliseconds(stoll(tsStr)));
|
||||
val.parse_from(buf.str());
|
||||
|
||||
cachedData.erase("timestamp");
|
||||
string tsStr = val["timestamp"].as_string().c_str();
|
||||
timestamp = std::chrono::system_clock::time_point(
|
||||
std::chrono::milliseconds(stoll(tsStr)));
|
||||
|
||||
val.erase("timestamp");
|
||||
} catch (...) {
|
||||
fmt::println(stderr, "Failed to read from cache file.");
|
||||
|
||||
return nullopt;
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
fmt::println("Successfully read from cache file.");
|
||||
return make_pair(cachedData, timestamp);
|
||||
return make_pair(val, timestamp);
|
||||
}
|
||||
|
||||
// Function to write cache to file
|
||||
void WriteCacheToFile(const json::object& data) {
|
||||
void WriteCacheToFile(const co::Json& data) {
|
||||
const string cacheFile = "/tmp/weather_cache.json";
|
||||
fmt::println("Writing to cache file...");
|
||||
ofstream ofs(cacheFile);
|
||||
std::ofstream ofs(cacheFile);
|
||||
|
||||
if (!ofs.is_open()) {
|
||||
fmt::println(stderr, "Failed to open cache file for writing.");
|
||||
return;
|
||||
}
|
||||
|
||||
json::object dataToWrite = data;
|
||||
data["timestamp"] =
|
||||
std::to_string(duration_cast<std::chrono::milliseconds>(
|
||||
std::chrono::system_clock::now().time_since_epoch())
|
||||
.count());
|
||||
|
||||
dataToWrite["timestamp"] = to_string(
|
||||
duration_cast<milliseconds>(system_clock::now().time_since_epoch())
|
||||
.count());
|
||||
|
||||
ofs << json::serialize(dataToWrite);
|
||||
ofs << data.as_string();
|
||||
|
||||
fmt::println("Successfully wrote to cache file.");
|
||||
}
|
||||
|
||||
// Function to make API request
|
||||
json::object MakeApiRequest(const string& url) {
|
||||
co::Json MakeApiRequest(const string& url) {
|
||||
using namespace cpr;
|
||||
|
||||
fmt::println("Making API request...");
|
||||
const Response res = Get(Url {url});
|
||||
fmt::println("Received response from API.");
|
||||
json::value json = json::parse(res.text);
|
||||
return json.as_object();
|
||||
co::Json json = json::parse(res.text);
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
// Core function to get weather information
|
||||
json::object Weather::getWeatherInfo() const {
|
||||
co::Json Weather::getWeatherInfo() const {
|
||||
using namespace cpr;
|
||||
|
||||
const Location loc = m_Location;
|
||||
|
@ -86,10 +80,11 @@ json::object Weather::getWeatherInfo() const {
|
|||
|
||||
// Check if cache is valid
|
||||
if (auto cachedData = ReadCacheFromFile()) {
|
||||
auto [data, timestamp] = *cachedData;
|
||||
auto& [data, timestamp] = *cachedData;
|
||||
|
||||
if (system_clock::now() - timestamp <
|
||||
minutes(10)) { // Assuming cache duration is always 10 minutes
|
||||
if (std::chrono::system_clock::now() - timestamp <
|
||||
std::chrono::minutes(
|
||||
10)) { // Assuming cache duration is always 10 minutes
|
||||
fmt::println("Cache is valid. Returning cached data.");
|
||||
return data;
|
||||
}
|
||||
|
@ -99,7 +94,7 @@ json::object Weather::getWeatherInfo() const {
|
|||
fmt::println("No valid cache found.");
|
||||
}
|
||||
|
||||
json::object result;
|
||||
co::Json result;
|
||||
|
||||
if (holds_alternative<string>(loc)) {
|
||||
const string city = get<string>(loc);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue