From 7b600cd4e1ad8900b7846cbfec07b7d48010c25f Mon Sep 17 00:00:00 2001
From: Daniel Diment <danieldiment.gj@gmail.com>
Date: Wed, 12 Apr 2023 23:22:14 +0200
Subject: [PATCH] Added support for import and export of youtube playlists

---
 src/invidious/user/exports.cr | 11 +++++++++++
 src/invidious/user/imports.cr | 10 ++++++++++
 2 files changed, 21 insertions(+)

diff --git a/src/invidious/user/exports.cr b/src/invidious/user/exports.cr
index b52503c9..1ff3eea1 100644
--- a/src/invidious/user/exports.cr
+++ b/src/invidious/user/exports.cr
@@ -4,6 +4,7 @@ struct Invidious::User
 
     def to_invidious(user : User)
       playlists = Invidious::Database::Playlists.select_like_iv(user.email)
+      youtube_playlists = Invidious::Database::Playlists.select_not_like_iv(user.email)
 
       return JSON.build do |json|
         json.object do
@@ -28,6 +29,16 @@ struct Invidious::User
               end
             end
           end
+          json.field "youtube_playlists" do
+            json.array do
+              youtube_playlists.each do |playlist|
+                json.object do
+                  json.field "title", playlist.title
+                  json.field "id", playlist.id
+                end
+              end
+            end
+          end
         end
       end
     end
diff --git a/src/invidious/user/imports.cr b/src/invidious/user/imports.cr
index 20ae0d47..00cf9a28 100644
--- a/src/invidious/user/imports.cr
+++ b/src/invidious/user/imports.cr
@@ -101,6 +101,16 @@ struct Invidious::User
           end
         end
       end
+
+      if youtube_playlists = data["youtube_playlists"]?.try &.as_a?
+        playlists.each do |item|
+          begin
+            playlist = get_playlist(item["id"])
+            subscribe_playlist(user, playlist)
+          rescue ex
+          end
+        end
+      end
     end
 
     # -------------------