From b9cbdce97631ab9ecd97a6c8cb6bcf7f6f951b99 Mon Sep 17 00:00:00 2001 From: Ulysses Zhan Date: Sat, 14 Oct 2023 15:49:33 -0700 Subject: [PATCH 1/5] add: importing watch history from YouTube --- src/invidious/routes/preferences.cr | 9 +++++++++ src/invidious/user/imports.cr | 21 +++++++++++++++++++++ src/invidious/views/user/data_control.ecr | 5 +++++ 3 files changed, 35 insertions(+) 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 86d0ce6e..744b4431 100644 --- a/src/invidious/user/imports.cr +++ b/src/invidious/user/imports.cr @@ -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=(?[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.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 @@ +
+ + +
+
From a1a0e4c59f7db41eb50f004ef09e59a7fa087f5b Mon Sep 17 00:00:00 2001 From: Ulysses Zhan Date: Sat, 14 Oct 2023 15:56:04 -0700 Subject: [PATCH 2/5] update readme about importing watch history --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 88770383..905d4a12 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 From 50977fb5d9986b3759c5ce336eefac50ae7445c0 Mon Sep 17 00:00:00 2001 From: Ulysses Zhan Date: Sat, 14 Oct 2023 16:05:07 -0700 Subject: [PATCH 3/5] added translation importing watch history from youtube: zh-CN, zh-TW --- locales/zh-CN.json | 1 + locales/zh-TW.json | 1 + 2 files changed, 2 insertions(+) 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": "儲存", From 81a4f29c735f1b067c5e53d4e49d626bc6e787e3 Mon Sep 17 00:00:00 2001 From: Ulysses Zhan Date: Mon, 16 Oct 2023 21:46:41 -0700 Subject: [PATCH 4/5] add 'Import YouTube watch history (.json)' entry to en-US.json --- locales/en-US.json | 1 + 1 file changed, 1 insertion(+) 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)", From 3b219a4c7f932867f5a12608d8604436c722c1a0 Mon Sep 17 00:00:00 2001 From: Ulysses Zhan Date: Fri, 20 Oct 2023 13:45:16 -0700 Subject: [PATCH 5/5] remove a debug statement --- src/invidious/user/imports.cr | 1 - 1 file changed, 1 deletion(-) diff --git a/src/invidious/user/imports.cr b/src/invidious/user/imports.cr index 744b4431..26a22a27 100644 --- a/src/invidious/user/imports.cr +++ b/src/invidious/user/imports.cr @@ -226,7 +226,6 @@ struct Invidious::User 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_-]+)$/) - puts match["video_id"] match["video_id"] end watched.reverse! # YouTube have newest first