add: importing watch history from YouTube

This commit is contained in:
Ulysses Zhan
2023-10-14 15:49:33 -07:00
parent 069e91d2a6
commit b9cbdce976
3 changed files with 35 additions and 0 deletions

View File

@@ -218,6 +218,27 @@ struct Invidious::User
end
end
def from_youtube_wh(user : User, body : String, filename : String, type : String) : Bool
extension = filename.split(".").last
if extension == "json" || type == "application/json"
data = JSON.parse(body)
watched = data.as_a.compact_map do |item|
next unless url = item["titleUrl"]?
next unless match = url.as_s.match(/\?v=(?<video_id>[a-zA-Z0-9_-]+)$/)
puts match["video_id"]
match["video_id"]
end
watched.reverse! # YouTube have newest first
user.watched += watched
user.watched.uniq!
Invidious::Database::Users.update_watch_history(user)
return true
else
return false
end
end
# -------------------
# Freetube
# -------------------