performance: read and write data as byte arrays.

This commit is contained in:
FireMasterK 2021-01-12 13:45:09 +05:30
parent 687fa092f9
commit 6b22474093
No known key found for this signature in database
GPG Key ID: 8DFF5DD33E93DB58
2 changed files with 46 additions and 31 deletions

View File

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

View File

@ -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<StreamInfo> 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<StreamInfoItem> 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<StreamItem> relatedStreams = new ObjectArrayList<>();
public static final String trendingResponse() throws ParsingException, ExtractionException, IOException {
public static final byte[] trendingResponse() throws ParsingException, ExtractionException, IOException {
final List<StreamItem> 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<StreamInfoItem> 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<InfoItem> 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);
}