Invidious is an alternative front-end to YouTube
add: importing watch history from YouTube
Ulysses Zhanb9cbdce97631
diff --git a/src/invidious/routes/preferences.cr b/src/invidious/routes/preferences.crindex abe0f34e..112535bd 100644--- a/src/invidious/routes/preferences.cr+++ b/src/invidious/routes/preferences.cr@@ -319,6 +319,15 @@ module Invidious::Routes::PreferencesRouteresponse: error_template(415, "Invalid playlist file uploaded"))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")+ )+ endwhen "import_freetube"Invidious::User::Import.from_freetube(user, body)when "import_newpipe_subscriptions"diff --git a/src/invidious/user/imports.cr b/src/invidious/user/imports.crindex 86d0ce6e..744b4431 100644--- a/src/invidious/user/imports.cr+++ b/src/invidious/user/imports.cr@@ -218,6 +218,27 @@ struct Invidious::Userendend+ 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# -------------------diff --git a/src/invidious/views/user/data_control.ecr b/src/invidious/views/user/data_control.ecrindex 27654b40..9ce42c99 100644--- a/src/invidious/views/user/data_control.ecr+++ b/src/invidious/views/user/data_control.ecr@@ -26,6 +26,11 @@<input type="file" id="import_youtube_pl" name="import_youtube_pl"></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"><label for="import_freetube"><%= translate(locale, "Import FreeTube subscriptions (.db)") %></label><input type="file" id="import_freetube" name="import_freetube">