User: Add support for importing Youtube watch history (#4171)

このコミットが含まれているのは:
Samantaz Fox 2023-10-21 18:33:05 +02:00
コミット 2414e7db41
この署名に対応する既知のキーがデータベースに存在しません
GPGキーID: F42821059186176E
7個のファイルの変更38行の追加1行の削除

ファイルの表示

@ -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

ファイルの表示

@ -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)",

ファイルの表示

@ -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": "删除",

ファイルの表示

@ -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": "儲存",

ファイルの表示

@ -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"

ファイルの表示

@ -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=(?<video_id>[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
# -------------------

ファイルの表示

@ -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">