From 6ccd8fcbcb81ce2a9ba7e52bfb22ef92e8ac13af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Goli=C5=84ski?= Date: Tue, 5 Nov 2019 20:39:33 +0100 Subject: [PATCH] Add date of find to Cache structure. Update OKApi source to grab it. It is not the cleanest solution possible. Maybe Caches should be a std::map, not std::set... --- cache.cpp | 8 +++++++- cache.h | 3 +++ okapi.cpp | 39 ++++++++++++++++++++++++++------------- okapi.h | 4 +++- 4 files changed, 39 insertions(+), 15 deletions(-) diff --git a/cache.cpp b/cache.cpp index f910b4d..fde2af1 100644 --- a/cache.cpp +++ b/cache.cpp @@ -1,6 +1,8 @@ #include "cache.h" #include +#include + const static int Earth_radius = 6378; Position Cache::home; @@ -11,7 +13,7 @@ static float degtorad(float x) { void Cache::show() const { std::cout << "Cache:\t" << code << ' ' << name << " (type: " << type << ", owned by " << owner << ")\n"; - std::cout << '\t' << pos.lat << " " << pos.lon << "\t\t D/T: " << diff << '/' << terr << '\n'; + std::cout << '\t' << pos.lat << " " << pos.lon << "\t\tD/T: " << diff << '/' << terr << "\t\tFound on " << date() << '\n'; } std::string Cache::link() const { @@ -22,6 +24,10 @@ float Cache::distance() const { return 2 * Earth_radius * asin(sqrt(pow(sin(degtorad((pos.lat - home.lat) / 2)), 2) + cos(degtorad(pos.lat)) * cos(degtorad(home.lat)) * pow(sin(degtorad((pos.lon - home.lon) / 2)), 2))); } +std::string Cache::date() const { + return std::to_string(date_found.tm_year+1900) + '-' + std::to_string(date_found.tm_mon) + "-" + std::to_string(date_found.tm_mday); +} + bool CacheCmpNS(const Cache& lhs, const Cache& rhs) { return lhs.pos.lat < rhs.pos.lat; } diff --git a/cache.h b/cache.h index 08688c5..c869e3a 100644 --- a/cache.h +++ b/cache.h @@ -3,6 +3,7 @@ #include #include #include +#include // enum Service { // gc, @@ -50,12 +51,14 @@ public: std::string origin; std::string owner; std::string owner_uuid; + std::tm date_found; static Position home; void show() const; std::string link() const; float distance() const; + std::string date() const; }; class CacheCmp { diff --git a/okapi.cpp b/okapi.cpp index 9aac9e5..c08c1cc 100644 --- a/okapi.cpp +++ b/okapi.cpp @@ -4,6 +4,8 @@ #include #include +#include +#include #include #include @@ -61,7 +63,7 @@ std::string Okapi::curl_post(std::string url, std::string post) { std::string Okapi::get_user_caches_json(std::string uuid, int count, int offset) { std::string service = url + OKAPI_logs; - std::string query = "consumer_key=" + key + "&user_uuid=" + uuid + "&fields=cache_code|type&limit=" + std::to_string(count) + "&offset=" + std::to_string(offset); + std::string query = "consumer_key=" + key + "&user_uuid=" + uuid + "&fields=cache_code|type|date&limit=" + std::to_string(count) + "&offset=" + std::to_string(offset); return curl_post(service, query); } @@ -100,17 +102,18 @@ std::string Okapi::get_caches_json(std::string codes) { // return c; // } -Caches Okapi::get_caches(std::vector codes) { - Caches cc; +Caches Okapi::get_caches(std::vector> codes) { Cache c; + Caches cc; - while (codes.size() > 0) { + uint n = 0; + while (n < codes.size()) { std::string codes_list; - int n = (codes.size() > 500) ? 500 : codes.size(); - for (int i = 0; i < n; i++) { - codes_list += codes.back(); +// int n = (codes.size() > 500) ? 500 : cc.codes(); + while (n < codes.size() && n < 500) { + codes_list += codes.at(n).first; codes_list += '|'; - codes.pop_back(); + n++; } codes_list.pop_back(); // remove trailing '|' @@ -128,6 +131,13 @@ Caches Okapi::get_caches(std::vector codes) { c.owner_uuid = el.value()["owner"]["uuid"]; c.pos = get_lat_lon(el.value()["location"]); c.origin = "OC.pl"; + //ugly way to handle date... + for (auto& el2 : codes) { + if (el2.first == c.code) { + c.date_found = el2.second; + break; + } + } cc.insert(c); } } @@ -136,9 +146,9 @@ Caches Okapi::get_caches(std::vector codes) { Caches Okapi::get_user_caches(std::string uuid, int count) { Caches cc; - std::vector codes; + std::vector> codes; json j; - + std::tm date; int off = 0; if (count == 0) @@ -148,7 +158,9 @@ Caches Okapi::get_user_caches(std::string uuid, int count) { for (auto& el : j.items()) { if (el.value()["type"] == "Found it") { - codes.emplace_back(el.value()["cache_code"]); + std::stringstream ss(el.value()["date"].get()); + ss >> std::get_time(&date, "%Y-%m-%dT%H-%M-%S"); + codes.push_back(std::pair(el.value()["cache_code"].get(), date)); } } off += j.size(); @@ -161,7 +173,9 @@ Caches Okapi::get_user_caches(std::string uuid, int count) { for (auto& el : j.items()) { if (el.value()["type"] == "Found it") { - codes.emplace_back(el.value()["cache_code"]); + std::stringstream ss(el.value()["date"].get()); + ss >> std::get_time(&date, "%Y-%m-%dT%H-%M-%S"); + codes.push_back(std::pair(el.value()["cache_code"].get(), date)); } } off += j.size(); @@ -169,7 +183,6 @@ Caches Okapi::get_user_caches(std::string uuid, int count) { } while (j.size() > 0 && count > 0); } - // Debug(3) << codes; cc = get_caches(codes); Debug(2) << "Caches read from OC: " << cc.size() << '\n'; return cc; diff --git a/okapi.h b/okapi.h index 7dc79fc..cb1bcd9 100644 --- a/okapi.h +++ b/okapi.h @@ -3,6 +3,8 @@ #include "api.h" #include +#include +#include class Okapi : public Api { private: @@ -19,7 +21,7 @@ public: Okapi(std::string server_url, std::string consumer_key) : url(server_url), key(consumer_key) {} // Cache get_cache(std::string code); - Caches get_caches(std::vector codes); + Caches get_caches(std::vector> codes); Caches get_user_caches(std::string uuid, int count = 0); std::string get_changelog_json(int revision);