Function to grab only ratings from OKapi

sql-rework
Tomasz Goliński 2020-08-28 18:42:54 +02:00
rodzic cf48fc55c8
commit 0a86f18d7e
2 zmienionych plików z 50 dodań i 0 usunięć

Wyświetl plik

@ -97,6 +97,12 @@ void Okapi::get_caches_json(const std::string& codes) const {
curl_post(service, query);
}
void Okapi::get_caches_ratings_json(const std::string& codes) const {
std::string service = url + OKAPI_caches;
std::string query = "consumer_key=" + key + "&cache_codes=" + codes + "&fields=code|recommendations|rating|status";
curl_post(service, query);
}
// Cache Okapi::get_cache(std::string code) {
// std::string output = get_cache_json(code);
// json j = json::parse(output);
@ -252,6 +258,48 @@ void Okapi::update_caches(Caches& cc) const {
cc = get_caches(codes);
}
void Okapi::update_caches_ratings(Caches& cc) const {
std::map<std::string, Cache*> pcc;
std::string code;
uint k;
std::string codes_list;
codes_list.reserve(MAX_CACHES * 8); // maximum of MAX_CACHES codes, 7 chars per code plus a separator
auto it = cc.begin();
while (it != cc.end()) {
k = 0;
while (it != cc.end() && k < MAX_CACHES) {
codes_list += it->code;
codes_list += '|';
pcc[it->code] = &*it;
it++;
k++;
}
codes_list.pop_back(); // remove trailing '|'
get_caches_json(codes_list);
json j = json::parse(curl_output);
codes_list.clear();
for (auto& el : j.items()) {
code = el.value()["code"];
pcc[code]->fav = el.value()["recommendations"];
if (!el.value()["rating"].is_null()) pcc[code]->rating = el.value()["rating"];
if (el.value()["status"] == "Available")
pcc[code]->status = ok;
else if (el.value()["status"] == "Temporarily unavailable")
pcc[code]->status = disabled;
else if (el.value()["status"] == "Archived")
pcc[code]->status = archived;
else
pcc[code]->status = unknown;
}
}
}
std::string Okapi::get_uuid(const std::string& username) const {
std::string service = url + OKAPI_username;
char* user_esc = curl_easy_escape(curl, username.c_str(), username.size());

Wyświetl plik

@ -23,6 +23,7 @@ private:
void get_user_caches_json(const std::string& uuid, int count = 0, int offset = 0) const;
// std::string get_cache_json(std::string code) const;
void get_caches_json(const std::string& codes) const;
void get_caches_ratings_json(const std::string& codes) const;
const static int MAX_LOGS = 1000;
const static int MAX_CACHES = 500;
@ -37,6 +38,7 @@ public:
std::string get_changelog_json(int revision) const;
void update_caches(Caches& cc) const;
void update_caches_ratings(Caches& cc) const;
std::string get_uuid(const std::string& username) const;
std::string get_profile_url(const std::string& uuid) const;
};