Refactor export function

pull/3617/head
Brahim Hadriche 2 years ago
parent b2589c74be
commit 2606decd21

@ -35,33 +35,7 @@ module Invidious::Routes::API::V1::Authenticated
env.response.content_type = "application/json" env.response.content_type = "application/json"
user = env.get("user").as(User) user = env.get("user").as(User)
playlists = Invidious::Database::Playlists.select_like_iv(user.email) return Invidious::User::Export.to_invidious(user)
return JSON.build do |json|
json.object do
json.field "subscriptions", user.subscriptions
json.field "watch_history", user.watched
json.field "preferences", user.preferences
json.field "playlists" do
json.array do
playlists.each do |playlist|
json.object do
json.field "title", playlist.title
json.field "description", html_to_content(playlist.description_html)
json.field "privacy", playlist.privacy.to_s
json.field "videos" do
json.array do
Invidious::Database::PlaylistVideos.select_ids(playlist.id, playlist.index, limit: 500).each do |video_id|
json.string video_id
end
end
end
end
end
end
end
end
end
end end
def self.import_invidious(env) def self.import_invidious(env)

@ -106,31 +106,7 @@ module Invidious::Routes::Subscriptions
env.response.headers["content-disposition"] = "attachment" env.response.headers["content-disposition"] = "attachment"
playlists = Invidious::Database::Playlists.select_like_iv(user.email) playlists = Invidious::Database::Playlists.select_like_iv(user.email)
return JSON.build do |json| return Invidious::User::Export.to_invidious(user)
json.object do
json.field "subscriptions", user.subscriptions
json.field "watch_history", user.watched
json.field "preferences", user.preferences
json.field "playlists" do
json.array do
playlists.each do |playlist|
json.object do
json.field "title", playlist.title
json.field "description", html_to_content(playlist.description_html)
json.field "privacy", playlist.privacy.to_s
json.field "videos" do
json.array do
Invidious::Database::PlaylistVideos.select_ids(playlist.id, playlist.index, limit: 500).each do |video_id|
json.string video_id
end
end
end
end
end
end
end
end
end
else else
env.response.content_type = "application/xml" env.response.content_type = "application/xml"
env.response.headers["content-disposition"] = "attachment" env.response.headers["content-disposition"] = "attachment"

@ -0,0 +1,35 @@
struct Invidious::User
module Export
extend self
def to_invidious(user : User)
playlists = Invidious::Database::Playlists.select_like_iv(user.email)
return JSON.build do |json|
json.object do
json.field "subscriptions", user.subscriptions
json.field "watch_history", user.watched
json.field "preferences", user.preferences
json.field "playlists" do
json.array do
playlists.each do |playlist|
json.object do
json.field "title", playlist.title
json.field "description", html_to_content(playlist.description_html)
json.field "privacy", playlist.privacy.to_s
json.field "videos" do
json.array do
Invidious::Database::PlaylistVideos.select_ids(playlist.id, playlist.index, limit: 500).each do |video_id|
json.string video_id
end
end
end
end
end
end
end
end
end
end
end # module
end
Loading…
Cancel
Save