Use another API for getting user illusts
This commit is contained in:
parent
6fc4bcbc16
commit
8a11873dfe
6
main.cpp
6
main.cpp
|
@ -5,7 +5,6 @@
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "pixivclient.h"
|
#include "pixivclient.h"
|
||||||
#include "servehelper.h"
|
#include "servehelper.h"
|
||||||
#include "numberhelper.h"
|
|
||||||
#include "routes/routes.h"
|
#include "routes/routes.h"
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
|
@ -56,11 +55,6 @@ int main(int argc, char** argv) {
|
||||||
});
|
});
|
||||||
|
|
||||||
#ifndef NDEBUG
|
#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) {
|
server.Get("/debug/exception/known", [](const httplib::Request& req, httplib::Response& res) {
|
||||||
throw std::runtime_error("awoo");
|
throw std::runtime_error("awoo");
|
||||||
});
|
});
|
||||||
|
|
|
@ -15,20 +15,13 @@ User PixivClient::get_user(uint64_t user_id) {
|
||||||
return this->_handle_result(std::move(res)).at("user_details").get<User>();
|
return this->_handle_result(std::move(res)).at("user_details").get<User>();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<uint64_t> PixivClient::get_illusts(uint64_t user_id) {
|
Illusts PixivClient::get_illusts(uint64_t user_id, size_t page) {
|
||||||
httplib::Result res = this->_www_pixiv_net_client.Get("/touch/ajax/illust/user_illusts", {
|
httplib::Params params = {{"lang", "en"}, {"id", std::to_string(user_id)}};
|
||||||
{"lang", "en"}, {"user_id", std::to_string(user_id)}
|
if (page != 0) {
|
||||||
}, httplib::Headers());
|
params.insert({"p", std::to_string(page + 1)});
|
||||||
|
|
||||||
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)));
|
|
||||||
}
|
}
|
||||||
|
httplib::Result res = this->_www_pixiv_net_client.Get("/touch/ajax/user/illusts", std::move(params), httplib::Headers());
|
||||||
return ret;
|
return this->_handle_result(std::move(res)).get<Illusts>();
|
||||||
}
|
}
|
||||||
|
|
||||||
Illust PixivClient::get_illust(uint64_t illust_id) {
|
Illust PixivClient::get_illust(uint64_t illust_id) {
|
||||||
|
|
|
@ -11,7 +11,7 @@ public:
|
||||||
PixivClient();
|
PixivClient();
|
||||||
|
|
||||||
User get_user(uint64_t user_id);
|
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);
|
Illust get_illust(uint64_t illust_id);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -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.ai_generated = illust_details.at("ai_type").get<int>() != 0;
|
||||||
illust_details.at("upload_timestamp").get_to(illust.upload_time);
|
illust_details.at("upload_timestamp").get_to(illust.upload_time);
|
||||||
|
|
||||||
|
if (full_data) {
|
||||||
std::string comment_html = illust_details.at("comment_html").get<std::string>();
|
std::string comment_html = illust_details.at("comment_html").get<std::string>();
|
||||||
if (!comment_html.empty()) {
|
if (!comment_html.empty()) {
|
||||||
illust.comment_html = std::move(comment_html);
|
illust.comment_html = std::move(comment_html);
|
||||||
}
|
}
|
||||||
if (illust_details.contains("display_tags")) {
|
|
||||||
illust_details["display_tags"].get_to(illust.tags);
|
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 std::regex resolution_path_regex("/c/(\\d+x\\d+)(.+)");
|
||||||
static inline std::optional<std::string> get_1920x960_cover_image(blankie::murl::Url url) {
|
static inline std::optional<std::string> get_1920x960_cover_image(blankie::murl::Url url) {
|
||||||
std::smatch sm;
|
std::smatch sm;
|
||||||
|
|
|
@ -47,6 +47,13 @@ struct Illust {
|
||||||
std::vector<Images> images;
|
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, User& user);
|
||||||
void from_json(const nlohmann::json& j, Tag& tag);
|
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, Illust& illust);
|
||||||
|
void from_json(const nlohmann::json& j, Illusts& illusts);
|
||||||
|
|
|
@ -6,21 +6,18 @@
|
||||||
#include "../../pixivclient.h"
|
#include "../../pixivclient.h"
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
|
||||||
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);
|
||||||
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);
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
void user_illustrations_route(const httplib::Request& req, httplib::Response& res, const Config& config, PixivClient& pixiv_client) {
|
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 user_id = to_ull(req.matches[1].str());
|
||||||
uint64_t page = req.has_param("p") ? to_ull(req.get_param_value("p")) - 1 : 0;
|
uint64_t page = req.has_param("p") ? to_ull(req.get_param_value("p")) - 1 : 0;
|
||||||
User user;
|
User user;
|
||||||
std::vector<uint64_t> illust_ids;
|
Illusts illusts;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
user = pixiv_client.get_user(user_id);
|
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) {
|
} catch (const PixivException& e) {
|
||||||
if (e.status == 404) {
|
if (e.status == 404) {
|
||||||
res.status = 404;
|
res.status = 404;
|
||||||
|
@ -36,22 +33,19 @@ void user_illustrations_route(const httplib::Request& req, httplib::Response& re
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const constexpr size_t items_per_page = 18; // on pixiv touch
|
|
||||||
Element body("body", {
|
Element body("body", {
|
||||||
generate_user_header(std::move(user), config),
|
generate_user_header(std::move(user), config),
|
||||||
generate_pager(page, illust_ids.size(), items_per_page),
|
generate_pager(illusts, page),
|
||||||
Element("br"),
|
Element("br"),
|
||||||
generate_content(std::move(illust_ids), page, items_per_page),
|
generate_content(illusts),
|
||||||
generate_pager(page, illust_ids.size(), items_per_page)
|
generate_pager(illusts, page)
|
||||||
});
|
});
|
||||||
(void)page_to_offsets;
|
|
||||||
serve(req, res, config, user.display_name + " illustrations", std::move(body));
|
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;
|
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) {
|
auto link = [](std::string href, const char* text, bool add_link) {
|
||||||
Element b("b");
|
Element b("b");
|
||||||
if (add_link) {
|
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"}}, {
|
return Element("div", {{"class", "center"}}, {
|
||||||
link("?p=1", "First", page != 0), " ",
|
link("?p=1", "First", page != 0), " ",
|
||||||
link("?p="s + std::to_string(page), "Prev", page != 0), " ",
|
link("?p="s + std::to_string(page), "Prev", page != 0), " ",
|
||||||
std::to_string(page + 1), "/", std::to_string(total_pages), " ",
|
std::to_string(page + 1), "/", std::to_string(illusts.total_pages), " ",
|
||||||
link("?p="s + std::to_string(page + 2), "Next", page + 1 < total_pages), " ",
|
link("?p="s + std::to_string(page + 2), "Next", page + 1 < illusts.total_pages), " ",
|
||||||
link("?p="s + std::to_string(total_pages), "Last", page + 1 < 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
|
// TODO be real
|
||||||
Element ul("ul");
|
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);
|
ul.nodes.reserve(illusts.illusts.size());
|
||||||
for (size_t i = illust_ids_offsets.first; i < illust_ids_offsets.second; i++) {
|
for (const Illust& i : illusts.illusts) {
|
||||||
ul.nodes.push_back(Element("li", {
|
ul.nodes.push_back(Element("li", {
|
||||||
std::to_string(illust_ids[i])
|
std::to_string(i.illust_id)
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
return ul;
|
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};
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue