pixwhile/pixivclient.cpp

46 lines
1.7 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"},
{"Cookie", "webp_available=1"}
});
}
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>();
}
Illusts PixivClient::get_illusts(uint64_t user_id, size_t page) {
httplib::Params params = {{"lang", "en"}, {"id", std::to_string(user_id)}};
if (page != 0) {
params.insert({"p", std::to_string(page + 1)});
}
httplib::Result res = this->_www_pixiv_net_client.Get("/touch/ajax/user/illusts", std::move(params), httplib::Headers());
return this->_handle_result(std::move(res)).get<Illusts>();
}
Illust PixivClient::get_illust(uint64_t illust_id) {
httplib::Result res = this->_www_pixiv_net_client.Get("/touch/ajax/illust/details", {
{"lang", "en"}, {"illust_id", std::to_string(illust_id)}
}, httplib::Headers());
return this->_handle_result(std::move(res)).get<Illust>();
}
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");
}