mirror of
https://github.com/TeamPiped/Piped-Backend.git
synced 2024-08-14 23:51:41 +00:00
Allow mass importing subscriptions.
This commit is contained in:
parent
7a18a6e6b2
commit
e6711b197f
2 changed files with 71 additions and 0 deletions
|
@ -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);
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue