Split Images and User to pixivmodels.{h,cpp}

This commit is contained in:
blankie 2023-04-08 14:41:34 +07:00
parent 2750953d5c
commit 00df5327f0
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
7 changed files with 143 additions and 133 deletions

View File

@ -23,7 +23,7 @@ list(APPEND FLAGS -Werror -Wall -Wextra -Wshadow -Wpedantic -Wno-gnu-anonymous-s
add_link_options(${FLAGS}) add_link_options(${FLAGS})
add_executable(${PROJECT_NAME} main.cpp misc.cpp config.cpp servehelper.cpp numberhelper.cpp pixivclient.cpp add_executable(${PROJECT_NAME} main.cpp misc.cpp config.cpp servehelper.cpp numberhelper.cpp pixivclient.cpp pixivmodels.cpp
blankie/serializer.cpp blankie/escape.cpp blankie/murl.cpp blankie/serializer.cpp blankie/escape.cpp blankie/murl.cpp
routes/home.cpp routes/css.cpp routes/users/common.cpp routes/users/users.cpp routes/users/illustrations.cpp) routes/home.cpp routes/css.cpp routes/users/common.cpp routes/users/users.cpp routes/users/illustrations.cpp)
set_target_properties(${PROJECT_NAME} set_target_properties(${PROJECT_NAME}

View File

@ -1,33 +1,6 @@
#include <regex>
#include "numberhelper.h" #include "numberhelper.h"
#include "blankie/murl.h"
#include "pixivclient.h" #include "pixivclient.h"
static inline std::optional<std::string> get_1920x960_cover_image(blankie::murl::Url url);
static inline std::optional<std::string> get_original_cover_image(blankie::murl::Url url);
static inline std::optional<std::string> get_original_profile_picture(blankie::murl::Url url);
const std::string& Images::original_or_thumbnail() const {
if (this->original) {
return *this->original;
}
if (!this->thumbnails.empty()) {
return this->thumbnails.back();
}
throw std::runtime_error("Images does not contain any images");
}
const std::string& Images::thumbnail_or_original() const {
if (!this->thumbnails.empty()) {
return this->thumbnails.back();
}
if (this->original) {
return *this->original;
}
throw std::runtime_error("Images does not contain any images");
}
PixivClient::PixivClient() { PixivClient::PixivClient() {
this->_www_pixiv_net_client.set_keep_alive(true); this->_www_pixiv_net_client.set_keep_alive(true);
this->_www_pixiv_net_client.set_default_headers({ this->_www_pixiv_net_client.set_default_headers({
@ -69,84 +42,3 @@ nlohmann::json PixivClient::_handle_result(httplib::Result res) {
} }
return j.at("body"); return j.at("body");
} }
void from_json(const nlohmann::json& j, User& user) {
j.at("user_account").get_to(user.username);
j.at("user_name").get_to(user.display_name);
user.user_id = to_ull(j.at("user_id").get_ref<const nlohmann::json::string_t&>());
if (j.contains("cover_image") && j["cover_image"].is_object()) {
nlohmann::json cover_image = j["cover_image"];
std::string c_720x360 = cover_image.at("profile_cover_image").at("720x360").get<std::string>();
std::optional<std::string> original = get_original_cover_image(c_720x360);
std::optional<std::string> c_1920x960 = get_1920x960_cover_image(c_720x360);
user.cover_images = {std::move(original), {std::move(c_720x360)}};
if (c_1920x960) {
user.cover_images->thumbnails.push_back(std::move(*c_1920x960));
}
}
nlohmann::json profile_img = j.at("profile_img");
if (profile_img.contains("main_s")) {
user.profile_pictures.thumbnails.push_back(profile_img["main_s"].get<std::string>());
}
user.profile_pictures.thumbnails.push_back(profile_img.at("main").get<std::string>());
user.profile_pictures.original = get_original_profile_picture(user.profile_pictures.thumbnails.back());
std::string user_webpage = j.at("user_webpage").get<std::string>();
if (!user_webpage.empty()) {
user.links.push_back({"Webpage", std::move(user_webpage)});
}
auto add_social_as_needed = [&](const char* key, const char* public_name) {
nlohmann::json social = j.at("social");
if (!social.is_object()) {
return;
}
if (!social.contains(key)) {
return;
}
std::string url = social[key].at("url").get<std::string>();
user.links.push_back({public_name, std::move(url)});
};
add_social_as_needed("twitter", "Twitter");
add_social_as_needed("instagram", "Instagram");
add_social_as_needed("tumblr", "Tumblr");
add_social_as_needed("facebook", "Facebook");
add_social_as_needed("circlems", "Circle.ms");
add_social_as_needed("pawoo", "Pawoo");
}
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;
if (!std::regex_match(url.path, sm, resolution_path_regex)) {
return std::nullopt;
}
if (sm[1] == "1920x960") {
return std::nullopt;
}
url.path = "/c/1920x960" + sm.str(2);
return url.to_string();
}
static std::regex thumbnail_path_regex("/c/[^/]+/(.+)_master\\d+(\\.\\w{3,4})?");
static inline std::optional<std::string> get_original_cover_image(blankie::murl::Url url) {
std::smatch sm;
if (!std::regex_match(url.path, sm, thumbnail_path_regex)) {
return std::nullopt;
}
url.path = sm.str(1) + sm.str(2);
return url.to_string();
}
static std::regex profile_picture_thumbnail_path_regex("(/.+)_\\d{2,}(\\.\\w{3,4})");
static inline std::optional<std::string> get_original_profile_picture(blankie::murl::Url url) {
std::smatch sm;
if (!std::regex_match(url.path, sm, profile_picture_thumbnail_path_regex)) {
return std::nullopt;
}
url.path = sm.str(1) + sm.str(2);
return url.to_string();
}

View File

@ -1,30 +1,11 @@
#pragma once #pragma once
#include <vector> #include <vector>
#include <utility>
#include <optional>
#include "pixivmodels.h"
#include <httplib/httplib.h> #include <httplib/httplib.h>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
struct Images {
std::optional<std::string> original;
std::vector<std::string> thumbnails;
const std::string& original_or_thumbnail() const;
const std::string& thumbnail_or_original() const;
};
struct User {
std::string username;
std::string display_name;
uint64_t user_id;
std::optional<Images> cover_images;
Images profile_pictures;
std::vector<std::pair<std::string, std::string>> links;
};
class PixivClient { class PixivClient {
public: public:
PixivClient(); PixivClient();
@ -64,5 +45,3 @@ public:
private: private:
std::string _message; std::string _message;
}; };
void from_json(const nlohmann::json& j, User& user);

111
pixivmodels.cpp Normal file
View File

@ -0,0 +1,111 @@
#include <regex>
#include "blankie/murl.h"
#include "pixivmodels.h"
#include "numberhelper.h"
static inline std::optional<std::string> get_1920x960_cover_image(blankie::murl::Url url);
static inline std::optional<std::string> get_original_cover_image(blankie::murl::Url url);
static inline std::optional<std::string> get_original_profile_picture(blankie::murl::Url url);
const std::string& Images::original_or_thumbnail() const {
if (this->original) {
return *this->original;
}
if (!this->thumbnails.empty()) {
return this->thumbnails.back();
}
throw std::runtime_error("Images does not contain any images");
}
const std::string& Images::thumbnail_or_original() const {
if (!this->thumbnails.empty()) {
return this->thumbnails.back();
}
if (this->original) {
return *this->original;
}
throw std::runtime_error("Images does not contain any images");
}
void from_json(const nlohmann::json& j, User& user) {
j.at("user_account").get_to(user.username);
j.at("user_name").get_to(user.display_name);
user.user_id = to_ull(j.at("user_id").get_ref<const nlohmann::json::string_t&>());
if (j.contains("cover_image") && j["cover_image"].is_object()) {
nlohmann::json cover_image = j["cover_image"];
std::string c_720x360 = cover_image.at("profile_cover_image").at("720x360").get<std::string>();
std::optional<std::string> original = get_original_cover_image(c_720x360);
std::optional<std::string> c_1920x960 = get_1920x960_cover_image(c_720x360);
user.cover_images = {std::move(original), {std::move(c_720x360)}};
if (c_1920x960) {
user.cover_images->thumbnails.push_back(std::move(*c_1920x960));
}
}
nlohmann::json profile_img = j.at("profile_img");
if (profile_img.contains("main_s")) {
user.profile_pictures.thumbnails.push_back(profile_img["main_s"].get<std::string>());
}
user.profile_pictures.thumbnails.push_back(profile_img.at("main").get<std::string>());
user.profile_pictures.original = get_original_profile_picture(user.profile_pictures.thumbnails.back());
std::string user_webpage = j.at("user_webpage").get<std::string>();
if (!user_webpage.empty()) {
user.links.push_back({"Webpage", std::move(user_webpage)});
}
auto add_social_as_needed = [&](const char* key, const char* public_name) {
nlohmann::json social = j.at("social");
if (!social.is_object()) {
return;
}
if (!social.contains(key)) {
return;
}
std::string url = social[key].at("url").get<std::string>();
user.links.push_back({public_name, std::move(url)});
};
add_social_as_needed("twitter", "Twitter");
add_social_as_needed("instagram", "Instagram");
add_social_as_needed("tumblr", "Tumblr");
add_social_as_needed("facebook", "Facebook");
add_social_as_needed("circlems", "Circle.ms");
add_social_as_needed("pawoo", "Pawoo");
}
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;
if (!std::regex_match(url.path, sm, resolution_path_regex)) {
return std::nullopt;
}
if (sm[1] == "1920x960") {
return std::nullopt;
}
url.path = "/c/1920x960" + sm.str(2);
return url.to_string();
}
static std::regex thumbnail_path_regex("/c/[^/]+/(.+)_master\\d+(\\.\\w{3,4})?");
static inline std::optional<std::string> get_original_cover_image(blankie::murl::Url url) {
std::smatch sm;
if (!std::regex_match(url.path, sm, thumbnail_path_regex)) {
return std::nullopt;
}
url.path = sm.str(1) + sm.str(2);
return url.to_string();
}
static std::regex profile_picture_thumbnail_path_regex("(/.+)_\\d{2,}(\\.\\w{3,4})");
static inline std::optional<std::string> get_original_profile_picture(blankie::murl::Url url) {
std::smatch sm;
if (!std::regex_match(url.path, sm, profile_picture_thumbnail_path_regex)) {
return std::nullopt;
}
url.path = sm.str(1) + sm.str(2);
return url.to_string();
}

28
pixivmodels.h Normal file
View File

@ -0,0 +1,28 @@
#pragma once
#include <string>
#include <vector>
#include <optional>
#include <utility>
#include <nlohmann/json.hpp>
struct Images {
std::optional<std::string> original;
std::vector<std::string> thumbnails;
const std::string& original_or_thumbnail() const;
const std::string& thumbnail_or_original() const;
};
struct User {
std::string username;
std::string display_name;
uint64_t user_id;
std::optional<Images> cover_images;
Images profile_pictures;
std::vector<std::pair<std::string, std::string>> links;
};
void from_json(const nlohmann::json& j, User& user);

View File

@ -1,7 +1,7 @@
#include "common.h" #include "common.h"
#include "../../config.h" #include "../../config.h"
#include "../../servehelper.h" #include "../../servehelper.h"
#include "../../pixivclient.h" #include "../../pixivmodels.h"
static inline Element generate_user_links(const User& user); static inline Element generate_user_links(const User& user);

View File

@ -1,7 +1,7 @@
#pragma once #pragma once
#include "../../blankie/serializer.h" #include "../../blankie/serializer.h"
struct User; // forward declaration from ../../pixivclient.h struct User; // forward declaration from ../../pixivmodels.h
struct Config; // forward declaration from ../../config.h struct Config; // forward declaration from ../../config.h
using Element = blankie::html::Element; using Element = blankie::html::Element;