pixwhile/pixivclient.cpp

45 lines
1.4 KiB
C++
Raw Normal View History

#include "numberhelper.h"
2023-04-04 17:01:50 +00:00
#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>();
}
2023-04-07 12:00:16 +00:00
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;
}
2023-04-04 17:01:50 +00:00
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");
}