From d79c1ff3e9015bc093200aa1d48c265dfc9c6f42 Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Sun, 4 Feb 2018 19:42:13 -0600 Subject: [PATCH] Fix video indexing --- src/helpers.cr | 29 ++++++++++++++++++++--------- src/invidious.cr | 4 ++++ 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/helpers.cr b/src/helpers.cr index 1afe7674..a5148bdf 100644 --- a/src/helpers.cr +++ b/src/helpers.cr @@ -100,27 +100,38 @@ def fetch_video(id, client) views = info["view_count"].to_i64 likes = html.xpath_node(%q(//button[@title="I like this"]/span)) - likes = likes ? likes.content.delete(",").to_i : 1 + likes = likes ? likes.content.delete(",").to_i : 0 dislikes = html.xpath_node(%q(//button[@title="I dislike this"]/span)) dislikes = dislikes ? dislikes.content.delete(",").to_i : 0 wilson_score = ci_lower_bound(likes, likes + dislikes) - published = html.xpath_node(%q(//strong[@class="watch-time-text"])) + published = html.xpath_node(%q(//strong[contains(@class,"watch-time-text")])) if published published = published.content - published = published.lchop("Published on ") - published = published.lchop("Streamed live on ") - published = published.lchop("Started streaming on ") + else + raise "Could not find date published" + end + + published = published.lchop("Published ") + published = published.lchop("Streamed live ") + published = published.lchop("Started streaming ") + published = published.lchop("on ") + published = published.lchop("Scheduled for ") if !published.includes?("ago") published = Time.parse(published, "%b %-d, %Y") else # Time matches format "20 hours ago", "40 minutes ago"... - published = Time.now.date - end + delta = published.split(" ")[0].to_i + case published + when .includes? "minute" + published = Time.now - delta.minutes + when .includes? "hour" + published = Time.now - delta.hours else - published = Time.epoch(0) + raise "Could not parse #{published}" + end end video = Video.new(id, info, html, Time.now, title, views, likes, dislikes, wilson_score, published) @@ -165,7 +176,7 @@ def search(query, client) end def decrypt_signature(a) - a = a.split(""); + a = a.split("") a.delete_at(0..2) a = a.reverse c = a[0] diff --git a/src/invidious.cr b/src/invidious.cr index 54aaa55f..e103903c 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -182,7 +182,11 @@ get "/watch" do |env| rating = video.info["avg_rating"].to_f64 engagement = ((video.dislikes.to_f + video.likes.to_f)/video.views * 100) + if video.likes > 0 || video.dislikes > 0 calculated_rating = (video.likes.to_f/(video.likes.to_f + video.dislikes.to_f) * 4 + 1) + else + calculated_rating = 0.0 + end templated "watch" end