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

@ -319,6 +319,15 @@ module Invidious::Routes::PreferencesRoute
response: error_template(415, "Invalid playlist file uploaded") response: error_template(415, "Invalid playlist file uploaded")
) )
end end
when "import_youtube_wh"
filename = part.filename || ""
success = Invidious::User::Import.from_youtube_wh(user, body, filename, type)
if !success
haltf(env, status_code: 415,
response: error_template(415, "Invalid watch history file uploaded")
)
end
when "import_freetube" when "import_freetube"
Invidious::User::Import.from_freetube(user, body) Invidious::User::Import.from_freetube(user, body)
when "import_newpipe_subscriptions" when "import_newpipe_subscriptions"

View File

@ -218,6 +218,27 @@ struct Invidious::User
end end
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 # Freetube
# ------------------- # -------------------

View File

@ -26,6 +26,11 @@
<input type="file" id="import_youtube_pl" name="import_youtube_pl"> <input type="file" id="import_youtube_pl" name="import_youtube_pl">
</div> </div>
<div class="pure-control-group">
<label for="import_youtube_wh"><%= translate(locale, "Import YouTube watch history (.json)") %></label>
<input type="file" id="import_youtube_wh" name="import_youtube_wh">
</div>
<div class="pure-control-group"> <div class="pure-control-group">
<label for="import_freetube"><%= translate(locale, "Import FreeTube subscriptions (.db)") %></label> <label for="import_freetube"><%= translate(locale, "Import FreeTube subscriptions (.db)") %></label>
<input type="file" id="import_freetube" name="import_freetube"> <input type="file" id="import_freetube" name="import_freetube">