pixwhile/pixivclient.cpp

45 lines
1.6 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>();
}
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)});
2023-04-07 12:00:16 +00:00
}
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>();
2023-04-07 12:00:16 +00:00
}
2023-04-08 16:06:56 +00:00
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>();
}
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");
}