diff --git a/src/main/java/me/kavin/piped/ServerLauncher.java b/src/main/java/me/kavin/piped/ServerLauncher.java index 41bc670..1ebfbc4 100644 --- a/src/main/java/me/kavin/piped/ServerLauncher.java +++ b/src/main/java/me/kavin/piped/ServerLauncher.java @@ -1,8 +1,26 @@ package me.kavin.piped; +import static io.activej.config.converter.ConfigConverters.ofInetSocketAddress; +import static io.activej.http.HttpHeaders.CACHE_CONTROL; +import static io.activej.http.HttpHeaders.CONTENT_TYPE; + +import java.io.ByteArrayInputStream; +import java.net.InetSocketAddress; +import java.nio.charset.StandardCharsets; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; + +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.jetbrains.annotations.NotNull; +import org.schabi.newpipe.extractor.exceptions.AgeRestrictedContentException; +import org.schabi.newpipe.extractor.exceptions.ContentNotAvailableException; +import org.xml.sax.InputSource; + import com.fasterxml.jackson.core.JsonProcessingException; import com.rometools.rome.feed.synd.SyndFeed; import com.rometools.rome.io.SyndFeedInput; + import io.activej.config.Config; import io.activej.http.AsyncServlet; import io.activej.http.HttpMethod; @@ -17,22 +35,6 @@ import me.kavin.piped.utils.CustomServletDecorator; import me.kavin.piped.utils.ResponseHelper; import me.kavin.piped.utils.SponsorBlockUtils; import me.kavin.piped.utils.resp.ErrorResponse; -import org.apache.commons.lang3.exception.ExceptionUtils; -import org.jetbrains.annotations.NotNull; -import org.schabi.newpipe.extractor.exceptions.AgeRestrictedContentException; -import org.schabi.newpipe.extractor.exceptions.ContentNotAvailableException; -import org.xml.sax.InputSource; - -import java.io.ByteArrayInputStream; -import java.net.InetSocketAddress; -import java.nio.charset.StandardCharsets; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.Executor; -import java.util.concurrent.Executors; - -import static io.activej.config.converter.ConfigConverters.ofInetSocketAddress; -import static io.activej.http.HttpHeaders.CACHE_CONTROL; -import static io.activej.http.HttpHeaders.CONTENT_TYPE; public class ServerLauncher extends MultithreadedHttpServerLauncher { @@ -145,7 +147,8 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher { } })).map("/trending", AsyncServlet.ofBlocking(executor, request -> { try { - return getJsonResponse(ResponseHelper.trendingResponse(request.getQueryParameter("region")), "public, max-age=3600"); + return getJsonResponse(ResponseHelper.trendingResponse(request.getQueryParameter("region")), + "public, max-age=3600"); } catch (Exception e) { return getErrorResponse(e); } diff --git a/src/main/java/me/kavin/piped/utils/ResponseHelper.java b/src/main/java/me/kavin/piped/utils/ResponseHelper.java index 017933e..55e6c80 100644 --- a/src/main/java/me/kavin/piped/utils/ResponseHelper.java +++ b/src/main/java/me/kavin/piped/utils/ResponseHelper.java @@ -1,16 +1,17 @@ package me.kavin.piped.utils; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.github.benmanes.caffeine.cache.Caffeine; -import com.github.benmanes.caffeine.cache.LoadingCache; -import it.unimi.dsi.fastutil.objects.ObjectArrayList; -import me.kavin.piped.consts.Constants; -import me.kavin.piped.ipfs.IPFS; -import me.kavin.piped.utils.obj.*; -import me.kavin.piped.utils.obj.search.SearchChannel; -import me.kavin.piped.utils.obj.search.SearchItem; -import me.kavin.piped.utils.obj.search.SearchPlaylist; -import me.kavin.piped.utils.obj.search.SearchStream; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URL; +import java.net.http.HttpRequest; +import java.net.http.HttpRequest.BodyPublishers; +import java.net.http.HttpResponse.BodyHandlers; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; + import org.apache.commons.lang3.exception.ExceptionUtils; import org.json.JSONObject; import org.schabi.newpipe.extractor.InfoItem; @@ -32,17 +33,28 @@ import org.schabi.newpipe.extractor.search.SearchInfo; import org.schabi.newpipe.extractor.stream.StreamInfo; import org.schabi.newpipe.extractor.stream.StreamInfoItem; -import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URI; -import java.net.URL; -import java.net.http.HttpRequest; -import java.net.http.HttpRequest.BodyPublishers; -import java.net.http.HttpResponse.BodyHandlers; -import java.util.Collections; -import java.util.List; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.TimeUnit; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.github.benmanes.caffeine.cache.Caffeine; +import com.github.benmanes.caffeine.cache.LoadingCache; + +import it.unimi.dsi.fastutil.objects.ObjectArrayList; +import me.kavin.piped.consts.Constants; +import me.kavin.piped.ipfs.IPFS; +import me.kavin.piped.utils.obj.Channel; +import me.kavin.piped.utils.obj.ChapterSegment; +import me.kavin.piped.utils.obj.Comment; +import me.kavin.piped.utils.obj.CommentsPage; +import me.kavin.piped.utils.obj.PipedStream; +import me.kavin.piped.utils.obj.Playlist; +import me.kavin.piped.utils.obj.SearchResults; +import me.kavin.piped.utils.obj.StreamItem; +import me.kavin.piped.utils.obj.Streams; +import me.kavin.piped.utils.obj.StreamsPage; +import me.kavin.piped.utils.obj.Subtitle; +import me.kavin.piped.utils.obj.search.SearchChannel; +import me.kavin.piped.utils.obj.search.SearchItem; +import me.kavin.piped.utils.obj.search.SearchPlaylist; +import me.kavin.piped.utils.obj.search.SearchStream; public class ResponseHelper { @@ -192,15 +204,14 @@ public class ResponseHelper { } - public static final byte[] trendingResponse(String region) throws ParsingException, ExtractionException, IOException { - - region = (region == null || region.isEmpty()) ? "US" : region; + public static final byte[] trendingResponse(String region) + throws ParsingException, ExtractionException, IOException { final List relatedStreams = new ObjectArrayList<>(); KioskList kioskList = Constants.YOUTUBE_SERVICE.getKioskList(); kioskList.forceContentCountry(new ContentCountry(region)); - KioskExtractor extractor = kioskList.getDefaultKioskExtractor(); + KioskExtractor extractor = kioskList.getDefaultKioskExtractor(); extractor.fetchPage(); KioskInfo info = KioskInfo.getInfo(extractor); @@ -291,26 +302,26 @@ public class ResponseHelper { info.getRelatedItems().forEach(item -> { switch (item.getInfoType()) { - case STREAM: - StreamInfoItem stream = (StreamInfoItem) item; - items.add(new SearchStream(item.getName(), rewriteURL(item.getThumbnailUrl()), - item.getUrl().substring(23), stream.getTextualUploadDate(), stream.getUploaderName(), - optionalSubstring(stream.getUploaderUrl(), 23), stream.getViewCount(), stream.getDuration(), - stream.isUploaderVerified())); - break; - case CHANNEL: - ChannelInfoItem channel = (ChannelInfoItem) item; - items.add(new SearchChannel(item.getName(), rewriteURL(item.getThumbnailUrl()), - item.getUrl().substring(23), channel.getDescription(), channel.getSubscriberCount(), - channel.getStreamCount(), channel.isVerified())); - break; - case PLAYLIST: - PlaylistInfoItem playlist = (PlaylistInfoItem) item; - items.add(new SearchPlaylist(item.getName(), rewriteURL(item.getThumbnailUrl()), - item.getUrl().substring(23), playlist.getUploaderName(), playlist.getStreamCount())); - break; - default: - break; + case STREAM: + StreamInfoItem stream = (StreamInfoItem) item; + items.add(new SearchStream(item.getName(), rewriteURL(item.getThumbnailUrl()), + item.getUrl().substring(23), stream.getTextualUploadDate(), stream.getUploaderName(), + optionalSubstring(stream.getUploaderUrl(), 23), stream.getViewCount(), stream.getDuration(), + stream.isUploaderVerified())); + break; + case CHANNEL: + ChannelInfoItem channel = (ChannelInfoItem) item; + items.add(new SearchChannel(item.getName(), rewriteURL(item.getThumbnailUrl()), + item.getUrl().substring(23), channel.getDescription(), channel.getSubscriberCount(), + channel.getStreamCount(), channel.isVerified())); + break; + case PLAYLIST: + PlaylistInfoItem playlist = (PlaylistInfoItem) item; + items.add(new SearchPlaylist(item.getName(), rewriteURL(item.getThumbnailUrl()), + item.getUrl().substring(23), playlist.getUploaderName(), playlist.getStreamCount())); + break; + default: + break; } }); @@ -334,26 +345,26 @@ public class ResponseHelper { pages.getItems().forEach(item -> { switch (item.getInfoType()) { - case STREAM: - StreamInfoItem stream = (StreamInfoItem) item; - items.add(new SearchStream(item.getName(), rewriteURL(item.getThumbnailUrl()), - item.getUrl().substring(23), stream.getTextualUploadDate(), stream.getUploaderName(), - optionalSubstring(stream.getUploaderUrl(), 23), stream.getViewCount(), stream.getDuration(), - stream.isUploaderVerified())); - break; - case CHANNEL: - ChannelInfoItem channel = (ChannelInfoItem) item; - items.add(new SearchChannel(item.getName(), rewriteURL(item.getThumbnailUrl()), - item.getUrl().substring(23), channel.getDescription(), channel.getSubscriberCount(), - channel.getStreamCount(), channel.isVerified())); - break; - case PLAYLIST: - PlaylistInfoItem playlist = (PlaylistInfoItem) item; - items.add(new SearchPlaylist(item.getName(), rewriteURL(item.getThumbnailUrl()), - item.getUrl().substring(23), playlist.getUploaderName(), playlist.getStreamCount())); - break; - default: - break; + case STREAM: + StreamInfoItem stream = (StreamInfoItem) item; + items.add(new SearchStream(item.getName(), rewriteURL(item.getThumbnailUrl()), + item.getUrl().substring(23), stream.getTextualUploadDate(), stream.getUploaderName(), + optionalSubstring(stream.getUploaderUrl(), 23), stream.getViewCount(), stream.getDuration(), + stream.isUploaderVerified())); + break; + case CHANNEL: + ChannelInfoItem channel = (ChannelInfoItem) item; + items.add(new SearchChannel(item.getName(), rewriteURL(item.getThumbnailUrl()), + item.getUrl().substring(23), channel.getDescription(), channel.getSubscriberCount(), + channel.getStreamCount(), channel.isVerified())); + break; + case PLAYLIST: + PlaylistInfoItem playlist = (PlaylistInfoItem) item; + items.add(new SearchPlaylist(item.getName(), rewriteURL(item.getThumbnailUrl()), + item.getUrl().substring(23), playlist.getUploaderName(), playlist.getStreamCount())); + break; + default: + break; } }); @@ -425,7 +436,7 @@ public class ResponseHelper { String lbryId = new JSONObject(Constants.h2client.send(HttpRequest .newBuilder(URI.create("https://api.lbry.com/yt/resolve?video_ids=" + URLUtils.silentEncode(videoId))) .setHeader("User-Agent", Constants.USER_AGENT).build(), BodyHandlers.ofString()).body()) - .getJSONObject("data").getJSONObject("videos").optString(videoId); + .getJSONObject("data").getJSONObject("videos").optString(videoId); if (!lbryId.isEmpty()) new JSONObject(Constants.h2client.send(HttpRequest