Use another API for getting user illusts

This commit is contained in:
blankie 2023-04-08 23:27:45 +07:00
parent 6fc4bcbc16
commit 8a11873dfe
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
6 changed files with 40 additions and 61 deletions

View File

@ -5,7 +5,6 @@
#include "config.h"
#include "pixivclient.h"
#include "servehelper.h"
#include "numberhelper.h"
#include "routes/routes.h"
int main(int argc, char** argv) {
@ -56,11 +55,6 @@ int main(int argc, char** argv) {
});
#ifndef NDEBUG
// TODO remove
server.Get("/debug/illust", [&](const httplib::Request& req, httplib::Response& res) {
Illust illust = pixiv_client.get_illust(to_ull(req.get_param_value("id")));
res.set_content(illust.title, "text/plain; charset=utf-8");
});
server.Get("/debug/exception/known", [](const httplib::Request& req, httplib::Response& res) {
throw std::runtime_error("awoo");
});

View File

@ -15,20 +15,13 @@ User PixivClient::get_user(uint64_t user_id) {
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)));
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)});
}
return ret;
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) {

View File

@ -11,7 +11,7 @@ public:
PixivClient();
User get_user(uint64_t user_id);
std::vector<uint64_t> get_illusts(uint64_t user_id);
Illusts get_illusts(uint64_t user_id, size_t page);
Illust get_illust(uint64_t illust_id);
private:

View File

@ -102,11 +102,11 @@ void from_json(const nlohmann::json& j, Illust& illust) {
illust.ai_generated = illust_details.at("ai_type").get<int>() != 0;
illust_details.at("upload_timestamp").get_to(illust.upload_time);
std::string comment_html = illust_details.at("comment_html").get<std::string>();
if (!comment_html.empty()) {
illust.comment_html = std::move(comment_html);
}
if (illust_details.contains("display_tags")) {
if (full_data) {
std::string comment_html = illust_details.at("comment_html").get<std::string>();
if (!comment_html.empty()) {
illust.comment_html = std::move(comment_html);
}
illust_details["display_tags"].get_to(illust.tags);
}
@ -121,6 +121,12 @@ void from_json(const nlohmann::json& j, Illust& illust) {
}
}
void from_json(const nlohmann::json& j, Illusts& illusts) {
j.at("illusts").get_to(illusts.illusts);
j.at("total").get_to(illusts.total_illusts);
j.at("lastPage").get_to(illusts.total_pages);
}
static std::regex resolution_path_regex("/c/(\\d+x\\d+)(.+)");
static inline std::optional<std::string> get_1920x960_cover_image(blankie::murl::Url url) {
std::smatch sm;

View File

@ -47,6 +47,13 @@ struct Illust {
std::vector<Images> images;
};
struct Illusts {
std::vector<Illust> illusts;
size_t total_illusts;
size_t total_pages;
};
void from_json(const nlohmann::json& j, User& user);
void from_json(const nlohmann::json& j, Tag& tag);
void from_json(const nlohmann::json& j, Illust& illust);
void from_json(const nlohmann::json& j, Illusts& illusts);

View File

@ -6,21 +6,18 @@
#include "../../pixivclient.h"
#include "common.h"
static Element generate_pager(size_t page, size_t items, size_t items_per_page);
static inline Element generate_content(const std::vector<uint64_t>& illust_ids, size_t page, size_t items_per_page);
static inline size_t page_count(size_t items, size_t items_per_page);
static inline std::pair<size_t, size_t> page_to_offsets(size_t page, size_t items, size_t items_per_page);
static Element generate_pager(const Illusts& illusts, size_t page);
static inline Element generate_content(const Illusts& illusts);
void user_illustrations_route(const httplib::Request& req, httplib::Response& res, const Config& config, PixivClient& pixiv_client) {
uint64_t user_id = to_ull(req.matches[1].str());
uint64_t page = req.has_param("p") ? to_ull(req.get_param_value("p")) - 1 : 0;
User user;
std::vector<uint64_t> illust_ids;
Illusts illusts;
try {
user = pixiv_client.get_user(user_id);
illust_ids = pixiv_client.get_illusts(user_id);
illusts = pixiv_client.get_illusts(user_id, page);
} catch (const PixivException& e) {
if (e.status == 404) {
res.status = 404;
@ -36,22 +33,19 @@ void user_illustrations_route(const httplib::Request& req, httplib::Response& re
return;
}
const constexpr size_t items_per_page = 18; // on pixiv touch
Element body("body", {
generate_user_header(std::move(user), config),
generate_pager(page, illust_ids.size(), items_per_page),
generate_pager(illusts, page),
Element("br"),
generate_content(std::move(illust_ids), page, items_per_page),
generate_pager(page, illust_ids.size(), items_per_page)
generate_content(illusts),
generate_pager(illusts, page)
});
(void)page_to_offsets;
serve(req, res, config, user.display_name + " illustrations", std::move(body));
}
static Element generate_pager(size_t page, size_t items, size_t items_per_page) {
static Element generate_pager(const Illusts& illusts, size_t page) {
using namespace std::string_literals;
size_t total_pages = page_count(items, items_per_page);
auto link = [](std::string href, const char* text, bool add_link) {
Element b("b");
if (add_link) {
@ -64,37 +58,22 @@ static Element generate_pager(size_t page, size_t items, size_t items_per_page)
return Element("div", {{"class", "center"}}, {
link("?p=1", "First", page != 0), " ",
link("?p="s + std::to_string(page), "Prev", page != 0), " ",
std::to_string(page + 1), "/", std::to_string(total_pages), " ",
link("?p="s + std::to_string(page + 2), "Next", page + 1 < total_pages), " ",
link("?p="s + std::to_string(total_pages), "Last", page + 1 < total_pages)
std::to_string(page + 1), "/", std::to_string(illusts.total_pages), " ",
link("?p="s + std::to_string(page + 2), "Next", page + 1 < illusts.total_pages), " ",
link("?p="s + std::to_string(illusts.total_pages), "Last", page + 1 < illusts.total_pages)
});
}
static inline Element generate_content(const std::vector<uint64_t>& illust_ids, size_t page, size_t items_per_page) {
static inline Element generate_content(const Illusts& illusts) {
// TODO be real
Element ul("ul");
std::pair<size_t, size_t> illust_ids_offsets = page_to_offsets(page, illust_ids.size(), items_per_page);
ul.nodes.reserve(items_per_page);
for (size_t i = illust_ids_offsets.first; i < illust_ids_offsets.second; i++) {
ul.nodes.reserve(illusts.illusts.size());
for (const Illust& i : illusts.illusts) {
ul.nodes.push_back(Element("li", {
std::to_string(illust_ids[i])
std::to_string(i.illust_id)
}));
}
return ul;
}
static inline size_t page_count(size_t items, size_t items_per_page) {
size_t ret = items / items_per_page;
if (items % items_per_page != 0) {
ret++;
}
return ret;
}
static inline std::pair<size_t, size_t> page_to_offsets(size_t page, size_t items, size_t items_per_page) {
size_t start_offset = page * items_per_page;
size_t end_offset = start_offset + items_per_page;
return {items > start_offset ? start_offset : items, items > end_offset ? end_offset : items};
}