diff --git a/README.md b/README.md index 8c3b460a..6a314c16 100644 --- a/README.md +++ b/README.md @@ -82,7 +82,7 @@ **Data import/export** - Import subscriptions from YouTube, NewPipe and Freetube -- Import watch history from NewPipe +- Import watch history from YouTube and NewPipe - Export subscriptions to NewPipe and Freetube - Import/Export Invidious user data diff --git a/locales/en-US.json b/locales/en-US.json index 06d095dc..a9f78165 100644 --- a/locales/en-US.json +++ b/locales/en-US.json @@ -40,6 +40,7 @@ "Import Invidious data": "Import Invidious JSON data", "Import YouTube subscriptions": "Import YouTube/OPML subscriptions", "Import YouTube playlist (.csv)": "Import YouTube playlist (.csv)", + "Import YouTube watch history (.json)": "Import YouTube watch history (.json)", "Import FreeTube subscriptions (.db)": "Import FreeTube subscriptions (.db)", "Import NewPipe subscriptions (.json)": "Import NewPipe subscriptions (.json)", "Import NewPipe data (.zip)": "Import NewPipe data (.zip)", diff --git a/locales/zh-CN.json b/locales/zh-CN.json index 5e5d0ebb..db86a9bf 100644 --- a/locales/zh-CN.json +++ b/locales/zh-CN.json @@ -461,6 +461,7 @@ "Standard YouTube license": "标准 YouTube 许可证", "Download is disabled": "已禁用下载", "Import YouTube playlist (.csv)": "导入 YouTube 播放列表(.csv)", + "Import YouTube watch history (.json)": "导入 YouTube 观看历史(.json)", "generic_button_cancel": "取消", "playlist_button_add_items": "添加视频", "generic_button_delete": "删除", diff --git a/locales/zh-TW.json b/locales/zh-TW.json index de659c92..565f1d88 100644 --- a/locales/zh-TW.json +++ b/locales/zh-TW.json @@ -461,6 +461,7 @@ "Standard YouTube license": "標準 YouTube 授權條款", "Download is disabled": "已停用下載", "Import YouTube playlist (.csv)": "匯入 YouTube 播放清單 (.csv)", + "Import YouTube watch history (.json)": "匯入 YouTube 觀看歷史 (.json)", "generic_button_cancel": "取消", "generic_button_edit": "編輯", "generic_button_save": "儲存", diff --git a/src/invidious/routes/preferences.cr b/src/invidious/routes/preferences.cr index abe0f34e..112535bd 100644 --- a/src/invidious/routes/preferences.cr +++ b/src/invidious/routes/preferences.cr @@ -319,6 +319,15 @@ module Invidious::Routes::PreferencesRoute response: 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") + ) + end when "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.cr index 0d21bc44..108f2ccc 100644 --- a/src/invidious/user/imports.cr +++ b/src/invidious/user/imports.cr @@ -218,6 +218,26 @@ 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=(?[a-zA-Z0-9_-]+)$/) + 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.ecr index 27654b40..9ce42c99 100644 --- a/src/invidious/views/user/data_control.ecr +++ b/src/invidious/views/user/data_control.ecr @@ -26,6 +26,11 @@ +
+ + +
+