Skip to content

Towards using only next endpoint for the major data. (Partial solution to #5003) #5237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

mk-pmb
Copy link

@mk-pmb mk-pmb commented Apr 11, 2025

From my discussion with @unixfox , these are the changes that I was able to salvage from #5003 that either were not critizised in code review or where I was able to apply the suggestions.

@absidue
Copy link
Contributor

absidue commented Apr 11, 2025

Looks like some of the commits in this branch are outdated as it is missing changes that are already in the master branch such as #4934.

@mk-pmb
Copy link
Author

mk-pmb commented Apr 11, 2025

That may well be. I don't understand enough crystal to be able to identify redundant code; I could only transplant changes that @unixfox made. If you tell me which (parts of) commits I shall omit, I can of course do that.

@mk-pmb
Copy link
Author

mk-pmb commented Apr 11, 2025

There's also this commit that looks important: mk-pmb@108ee36
… but should probably be solved by instead duplicating the unconditional part at the end into the tiny "else" branch and adding early return (then invert the condition to make it a tiny "then" branch), to avoid having to indent the huge original "then" branch.

(If you do decide to indent, please do that in a separate commit or git will make a reeeeeeally confusing diff. It will be bad enough already but at least that way a diff with proper ignore-whitespace options can assure you that no actual code was changed.)

Shouldn't really belong to this PR though.

Update: I learned enough crystal to do the early return transformation, but now I see that the common tail below has grown with companion stuff, so probably now the better choice is to factor out the "then" branch into its own function.

@mk-pmb mk-pmb force-pushed the use-only-next-for-extra-info-250411 branch from 3b761f4 to a9dfc70 Compare April 11, 2025 17:04
@mk-pmb
Copy link
Author

mk-pmb commented Apr 11, 2025

Ok yeah, some things seem to have been broken by git's rebase. I now discovered the UTC fallback, and it seems obvious even to me that the rebased commit does the opposite of what it claims. I removed that commit.

Comment on lines +72 to +80
return false if badges.nil?

badges.as_a.each do |badge|
icon_type = badge.dig("metadataBadgeRenderer", "icon", "iconType").as_s

return true if icon_type == "PRIVACY_UNLISTED"
end

return false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return false if badges.nil?
badges.as_a.each do |badge|
icon_type = badge.dig("metadataBadgeRenderer", "icon", "iconType").as_s
return true if icon_type == "PRIVACY_UNLISTED"
end
return false
return false if badges.nil?
badges.as_a.each do |badge|
icon_type = badge.dig("metadataBadgeRenderer", "icon", "iconType").as_s
return true if icon_type == "PRIVACY_UNLISTED"
end
return false

Comment on lines +230 to +239
published_txt = video_primary_renderer
.try &.dig?("dateText", "simpleText")

if published_txt.try &.as_s.includes?("ago") && !published_txt.nil?
published = decode_date(published_txt.as_s.lchop("Started streaming "))
elsif published_txt && published_txt.try &.as_s.matches?(/(\w{3} \d{1,2}, \d{4})$/)
published = Time.parse(published_txt.as_s.match!(/(\w{3} \d{1,2}, \d{4})$/)[0], "%b %-d, %Y", Time::Location::UTC)
else
published = Time.utc
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like this should be more efficient

    video_primary_renderer.try(&.dig?("dateText", "simpleText")).try do | published_txt |
      published_txt = published_txt.as_s

      published = if published_txt.includes?("ago")
        decode_date(published_txt.lchop("Started streaming "))
      elsif matched_time = published_txt.match(/(\w{3} \d{1,2}, \d{4})$/)
        Time.parse(matched_time[0], "%b %-d, %Y", Time::Location::UTC)
      end
    end

    published ||= Time.utc

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also try using the scheduledStartTime prior to the other attempts

https://github.com/iv-org/invidious/pull/5003/files#r1807376801

Copy link
Author

@mk-pmb mk-pmb Jun 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, thanks for your feedback! Unfortunately I know almost nothing about crystal so I'd rather not try and implement actual changes. In this PR I just rebased @unixfox's code in hopes to get it merged asap to avoid even more branch divergence and thus even more effort for rebasing. I hope it's not too late already.

If it's important to you that I update the PR, I can try and apply all the changes you requested. However, since those are your changes, wouldn't it be more clear to merge and then apply them, rather than having me apply them in unixfox's or your name? (Evem deciding that for each patch seems needlessly error-prone.)

Except of course if any of the patches introduce a critical bug, then obviously I should fix the offending code before it's merged. If so, please tell me, because I'm unable to classify the kind of changes you made.

For any code that's not actually "bad" but just "could be better" (e.g. "should be more efficient"), let's make improvements a separate PR. After all, this PR is titled "Towards … (Partial solution …)" because I know it's not perfect, it's just a useful step, or at least I hope it is. And since most contributors here have a tendency to only occasionally have time for this project, I think merging acceptable parts as early as possible is a good approach.

Comment on lines +255 to +258
if live_now.nil?
live_now = video_primary_renderer
.try &.dig?("viewCount", "videoViewCountRenderer", "isLive").try &.as_bool
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if live_now.nil?
live_now = video_primary_renderer
.try &.dig?("viewCount", "videoViewCountRenderer", "isLive").try &.as_bool
end
live_now ||= video_primary_renderer.try &.dig?("viewCount", "videoViewCountRenderer", "isLive").try &.as_bool

Comment on lines -451 to +489
"shortDescription" => JSON::Any.new(short_description.try &.as_s || nil),
"shortDescription" => JSON::Any.new(short_description.try &.as_s || ""),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is shortDescription not allowed to be nil?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea. @unixfox , do you still remember?

Comment on lines +72 to +85
return false if badges.nil?

badges.as_a.each do |badge|
icon_type = badge.dig("metadataBadgeRenderer", "icon", "iconType").as_s

return true if icon_type == "PRIVACY_UNLISTED"
end

return false
rescue ex
LOGGER.debug("Unable to parse owner badges. Got exception: #{ex.message}")
LOGGER.trace("Owner badges data: #{badges.to_json}")

return false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This entire function could pretty much be reduced down to a single line. I don't know if the error handling + logging is intentional here but it shouldn't even be needed if you just use a dig? instead.

Suggested change
return false if badges.nil?
badges.as_a.each do |badge|
icon_type = badge.dig("metadataBadgeRenderer", "icon", "iconType").as_s
return true if icon_type == "PRIVACY_UNLISTED"
end
return false
rescue ex
LOGGER.debug("Unable to parse owner badges. Got exception: #{ex.message}")
LOGGER.trace("Owner badges data: #{badges.to_json}")
return false
return badges.try(&.as_a.any?(&.dig?("metadataBadgeRenderer", "icon", "iconType").==("PRIVACY_UNLISTED"))) || false

Comment on lines +85 to +91
error_message = <<-END_HTML
<div class="error_message">
<h2>#{translate(locale, "error_processing_data_youtube")}</h2>
<p>#{translate(locale, message)}</p>
#{error_redirect_helper(env)}
</div>
END_HTML
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy and pasted from my review from the original PR:

Your changes here will show the Error while processing the data sent by YouTube message on any InfoException while also removing the next steps message on all InfoExceptions

Example:

info exception

And InfoExceptions are raised in places unrelated to YouTube as well such as when a user inserts the wrong password.

Related: #4651

"carousel_go_to": "Go to slide `x`",
"error_from_youtube_unplayable": "Video unplayable due to an error from YouTube:",
"error_processing_data_youtube": "Error while processing the data sent by YouTube",
"refresh_page": "Refresh the page"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"refresh_page": "Refresh the page"

Comment on lines +35 to +43
<div id="player-container" class="h-box">
<%= rendered "components/player" %>
</div>
<% else %>
<div id="player-error-container" class="h-box">
<h3>
<%= video.reason %>
</h3>
</div>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<div id="player-container" class="h-box">
<%= rendered "components/player" %>
</div>
<% else %>
<div id="player-error-container" class="h-box">
<h3>
<%= video.reason %>
</h3>
</div>
<div id="player-container" class="h-box">
<%= rendered "components/player" %>
</div>
<% else %>
<div id="player-error-container" class="h-box">
<h3>
<%= video.reason %>
</h3>
</div>

Comment on lines 198 to 202
video_details = player_response.dig?("videoDetails")
video_details ||= {} of String => JSON::Any
if !(microformat = player_response.dig?("microformat", "playerMicroformatRenderer"))
microformat = {} of String => JSON::Any
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants