Use a find instead of an each loop

This commit is contained in:
ChunkyProgrammer 2024-07-10 09:26:31 -04:00
parent 6b0e4e6817
commit bba1769f4b
1 changed files with 29 additions and 31 deletions

View File

@ -291,40 +291,38 @@ struct Invidious::User
def from_newpipe(user : User, body : String) : Bool def from_newpipe(user : User, body : String) : Bool
Compress::Zip::File.open(IO::Memory.new(body), true) do |file| Compress::Zip::File.open(IO::Memory.new(body), true) do |file|
file.entries.each do |entry| entry = file.entries.find { |file_entry| file_entry.filename == "newpipe.db" }
entry.open do |file_io| return false if entry.nil?
next if entry.filename != "newpipe.db" entry.open do |file_io|
# Ensure max size of 4MB
io_sized = IO::Sized.new(file_io, 0x400000)
# Ensure max size of 4MB begin
io_sized = IO::Sized.new(file_io, 0x400000) temp = File.tempfile(".db") do |tempfile|
begin
begin File.write(tempfile.path, io_sized.gets_to_end)
temp = File.tempfile(".db") do |tempfile| rescue
begin return false
File.write(tempfile.path, io_sized.gets_to_end) end
rescue
return false DB.open("sqlite3://" + tempfile.path) do |db|
end user.watched += db.query_all("SELECT url FROM streams", as: String)
.map(&.lchop("https://www.youtube.com/watch?v="))
DB.open("sqlite3://" + tempfile.path) do |db|
user.watched += db.query_all("SELECT url FROM streams", as: String) user.watched.uniq!
.map(&.lchop("https://www.youtube.com/watch?v=")) Invidious::Database::Users.update_watch_history(user)
user.watched.uniq! user.subscriptions += db.query_all("SELECT url FROM subscriptions", as: String)
Invidious::Database::Users.update_watch_history(user) .map(&.lchop("https://www.youtube.com/channel/"))
user.subscriptions += db.query_all("SELECT url FROM subscriptions", as: String) user.subscriptions.uniq!
.map(&.lchop("https://www.youtube.com/channel/")) user.subscriptions = get_batch_channels(user.subscriptions)
user.subscriptions.uniq! Invidious::Database::Users.update_subscriptions(user)
user.subscriptions = get_batch_channels(user.subscriptions)
Invidious::Database::Users.update_subscriptions(user)
end
end end
ensure
temp.delete if !temp.nil?
end end
ensure
temp.delete if !temp.nil?
end end
end end
end end