aboutsummaryrefslogtreecommitdiff
path: root/src/plugins
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2021-12-02 02:38:57 +0100
committerdec05eba <dec05eba@protonmail.com>2021-12-02 02:38:57 +0100
commit899669eda8441fb6bbfde8da3cc78ee1fff5212e (patch)
treedda5d6faf6a6bd9a1dcde3e64cb79b7d7f82d1fa /src/plugins
parent3d2bb79144aa63d691da46da7a77d8be868368cd (diff)
Fix youtube dislike count if a video has more dislikes than likes
Diffstat (limited to 'src/plugins')
-rw-r--r--src/plugins/Youtube.cpp31
1 files changed, 20 insertions, 11 deletions
diff --git a/src/plugins/Youtube.cpp b/src/plugins/Youtube.cpp
index 46b2376..3d10a4c 100644
--- a/src/plugins/Youtube.cpp
+++ b/src/plugins/Youtube.cpp
@@ -2074,9 +2074,9 @@ namespace QuickMedia {
errno = 0;
char *endptr;
- const int64_t likes = strtoll(label.c_str(), &endptr, 10);
+ const int64_t num_likes = strtoll(label.c_str(), &endptr, 10);
if(endptr != label.c_str() && errno == 0)
- return likes;
+ return num_likes;
return -1;
}
@@ -2183,7 +2183,7 @@ namespace QuickMedia {
BodyItems YoutubeVideoPage::get_related_media(const std::string &url) {
comments_continuation_token.clear();
- likes = -1;
+ num_likes = -1;
BodyItems result_items;
std::string video_id;
@@ -2229,8 +2229,8 @@ namespace QuickMedia {
if(comments_continuation_token.empty())
comments_continuation_token = two_column_watch_next_results_get_comments_continuation_token(tcwnr_json);
- if(likes == -1)
- likes = two_column_watch_next_results_get_video_likes(tcwnr_json);
+ if(num_likes == -1)
+ num_likes = two_column_watch_next_results_get_video_likes(tcwnr_json);
const Json::Value &secondary_results_json = tcwnr_json["secondaryResults"];
if(!secondary_results_json.isObject())
@@ -2278,7 +2278,7 @@ namespace QuickMedia {
return value + 0.5;
}
- static std::shared_ptr<BodyItem> video_details_to_body_item(const YoutubeVideoDetails &video_details, int64_t likes) {
+ static std::shared_ptr<BodyItem> video_details_to_body_item(const YoutubeVideoDetails &video_details, int64_t num_likes) {
auto body_item = BodyItem::create(video_details.title);
std::string description;
@@ -2290,12 +2290,21 @@ namespace QuickMedia {
if(!description.empty())
description += " • ";
- if(likes == -1) {
+ fprintf(stderr, "like/dislike ratio: %s\n", video_details.rating.c_str());
+
+ if(num_likes == -1) {
description += "rated " + video_details.rating.substr(0, 4) + "/5";
} else {
- fprintf(stderr, "video rating: %s\n", video_details.rating.c_str());
- int64_t num_dislikes = round_double((double)likes * ((5.0 - atof(video_details.rating.c_str())) / 5.0));
- description += "👍 " + number_separate_thousand_commas(std::to_string(likes)) + " 👎 " + number_separate_thousand_commas(std::to_string(num_dislikes));
+ double average_rating = atof(video_details.rating.c_str());
+ // Minimum rating for a video is 1.0, even if a video only has dislikes
+ average_rating -= 1.0;
+ if(average_rating < 0.00000001)
+ average_rating = 0.00000001;
+
+ const int64_t num_ratings = num_likes * (4.0 / average_rating);
+ const int64_t num_dislikes = (num_ratings - num_likes);
+
+ description += "👍 " + number_separate_thousand_commas(std::to_string(num_likes)) + " 👎 " + number_separate_thousand_commas(std::to_string(num_dislikes));
}
}
@@ -2319,7 +2328,7 @@ namespace QuickMedia {
PluginResult YoutubeVideoPage::get_related_pages(const BodyItems &related_videos, const std::string &channel_url, std::vector<Tab> &result_tabs) {
auto description_page_body = create_body();
- description_page_body->append_item(video_details_to_body_item(video_details, likes));
+ description_page_body->append_item(video_details_to_body_item(video_details, num_likes));
auto related_page_body = create_body(false, true);
related_page_body->set_items(related_videos);