pixwhile/pixivclient.cpp

45 lines
1.4 KiB
C++

#include "numberhelper.h"
#include "pixivclient.h"
PixivClient::PixivClient() {
this->_www_pixiv_net_client.set_keep_alive(true);
this->_www_pixiv_net_client.set_default_headers({
{"User-Agent", "Mozilla/5.0 (Android 12; Mobile; rv:97.0) Gecko/97.0 Firefox/97.0"}
});
}
User PixivClient::get_user(uint64_t user_id) {
httplib::Result res = this->_www_pixiv_net_client.Get("/touch/ajax/user/details", {
{"lang", "en"}, {"id", std::to_string(user_id)}
}, httplib::Headers());
return this->_handle_result(std::move(res)).at("user_details").get<User>();
}
std::vector<uint64_t> PixivClient::get_illusts(uint64_t user_id) {
httplib::Result res = this->_www_pixiv_net_client.Get("/touch/ajax/illust/user_illusts", {
{"lang", "en"}, {"user_id", std::to_string(user_id)}
}, httplib::Headers());
nlohmann::json user_illust_ids = this->_handle_result(std::move(res)).at("user_illust_ids");
std::vector<uint64_t> ret;
ret.reserve(user_illust_ids.size());
for (auto &[_, i] : user_illust_ids.items()) {
ret.push_back(to_ull(std::move(i)));
}
return ret;
}
nlohmann::json PixivClient::_handle_result(httplib::Result res) {
if (!res) {
throw HTTPLibException(res.error());
}
nlohmann::json j = nlohmann::json::parse(std::move(res->body));
if (j.at("error")) {
throw PixivException(res->status, j.at("message").get<std::string>());
}
return j.at("body");
}