diff --git a/src/main/java/me/kavin/piped/ServerLauncher.java b/src/main/java/me/kavin/piped/ServerLauncher.java index 4dfd006..22e17a8 100644 --- a/src/main/java/me/kavin/piped/ServerLauncher.java +++ b/src/main/java/me/kavin/piped/ServerLauncher.java @@ -240,6 +240,16 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher { } catch (Exception e) { return getErrorResponse(e); } + })).map(POST, "/import", AsyncServlet.ofBlocking(executor, request -> { + try { + String[] subscriptions = Constants.mapper.readValue(request.loadBody().getResult().asArray(), + String[].class); + return getJsonResponse( + ResponseHelper.importResponse(request.getHeader(AUTHORIZATION), subscriptions), + "private"); + } catch (Exception e) { + return getErrorResponse(e); + } })); return new CustomServletDecorator(router); diff --git a/src/main/java/me/kavin/piped/utils/ResponseHelper.java b/src/main/java/me/kavin/piped/utils/ResponseHelper.java index 953a439..53076ef 100644 --- a/src/main/java/me/kavin/piped/utils/ResponseHelper.java +++ b/src/main/java/me/kavin/piped/utils/ResponseHelper.java @@ -696,6 +696,67 @@ public class ResponseHelper { } + public static final byte[] importResponse(String session, String[] channelIds) + throws IOException, NoSuchAlgorithmException, InvalidKeySpecException { + + Session s = DatabaseSessionFactory.createSession(); + + User user = DatabaseHelper.getUserFromSessionWithSubscribed(s, session); + + if (user != null) { + + for (String channelId : channelIds) { + + if (!user.getSubscribed().contains(channelId)) { + user.getSubscribed().add(channelId); + s.update(user); + } + + me.kavin.piped.utils.obj.db.Channel channel = DatabaseHelper.getChannelFromId(s, channelId); + + if (channel == null) { + ChannelInfo info = null; + + try { + info = ChannelInfo.getInfo("https://youtube.com/channel/" + channelId); + } catch (IOException | ExtractionException e) { + ExceptionUtils.rethrow(e); + } + + channel = new me.kavin.piped.utils.obj.db.Channel(channelId, info.getName(), info.getAvatarUrl(), + false); + s.save(channel); + + try { + subscribePubSub(channelId); + } catch (IOException | InterruptedException e) { + ExceptionUtils.rethrow(e); + } + + for (StreamInfoItem item : info.getRelatedItems()) { + long time = item.getUploadDate() != null + ? item.getUploadDate().offsetDateTime().toInstant().toEpochMilli() + : System.currentTimeMillis(); + if ((System.currentTimeMillis() - time) < TimeUnit.DAYS.toMillis(10)) + handleNewVideo(item.getUrl(), time); + } + } + + } + + s.beginTransaction().commit(); + + s.close(); + + return Constants.mapper.writeValueAsBytes(new AcceptedResponse()); + } + + s.close(); + + return Constants.mapper.writeValueAsBytes(new AuthenticationFailureResponse()); + + } + private static final String getLBRYStreamURL(String videoId) throws IOException, InterruptedException { String lbryId = new JSONObject(Constants.h2client.send(HttpRequest