aboutsummaryrefslogtreecommitdiff
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
parent3d2bb79144aa63d691da46da7a77d8be868368cd (diff)
Fix youtube dislike count if a video has more dislikes than likes
-rw-r--r--TODO3
-rw-r--r--plugins/Youtube.hpp2
-rw-r--r--src/plugins/Youtube.cpp31
3 files changed, 22 insertions, 14 deletions
diff --git a/TODO b/TODO
index 4f654af..6ecd650 100644
--- a/TODO
+++ b/TODO
@@ -212,5 +212,4 @@ Improve pinephone video load performance by not restarting mpv on next video. In
Very large resolutions, such as 7680x2160 (id 272) for video https://www.youtube.com/watch?v=GxaH40zpvYc are not supported by the youtube android video loader. Find a way to fix that.
Use std::move(string) for all places where text.set_string is called.
Use reference for text.get_string() in all places.
-Cleanup font sizes that are not visible (or not used for a while). Also do the same for character ranges font texture.
-Youtube rating doesn't work if a youtube video has more dislikes than likes. The ratio simply becomes inverted then. Find a way to fix this using youtubes api. \ No newline at end of file
+Cleanup font sizes that are not visible (or not used for a while). Also do the same for character ranges font texture. \ No newline at end of file
diff --git a/plugins/Youtube.hpp b/plugins/Youtube.hpp
index a0c2e16..2a3b36e 100644
--- a/plugins/Youtube.hpp
+++ b/plugins/Youtube.hpp
@@ -174,7 +174,7 @@ namespace QuickMedia {
private:
std::string timestamp;
std::string comments_continuation_token;
- int64_t likes = -1;
+ int64_t num_likes = -1;
std::string livestream_url;
std::vector<YoutubeVideoFormat> video_formats;
std::vector<YoutubeAudioFormat> audio_formats;
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);