aboutsummaryrefslogtreecommitdiff
path: root/src/plugins
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2020-12-01 23:34:49 +0100
committerdec05eba <dec05eba@protonmail.com>2020-12-01 23:34:49 +0100
commitc13a28519a3409af8984c76f9297f100e6be818d (patch)
tree585384f16a7d384067cf796f7235c1e8b3d99510 /src/plugins
parent1acc8a27d975ebc714eca62ea7fc828fa92f2a9e (diff)
Youtube: fix description that contains bold text being cut off
Diffstat (limited to 'src/plugins')
-rw-r--r--src/plugins/Youtube.cpp66
1 files changed, 36 insertions, 30 deletions
diff --git a/src/plugins/Youtube.cpp b/src/plugins/Youtube.cpp
index e04c51f..ddaae21 100644
--- a/src/plugins/Youtube.cpp
+++ b/src/plugins/Youtube.cpp
@@ -6,30 +6,36 @@
namespace QuickMedia {
// This is a common setup of text in the youtube json
- static const char* yt_json_get_text(const Json::Value &json, const char *root_name) {
+ static std::optional<std::string> yt_json_get_text(const Json::Value &json, const char *root_name) {
if(!json.isObject())
- return nullptr;
+ return std::nullopt;
const Json::Value &text_json = json[root_name];
if(!text_json.isObject())
- return nullptr;
+ return std::nullopt;
const Json::Value &simple_text_json = text_json["simpleText"];
if(simple_text_json.isString()) {
return simple_text_json.asCString();
} else {
const Json::Value &runs_json = text_json["runs"];
- if(runs_json.isArray() && !runs_json.empty()) {
- const Json::Value &first_runs_json = runs_json[0];
- if(first_runs_json.isObject()) {
- const Json::Value &text_json = first_runs_json["text"];
- if(text_json.isString())
- return text_json.asCString();
- }
+ if(!runs_json.isArray() || runs_json.empty())
+ return std::nullopt;
+
+ std::string result;
+ for(const Json::Value &first_runs_json : runs_json) {
+ if(!first_runs_json.isObject())
+ continue;
+
+ const Json::Value &text_json = first_runs_json["text"];
+ if(text_json.isString())
+ result += text_json.asString();
}
+ if(!result.empty())
+ return result;
}
- return nullptr;
+ return std::nullopt;
}
static std::shared_ptr<BodyItem> parse_common_video_item(const Json::Value &video_item_json, std::unordered_set<std::string> &added_videos) {
@@ -41,14 +47,14 @@ namespace QuickMedia {
if(added_videos.find(video_id_str) != added_videos.end())
return nullptr;
- const char *title = yt_json_get_text(video_item_json, "title");
+ std::optional<std::string> title = yt_json_get_text(video_item_json, "title");
if(!title)
return nullptr;
- const char *date = yt_json_get_text(video_item_json, "publishedTimeText");
- const char *view_count_text = yt_json_get_text(video_item_json, "viewCountText");
- const char *owner_text = yt_json_get_text(video_item_json, "shortBylineText");
- const char *length = yt_json_get_text(video_item_json, "lengthText");
+ std::optional<std::string> date = yt_json_get_text(video_item_json, "publishedTimeText");
+ std::optional<std::string> view_count_text = yt_json_get_text(video_item_json, "viewCountText");
+ std::optional<std::string> owner_text = yt_json_get_text(video_item_json, "shortBylineText");
+ std::optional<std::string> length = yt_json_get_text(video_item_json, "lengthText");
if(!length) {
const Json::Value &thumbnail_overlays_json = video_item_json["thumbnailOverlays"];
if(thumbnail_overlays_json.isArray() && !thumbnail_overlays_json.empty()) {
@@ -58,24 +64,24 @@ namespace QuickMedia {
}
}
- auto body_item = BodyItem::create(title);
+ auto body_item = BodyItem::create(title.value());
std::string desc;
if(view_count_text)
- desc += view_count_text;
+ desc += view_count_text.value();
if(date) {
if(!desc.empty())
desc += " • ";
- desc += date;
+ desc += date.value();
}
if(length) {
if(!desc.empty())
desc += '\n';
- desc += length;
+ desc += length.value();
}
if(owner_text) {
if(!desc.empty())
desc += '\n';
- desc += owner_text;
+ desc += owner_text.value();
}
body_item->set_description(std::move(desc));
body_item->url = "https://www.youtube.com/watch?v=" + video_id_str;
@@ -145,26 +151,26 @@ namespace QuickMedia {
if(!channel_id_json.isString())
return nullptr;
- const char *title = yt_json_get_text(channel_renderer_json, "title");
+ std::optional<std::string> title = yt_json_get_text(channel_renderer_json, "title");
if(!title)
return nullptr;
- const char *description = yt_json_get_text(channel_renderer_json, "descriptionSnippet");
- const char *video_count = yt_json_get_text(channel_renderer_json, "videoCountText");
- const char *subscribers = yt_json_get_text(channel_renderer_json, "subscriberCountText");
+ std::optional<std::string> description = yt_json_get_text(channel_renderer_json, "descriptionSnippet");
+ std::optional<std::string> video_count = yt_json_get_text(channel_renderer_json, "videoCountText");
+ std::optional<std::string> subscribers = yt_json_get_text(channel_renderer_json, "subscriberCountText");
const Json::Value &thumbnail_json = channel_renderer_json["thumbnail"];
std::optional<Thumbnail> thumbnail = yt_json_get_largest_thumbnail(thumbnail_json);
- auto body_item = BodyItem::create(title);
+ auto body_item = BodyItem::create(title.value());
std::string desc;
if(subscribers)
- desc += subscribers;
+ desc += subscribers.value();
if(video_count) {
if(!desc.empty())
desc += " • ";
- desc += video_count;
- if(strcmp(video_count, "1") == 0)
+ desc += video_count.value();
+ if(strcmp(video_count.value().c_str(), "1") == 0)
desc += " video";
else
desc += " videos";
@@ -172,7 +178,7 @@ namespace QuickMedia {
if(description) {
if(!desc.empty())
desc += '\n';
- desc += description;
+ desc += description.value();
}
body_item->set_description(std::move(desc));
body_item->url = "https://www.youtube.com/channel/" + channel_id_json.asString();