mirror of
https://github.com/TeamPiped/Piped-Backend.git
synced 2025-04-29 16:30:29 +05:30
Merge pull request #484 from TeamPiped/pubsub-improve
Attempt to improve pubsub subscription handling.
This commit is contained in:
commit
02a1c33d55
@ -19,7 +19,6 @@ import org.schabi.newpipe.extractor.localization.Localization;
|
|||||||
import org.schabi.newpipe.extractor.services.youtube.YoutubeThrottlingDecrypter;
|
import org.schabi.newpipe.extractor.services.youtube.YoutubeThrottlingDecrypter;
|
||||||
import org.schabi.newpipe.extractor.services.youtube.extractors.YoutubeStreamExtractor;
|
import org.schabi.newpipe.extractor.services.youtube.extractors.YoutubeStreamExtractor;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
@ -99,7 +98,7 @@ public class Main {
|
|||||||
.forEach(id -> Multithreading.runAsyncLimitedPubSub(() -> {
|
.forEach(id -> Multithreading.runAsyncLimitedPubSub(() -> {
|
||||||
try {
|
try {
|
||||||
PubSubHelper.subscribePubSub(id);
|
PubSubHelper.subscribePubSub(id);
|
||||||
} catch (IOException e) {
|
} catch (Exception e) {
|
||||||
ExceptionHandler.handle(e);
|
ExceptionHandler.handle(e);
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
@ -69,8 +69,15 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher {
|
|||||||
}))
|
}))
|
||||||
.map(GET, "/version", AsyncServlet.ofBlocking(executor, request -> getRawResponse(Constants.VERSION.getBytes(UTF_8), "text/plain", "no-store")))
|
.map(GET, "/version", AsyncServlet.ofBlocking(executor, request -> getRawResponse(Constants.VERSION.getBytes(UTF_8), "text/plain", "no-store")))
|
||||||
.map(HttpMethod.OPTIONS, "/*", request -> HttpResponse.ofCode(200))
|
.map(HttpMethod.OPTIONS, "/*", request -> HttpResponse.ofCode(200))
|
||||||
.map(GET, "/webhooks/pubsub", request -> HttpResponse.ok200().withPlainText(Objects.requireNonNull(request.getQueryParameter("hub.challenge"))))
|
.map(GET, "/webhooks/pubsub", AsyncServlet.ofBlocking(executor, request -> {
|
||||||
.map(POST, "/webhooks/pubsub", AsyncServlet.ofBlocking(executor, request -> {
|
var topic = request.getQueryParameter("hub.topic");
|
||||||
|
if (topic != null)
|
||||||
|
Multithreading.runAsync(() -> {
|
||||||
|
String channelId = StringUtils.substringAfter(topic, "channel_id=");
|
||||||
|
PubSubHelper.updatePubSub(channelId);
|
||||||
|
});
|
||||||
|
return HttpResponse.ok200().withPlainText(Objects.requireNonNull(request.getQueryParameter("hub.challenge")));
|
||||||
|
})).map(POST, "/webhooks/pubsub", AsyncServlet.ofBlocking(executor, request -> {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
SyndFeed feed = new SyndFeedInput().build(
|
SyndFeed feed = new SyndFeedInput().build(
|
||||||
|
@ -35,24 +35,26 @@ public class PubSubHelper {
|
|||||||
.newCall(builder.post(formBuilder.build())
|
.newCall(builder.post(formBuilder.build())
|
||||||
.build()).execute()) {
|
.build()).execute()) {
|
||||||
|
|
||||||
if (resp.code() == 202) {
|
if (resp.code() != 202)
|
||||||
try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) {
|
|
||||||
var tr = s.beginTransaction();
|
|
||||||
if (pubsub == null) {
|
|
||||||
pubsub = new PubSub(channelId, System.currentTimeMillis());
|
|
||||||
s.insert(pubsub);
|
|
||||||
} else {
|
|
||||||
pubsub.setSubbedAt(System.currentTimeMillis());
|
|
||||||
s.update(pubsub);
|
|
||||||
}
|
|
||||||
tr.commit();
|
|
||||||
}
|
|
||||||
|
|
||||||
} else
|
|
||||||
System.out.println("Failed to subscribe: " + resp.code() + "\n" + Objects.requireNonNull(resp.body()).string());
|
System.out.println("Failed to subscribe: " + resp.code() + "\n" + Objects.requireNonNull(resp.body()).string());
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void updatePubSub(String channelId) {
|
||||||
|
var pubsub = DatabaseHelper.getPubSubFromId(channelId);
|
||||||
|
try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) {
|
||||||
|
s.beginTransaction();
|
||||||
|
if (pubsub == null) {
|
||||||
|
pubsub = new PubSub(channelId, System.currentTimeMillis());
|
||||||
|
s.insert(pubsub);
|
||||||
|
} else {
|
||||||
|
pubsub.setSubbedAt(System.currentTimeMillis());
|
||||||
|
s.update(pubsub);
|
||||||
|
}
|
||||||
|
s.getTransaction().commit();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user