Allow mass importing subscriptions.

This commit is contained in:
FireMasterK 2021-07-15 00:34:44 +05:30
parent 7a18a6e6b2
commit e6711b197f
No known key found for this signature in database
GPG key ID: 49451E4482CC5BCD
2 changed files with 71 additions and 0 deletions

View file

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

View file

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