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...
sql-rework
Tomasz Goliński 2019-11-05 20:39:33 +01:00
rodzic c400624a95
commit 6ccd8fcbcb
4 zmienionych plików z 39 dodań i 15 usunięć

Wyświetl plik

@ -1,6 +1,8 @@
#include "cache.h"
#include <cmath>
#include <ctime>
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;
}

Wyświetl plik

@ -3,6 +3,7 @@
#include <string>
#include <set>
#include <iostream>
#include <ctime>
// 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 {

Wyświetl plik

@ -4,6 +4,8 @@
#include <iostream>
#include <vector>
#include <iomanip>
#include <sstream>
#include <curl/curl.h>
#include <nlohmann/json.hpp>
@ -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<std::string> codes) {
Caches cc;
Caches Okapi::get_caches(std::vector<std::pair<std::string, std::tm>> 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<std::string> 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<std::string> codes) {
Caches Okapi::get_user_caches(std::string uuid, int count) {
Caches cc;
std::vector<std::string> codes;
std::vector<std::pair<std::string, std::tm>> 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<std::string>());
ss >> std::get_time(&date, "%Y-%m-%dT%H-%M-%S");
codes.push_back(std::pair(el.value()["cache_code"].get<std::string>(), 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<std::string>());
ss >> std::get_time(&date, "%Y-%m-%dT%H-%M-%S");
codes.push_back(std::pair(el.value()["cache_code"].get<std::string>(), 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;

Wyświetl plik

@ -3,6 +3,8 @@
#include "api.h"
#include <string>
#include <vector>
#include <utility>
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<std::string> codes);
Caches get_caches(std::vector<std::pair<std::string, std::tm>> codes);
Caches get_user_caches(std::string uuid, int count = 0);
std::string get_changelog_json(int revision);