Move original_or_thumbnail and thumbnail_or_original to Images

This commit is contained in:
blankie 2023-04-07 23:58:38 +07:00
parent 1f877e4d4e
commit 2750953d5c
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
3 changed files with 27 additions and 23 deletions

View File

@ -8,6 +8,26 @@ static inline std::optional<std::string> get_1920x960_cover_image(blankie::murl:
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() {
this->_www_pixiv_net_client.set_keep_alive(true);
this->_www_pixiv_net_client.set_default_headers({

View File

@ -10,6 +10,9 @@
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 {

View File

@ -4,21 +4,19 @@
#include "../../pixivclient.h"
static inline Element generate_user_links(const User& user);
static std::string thumbnail_or_original(const Images& images);
static std::string original_or_thumbnail(const Images& images);
Element generate_user_header(const User& user, const Config& config) {
Element header("header");
if (user.cover_images) {
std::string cover_original = proxy_image_url(config, original_or_thumbnail(*user.cover_images));
std::string cover_thumbnail = proxy_image_url(config, thumbnail_or_original(*user.cover_images));
std::string cover_original = proxy_image_url(config, user.cover_images->original_or_thumbnail());
std::string cover_thumbnail = proxy_image_url(config, user.cover_images->thumbnail_or_original());
header.nodes.push_back(Element("a", {{"href", std::move(cover_original)}}, {
Element("img", {{"class", "cover"}, {"src", std::move(cover_thumbnail)}}, {})
}));
}
std::string profile_picture_original = proxy_image_url(config, original_or_thumbnail(user.profile_pictures));
std::string profile_picture_thumbnail = proxy_image_url(config, thumbnail_or_original(user.profile_pictures));
std::string profile_picture_original = proxy_image_url(config, user.profile_pictures.original_or_thumbnail());
std::string profile_picture_thumbnail = proxy_image_url(config, user.profile_pictures.thumbnail_or_original());
header.nodes.push_back(Element("div", {{"class", "usermetadata"}}, {
Element("a", {{"href", std::move(profile_picture_original)}}, {
Element("img", {{"class", "profilepicture"}, {"src", std::move(profile_picture_thumbnail)}}, {})
@ -41,20 +39,3 @@ static inline Element generate_user_links(const User& user) {
}
return p;
}
static std::string thumbnail_or_original(const Images& images) {
if (!images.thumbnails.empty()) {
return images.thumbnails.back();
}
return images.original.value_or("");
}
static std::string original_or_thumbnail(const Images& images) {
if (images.original) {
return *images.original;
}
if (!images.thumbnails.empty()) {
return images.thumbnails.back();
}
return "";
}