diff --git a/main.cpp b/main.cpp index 2aca955..b67e80f 100644 --- a/main.cpp +++ b/main.cpp @@ -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"); }); diff --git a/pixivclient.cpp b/pixivclient.cpp index 50c2097..4216767 100644 --- a/pixivclient.cpp +++ b/pixivclient.cpp @@ -15,20 +15,13 @@ User PixivClient::get_user(uint64_t user_id) { return this->_handle_result(std::move(res)).at("user_details").get(); } -std::vector 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 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(); } Illust PixivClient::get_illust(uint64_t illust_id) { diff --git a/pixivclient.h b/pixivclient.h index ec95b3f..23dd385 100644 --- a/pixivclient.h +++ b/pixivclient.h @@ -11,7 +11,7 @@ public: PixivClient(); User get_user(uint64_t user_id); - std::vector get_illusts(uint64_t user_id); + Illusts get_illusts(uint64_t user_id, size_t page); Illust get_illust(uint64_t illust_id); private: diff --git a/pixivmodels.cpp b/pixivmodels.cpp index dbb1e55..652c989 100644 --- a/pixivmodels.cpp +++ b/pixivmodels.cpp @@ -102,11 +102,11 @@ void from_json(const nlohmann::json& j, Illust& illust) { illust.ai_generated = illust_details.at("ai_type").get() != 0; illust_details.at("upload_timestamp").get_to(illust.upload_time); - std::string comment_html = illust_details.at("comment_html").get(); - 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(); + 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 get_1920x960_cover_image(blankie::murl::Url url) { std::smatch sm; diff --git a/pixivmodels.h b/pixivmodels.h index b1a6ef0..defa9f1 100644 --- a/pixivmodels.h +++ b/pixivmodels.h @@ -47,6 +47,13 @@ struct Illust { std::vector images; }; +struct Illusts { + std::vector 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); diff --git a/routes/users/illustrations.cpp b/routes/users/illustrations.cpp index 7d4ace1..5493299 100644 --- a/routes/users/illustrations.cpp +++ b/routes/users/illustrations.cpp @@ -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& 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 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 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& 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 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 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}; -}