From 6b22474093bcc0bc77d8a84a29ae71026fac2c51 Mon Sep 17 00:00:00 2001 From: FireMasterK <20838718+FireMasterK@users.noreply.github.com> Date: Tue, 12 Jan 2021 13:45:09 +0530 Subject: [PATCH] performance: read and write data as byte arrays. --- src/main/java/me/kavin/piped/Main.java | 33 ++++++++++---- .../me/kavin/piped/utils/ResponseHelper.java | 44 +++++++++---------- 2 files changed, 46 insertions(+), 31 deletions(-) diff --git a/src/main/java/me/kavin/piped/Main.java b/src/main/java/me/kavin/piped/Main.java index d891472..528bbcf 100644 --- a/src/main/java/me/kavin/piped/Main.java +++ b/src/main/java/me/kavin/piped/Main.java @@ -20,13 +20,6 @@ public class Main { public static void main(String[] args) throws Exception { -// SyndFeed feed = new SyndFeedInput().build(new XmlReader(new FileInputStream("pubsub.xml"))); -// -// feed.getEntries().forEach(entry -> { -// System.out.println(entry.getLinks().get(0).getHref()); -// System.out.println(entry.getAuthors().get(0).getUri()); -// }); - NewPipe.init(new DownloaderImpl(), new Localization("en", "US")); HttpServer.create().port(Constants.PORT).route(routes -> { @@ -47,10 +40,22 @@ public class Main { routes.post("/webhooks/pubsub", (req, res) -> { try { - req.receive().asString().subscribe(str -> System.out.println(str)); + req.receive().asInputStream().subscribe(in -> { + try { + SyndFeed feed = new SyndFeedInput().build(new XmlReader(in)); + + feed.getEntries().forEach(entry -> { + System.out.println(entry.getLinks().get(0).getHref()); + System.out.println(entry.getAuthors().get(0).getUri()); + }); + + } catch (Exception e) { + e.printStackTrace(); + } + }); return writeResponse(res, "ok", 200, "private"); } catch (Exception e) { - e.printStackTrace(); + e.printStackTrace(); return writeResponse(res, ExceptionUtils.getStackTrace(e), 500, "private"); } @@ -201,4 +206,14 @@ public class Main { .send(ByteBufFlux.fromString(Flux.just(resp), java.nio.charset.StandardCharsets.UTF_8, ByteBufAllocator.DEFAULT)); } + + public static NettyOutbound writeResponse(HttpServerResponse res, byte[] resp, int code, String cache) { + return res.compression(true).addHeader("Access-Control-Allow-Origin", "*").addHeader("Cache-Control", cache) + .sendByteArray(Flux.just(resp)); + } + + public static NettyOutbound writeResponse(HttpServerResponse res, Flux resp, int code, String cache) { + return res.compression(true).addHeader("Access-Control-Allow-Origin", "*").addHeader("Cache-Control", cache) + .send(ByteBufFlux.fromString(resp, java.nio.charset.StandardCharsets.UTF_8, ByteBufAllocator.DEFAULT)); + } } diff --git a/src/main/java/me/kavin/piped/utils/ResponseHelper.java b/src/main/java/me/kavin/piped/utils/ResponseHelper.java index 057ed30..6da116e 100644 --- a/src/main/java/me/kavin/piped/utils/ResponseHelper.java +++ b/src/main/java/me/kavin/piped/utils/ResponseHelper.java @@ -51,7 +51,7 @@ public class ResponseHelper { .expireAfterWrite(1, TimeUnit.HOURS).maximumSize(1000) .build(key -> CommentsInfo.getInfo("https://www.youtube.com/watch?v=" + key)); - public static final String streamsResponse(String videoId) throws Exception { + public static final byte[] streamsResponse(String videoId) throws Exception { CompletableFuture futureStream = CompletableFuture.supplyAsync(() -> { try { @@ -134,11 +134,11 @@ public class ResponseHelper { info.getViewCount(), info.getLikeCount(), info.getDislikeCount(), audioStreams, videoStreams, relatedStreams, subtitles, livestream, hls); - return Constants.mapper.writeValueAsString(streams); + return Constants.mapper.writeValueAsBytes(streams); } - public static final String channelResponse(String channelId) + public static final byte[] channelResponse(String channelId) throws IOException, ExtractionException, InterruptedException { final ChannelInfo info = ChannelInfo.getInfo("https://youtube.com/channel/" + channelId); @@ -157,11 +157,11 @@ public class ResponseHelper { final Channel channel = new Channel(info.getName(), rewriteURL(info.getAvatarUrl()), rewriteURL(info.getBannerUrl()), info.getDescription(), nextpage, relatedStreams); - return Constants.mapper.writeValueAsString(channel); + return Constants.mapper.writeValueAsBytes(channel); } - public static final String channelPageResponse(String channelId, String url) + public static final byte[] channelPageResponse(String channelId, String url) throws IOException, ExtractionException, InterruptedException { InfoItemsPage page = ChannelInfo.getMoreItems(Constants.YOUTUBE_SERVICE, @@ -180,13 +180,13 @@ public class ResponseHelper { final StreamsPage streamspage = new StreamsPage(nextpage, relatedStreams); - return Constants.mapper.writeValueAsString(streamspage); + return Constants.mapper.writeValueAsBytes(streamspage); } final List relatedStreams = new ObjectArrayList<>(); - public static final String trendingResponse() throws ParsingException, ExtractionException, IOException { + public static final byte[] trendingResponse() throws ParsingException, ExtractionException, IOException { final List relatedStreams = new ObjectArrayList<>(); @@ -201,10 +201,10 @@ public class ResponseHelper { item.getTextualUploadDate(), item.getDuration(), item.getViewCount())); }); - return Constants.mapper.writeValueAsString(relatedStreams); + return Constants.mapper.writeValueAsBytes(relatedStreams); } - public static final String playlistResponse(String playlistId) + public static final byte[] playlistResponse(String playlistId) throws IOException, ExtractionException, InterruptedException { final PlaylistInfo info = PlaylistInfo.getInfo("https://www.youtube.com/playlist?list=" + playlistId); @@ -224,11 +224,11 @@ public class ResponseHelper { rewriteURL(info.getBannerUrl()), nextpage, info.getUploaderName(), info.getUploaderUrl().substring(23), rewriteURL(info.getUploaderAvatarUrl()), (int) info.getStreamCount(), relatedStreams); - return Constants.mapper.writeValueAsString(playlist); + return Constants.mapper.writeValueAsBytes(playlist); } - public static final String playlistPageResponse(String playlistId, String url) + public static final byte[] playlistPageResponse(String playlistId, String url) throws IOException, ExtractionException, InterruptedException { InfoItemsPage page = PlaylistInfo.getMoreItems(Constants.YOUTUBE_SERVICE, @@ -247,19 +247,19 @@ public class ResponseHelper { final StreamsPage streamspage = new StreamsPage(nextpage, relatedStreams); - return Constants.mapper.writeValueAsString(streamspage); + return Constants.mapper.writeValueAsBytes(streamspage); } - public static final String suggestionsResponse(String query) + public static final byte[] suggestionsResponse(String query) throws JsonProcessingException, IOException, ExtractionException { return Constants.mapper - .writeValueAsString(Constants.YOUTUBE_SERVICE.getSuggestionExtractor().suggestionList(query)); + .writeValueAsBytes(Constants.YOUTUBE_SERVICE.getSuggestionExtractor().suggestionList(query)); } - public static final String searchResponse(String q) throws IOException, ExtractionException, InterruptedException { + public static final byte[] searchResponse(String q) throws IOException, ExtractionException, InterruptedException { final SearchInfo info = SearchInfo.getInfo(Constants.YOUTUBE_SERVICE, Constants.YOUTUBE_SERVICE.getSearchQHFactory().fromQuery(q)); @@ -285,12 +285,12 @@ public class ResponseHelper { Page nextpage = info.getNextPage(); return nextpage != null - ? Constants.mapper.writeValueAsString(new SearchResults(nextpage.getUrl(), nextpage.getId(), items)) - : Constants.mapper.writeValueAsString(new SearchResults(null, null, items)); + ? Constants.mapper.writeValueAsBytes(new SearchResults(nextpage.getUrl(), nextpage.getId(), items)) + : Constants.mapper.writeValueAsBytes(new SearchResults(null, null, items)); } - public static final String searchPageResponse(String q, String url, String id) + public static final byte[] searchPageResponse(String q, String url, String id) throws IOException, ExtractionException, InterruptedException { InfoItemsPage pages = SearchInfo.getMoreItems(Constants.YOUTUBE_SERVICE, @@ -317,14 +317,14 @@ public class ResponseHelper { Page nextpage = pages.getNextPage(); return nextpage != null - ? Constants.mapper.writeValueAsString(new SearchResults(nextpage.getUrl(), nextpage.getId(), items)) - : Constants.mapper.writeValueAsString(new SearchResults(null, null, items)); + ? Constants.mapper.writeValueAsBytes(new SearchResults(nextpage.getUrl(), nextpage.getId(), items)) + : Constants.mapper.writeValueAsBytes(new SearchResults(null, null, items)); } - public static final String registerResponse(String user, String pass) throws IOException { + public static final byte[] registerResponse(String user, String pass) throws IOException { - return Constants.mapper.writeValueAsString(null); + return Constants.mapper.writeValueAsBytes(null); }