2020-11-12 21:19:45 +00:00
|
|
|
package me.kavin.piped.utils;
|
|
|
|
|
|
|
|
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;
|
2021-07-04 18:57:18 +00:00
|
|
|
import java.nio.charset.StandardCharsets;
|
2021-06-14 19:44:23 +00:00
|
|
|
import java.util.Collections;
|
2020-11-12 21:19:45 +00:00
|
|
|
import java.util.List;
|
2020-11-17 05:34:50 +00:00
|
|
|
import java.util.concurrent.CompletableFuture;
|
2020-11-12 21:19:45 +00:00
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
2020-11-17 05:34:50 +00:00
|
|
|
import org.apache.commons.lang3.exception.ExceptionUtils;
|
2020-11-12 21:19:45 +00:00
|
|
|
import org.json.JSONObject;
|
2020-12-14 07:11:42 +00:00
|
|
|
import org.schabi.newpipe.extractor.InfoItem;
|
2020-11-25 05:26:25 +00:00
|
|
|
import org.schabi.newpipe.extractor.ListExtractor.InfoItemsPage;
|
|
|
|
import org.schabi.newpipe.extractor.Page;
|
2020-11-12 21:19:45 +00:00
|
|
|
import org.schabi.newpipe.extractor.channel.ChannelInfo;
|
2021-05-28 12:08:32 +00:00
|
|
|
import org.schabi.newpipe.extractor.channel.ChannelInfoItem;
|
2020-11-12 21:19:45 +00:00
|
|
|
import org.schabi.newpipe.extractor.comments.CommentsInfo;
|
2021-04-03 15:08:32 +00:00
|
|
|
import org.schabi.newpipe.extractor.comments.CommentsInfoItem;
|
2020-11-12 21:19:45 +00:00
|
|
|
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
|
|
|
|
import org.schabi.newpipe.extractor.exceptions.ParsingException;
|
2021-07-04 17:53:33 +00:00
|
|
|
import org.schabi.newpipe.extractor.kiosk.KioskExtractor;
|
2020-11-12 21:19:45 +00:00
|
|
|
import org.schabi.newpipe.extractor.kiosk.KioskInfo;
|
2021-07-04 17:53:33 +00:00
|
|
|
import org.schabi.newpipe.extractor.kiosk.KioskList;
|
|
|
|
import org.schabi.newpipe.extractor.localization.ContentCountry;
|
2021-01-04 05:47:28 +00:00
|
|
|
import org.schabi.newpipe.extractor.playlist.PlaylistInfo;
|
2021-05-28 12:08:32 +00:00
|
|
|
import org.schabi.newpipe.extractor.playlist.PlaylistInfoItem;
|
2020-12-09 13:30:42 +00:00
|
|
|
import org.schabi.newpipe.extractor.search.SearchInfo;
|
2020-11-12 21:19:45 +00:00
|
|
|
import org.schabi.newpipe.extractor.stream.StreamInfo;
|
|
|
|
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
|
|
|
|
|
2020-11-25 05:26:25 +00:00
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
2020-11-12 21:19:45 +00:00
|
|
|
import com.github.benmanes.caffeine.cache.Caffeine;
|
|
|
|
import com.github.benmanes.caffeine.cache.LoadingCache;
|
2021-07-04 18:57:18 +00:00
|
|
|
import com.rometools.rome.feed.synd.SyndEntry;
|
|
|
|
import com.rometools.rome.feed.synd.SyndEntryImpl;
|
|
|
|
import com.rometools.rome.feed.synd.SyndFeed;
|
|
|
|
import com.rometools.rome.feed.synd.SyndFeedImpl;
|
|
|
|
import com.rometools.rome.io.FeedException;
|
|
|
|
import com.rometools.rome.io.SyndFeedOutput;
|
2020-11-12 21:19:45 +00:00
|
|
|
|
|
|
|
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
|
|
|
import me.kavin.piped.consts.Constants;
|
2021-02-24 09:52:29 +00:00
|
|
|
import me.kavin.piped.ipfs.IPFS;
|
2020-11-12 21:19:45 +00:00
|
|
|
import me.kavin.piped.utils.obj.Channel;
|
2021-02-24 09:52:29 +00:00
|
|
|
import me.kavin.piped.utils.obj.ChapterSegment;
|
2021-04-03 15:08:32 +00:00
|
|
|
import me.kavin.piped.utils.obj.Comment;
|
|
|
|
import me.kavin.piped.utils.obj.CommentsPage;
|
2020-11-25 05:26:25 +00:00
|
|
|
import me.kavin.piped.utils.obj.PipedStream;
|
2021-01-04 05:47:28 +00:00
|
|
|
import me.kavin.piped.utils.obj.Playlist;
|
2020-12-14 07:11:42 +00:00
|
|
|
import me.kavin.piped.utils.obj.SearchResults;
|
2020-11-12 21:19:45 +00:00
|
|
|
import me.kavin.piped.utils.obj.StreamItem;
|
|
|
|
import me.kavin.piped.utils.obj.Streams;
|
2021-01-04 05:47:28 +00:00
|
|
|
import me.kavin.piped.utils.obj.StreamsPage;
|
2020-11-12 21:19:45 +00:00
|
|
|
import me.kavin.piped.utils.obj.Subtitle;
|
2021-05-28 12:08:32 +00:00
|
|
|
import me.kavin.piped.utils.obj.search.SearchChannel;
|
2020-12-09 13:30:42 +00:00
|
|
|
import me.kavin.piped.utils.obj.search.SearchItem;
|
2021-05-28 12:08:32 +00:00
|
|
|
import me.kavin.piped.utils.obj.search.SearchPlaylist;
|
2020-12-09 13:30:42 +00:00
|
|
|
import me.kavin.piped.utils.obj.search.SearchStream;
|
2020-11-12 21:19:45 +00:00
|
|
|
|
|
|
|
public class ResponseHelper {
|
|
|
|
|
|
|
|
public static final LoadingCache<String, CommentsInfo> commentsCache = Caffeine.newBuilder()
|
2021-02-24 09:52:29 +00:00
|
|
|
.expireAfterWrite(1, TimeUnit.HOURS).maximumSize(1000)
|
|
|
|
.build(key -> CommentsInfo.getInfo("https://www.youtube.com/watch?v=" + key));
|
2020-11-12 21:19:45 +00:00
|
|
|
|
2021-01-12 08:15:09 +00:00
|
|
|
public static final byte[] streamsResponse(String videoId) throws Exception {
|
2020-11-17 05:34:50 +00:00
|
|
|
|
2021-02-24 09:52:29 +00:00
|
|
|
CompletableFuture<StreamInfo> futureStream = CompletableFuture.supplyAsync(() -> {
|
|
|
|
try {
|
|
|
|
return StreamInfo.getInfo("https://www.youtube.com/watch?v=" + videoId);
|
|
|
|
} catch (Exception e) {
|
|
|
|
ExceptionUtils.rethrow(e);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
|
|
|
CompletableFuture<String> futureLBRY = CompletableFuture.supplyAsync(() -> {
|
|
|
|
try {
|
|
|
|
return getLBRYStreamURL(videoId);
|
|
|
|
} catch (Exception e) {
|
|
|
|
ExceptionUtils.rethrow(e);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
|
|
|
final List<Subtitle> subtitles = new ObjectArrayList<>();
|
|
|
|
|
2021-05-28 15:17:25 +00:00
|
|
|
final StreamInfo info = futureStream.get();
|
2021-02-24 09:52:29 +00:00
|
|
|
|
|
|
|
// System.out.println(Constants.mapper.writeValueAsString(info.getStreamSegments()));
|
2021-04-21 14:22:58 +00:00
|
|
|
info.getSubtitles()
|
|
|
|
.forEach(subtitle -> subtitles.add(new Subtitle(rewriteURL(subtitle.getUrl()),
|
|
|
|
subtitle.getFormat().getMimeType(), subtitle.getDisplayLanguageName(),
|
|
|
|
subtitle.getLanguageTag(), subtitle.isAutoGenerated())));
|
2021-02-24 09:52:29 +00:00
|
|
|
|
|
|
|
final List<PipedStream> videoStreams = new ObjectArrayList<>();
|
|
|
|
final List<PipedStream> audioStreams = new ObjectArrayList<>();
|
|
|
|
|
2021-05-28 15:17:25 +00:00
|
|
|
final String lbryURL = futureLBRY.get();
|
2021-02-24 09:52:29 +00:00
|
|
|
|
|
|
|
if (lbryURL != null)
|
|
|
|
videoStreams.add(new PipedStream(lbryURL, "MP4", "LBRY", "video/mp4", false));
|
|
|
|
|
|
|
|
final String hls;
|
|
|
|
boolean livestream = false;
|
|
|
|
|
|
|
|
if ((hls = info.getHlsUrl()) != null && !hls.isEmpty())
|
|
|
|
livestream = true;
|
|
|
|
|
|
|
|
if (!livestream) {
|
|
|
|
info.getVideoOnlyStreams().forEach(stream -> videoStreams.add(new PipedStream(rewriteURL(stream.getUrl()),
|
|
|
|
String.valueOf(stream.getFormat()), stream.getResolution(), stream.getFormat().getMimeType(), true,
|
|
|
|
stream.getBitrate(), stream.getInitStart(), stream.getInitEnd(), stream.getIndexStart(),
|
|
|
|
stream.getIndexEnd(), stream.getCodec(), stream.getWidth(), stream.getHeight(), 30)));
|
|
|
|
info.getVideoStreams()
|
|
|
|
.forEach(stream -> videoStreams
|
|
|
|
.add(new PipedStream(rewriteURL(stream.getUrl()), String.valueOf(stream.getFormat()),
|
|
|
|
stream.getResolution(), stream.getFormat().getMimeType(), false)));
|
|
|
|
|
|
|
|
info.getAudioStreams()
|
|
|
|
.forEach(stream -> audioStreams.add(new PipedStream(rewriteURL(stream.getUrl()),
|
|
|
|
String.valueOf(stream.getFormat()), stream.getAverageBitrate() + " kbps",
|
|
|
|
stream.getFormat().getMimeType(), false, stream.getBitrate(), stream.getInitStart(),
|
|
|
|
stream.getInitEnd(), stream.getIndexStart(), stream.getIndexEnd(), stream.getCodec())));
|
|
|
|
}
|
|
|
|
|
|
|
|
final List<StreamItem> relatedStreams = new ObjectArrayList<>();
|
|
|
|
|
2021-04-21 14:02:50 +00:00
|
|
|
info.getRelatedItems().forEach(o -> {
|
2021-02-24 09:52:29 +00:00
|
|
|
StreamInfoItem item = (StreamInfoItem) o;
|
|
|
|
relatedStreams.add(new StreamItem(item.getUrl().substring(23), item.getName(),
|
|
|
|
rewriteURL(item.getThumbnailUrl()), item.getUploaderName(), item.getUploaderUrl().substring(23),
|
|
|
|
item.getTextualUploadDate(), item.getDuration(), item.getViewCount()));
|
|
|
|
});
|
|
|
|
|
|
|
|
List<ChapterSegment> segments = new ObjectArrayList<>();
|
|
|
|
|
|
|
|
info.getStreamSegments().forEach(
|
|
|
|
segment -> segments.add(new ChapterSegment(segment.getTitle(), segment.getStartTimeSeconds())));
|
|
|
|
|
|
|
|
final Streams streams = new Streams(info.getName(), info.getDescription().getContent(),
|
|
|
|
info.getTextualUploadDate(), info.getUploaderName(), info.getUploaderUrl().substring(23),
|
|
|
|
rewriteURL(info.getUploaderAvatarUrl()), rewriteURL(info.getThumbnailUrl()), info.getDuration(),
|
|
|
|
info.getViewCount(), info.getLikeCount(), info.getDislikeCount(), audioStreams, videoStreams,
|
|
|
|
relatedStreams, subtitles, livestream, hls);
|
|
|
|
|
|
|
|
return Constants.mapper.writeValueAsBytes(streams);
|
2020-11-12 21:19:45 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-05-28 18:40:11 +00:00
|
|
|
public static final byte[] channelResponse(String channelPath) throws Exception {
|
2020-11-12 21:19:45 +00:00
|
|
|
|
2021-05-28 18:40:11 +00:00
|
|
|
final ChannelInfo info = ChannelInfo.getInfo("https://youtube.com/" + channelPath);
|
2020-11-12 21:19:45 +00:00
|
|
|
|
2021-02-24 09:52:29 +00:00
|
|
|
final List<StreamItem> relatedStreams = new ObjectArrayList<>();
|
2020-11-12 21:19:45 +00:00
|
|
|
|
2021-02-24 09:52:29 +00:00
|
|
|
info.getRelatedItems().forEach(o -> {
|
|
|
|
StreamInfoItem item = o;
|
|
|
|
relatedStreams.add(new StreamItem(item.getUrl().substring(23), item.getName(),
|
|
|
|
rewriteURL(item.getThumbnailUrl()), item.getUploaderName(), item.getUploaderUrl().substring(23),
|
|
|
|
item.getTextualUploadDate(), item.getDuration(), item.getViewCount()));
|
|
|
|
});
|
2020-11-12 21:19:45 +00:00
|
|
|
|
2021-06-05 19:34:33 +00:00
|
|
|
String nextpage = null;
|
2021-03-29 13:59:10 +00:00
|
|
|
if (info.hasNextPage()) {
|
|
|
|
Page page = info.getNextPage();
|
2021-06-05 19:34:33 +00:00
|
|
|
nextpage = Constants.mapper.writeValueAsString(page);
|
2021-03-29 13:59:10 +00:00
|
|
|
}
|
2020-11-25 05:26:25 +00:00
|
|
|
|
2021-02-24 09:52:29 +00:00
|
|
|
final Channel channel = new Channel(info.getId(), info.getName(), rewriteURL(info.getAvatarUrl()),
|
2021-06-05 19:34:33 +00:00
|
|
|
rewriteURL(info.getBannerUrl()), info.getDescription(), nextpage, relatedStreams);
|
2020-11-12 21:19:45 +00:00
|
|
|
|
2021-02-24 09:52:29 +00:00
|
|
|
IPFS.publishData(channel);
|
|
|
|
|
|
|
|
return Constants.mapper.writeValueAsBytes(channel);
|
2020-11-12 21:19:45 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-06-05 19:34:33 +00:00
|
|
|
public static final byte[] channelPageResponse(String channelId, String prevpageStr)
|
2021-02-24 09:52:29 +00:00
|
|
|
throws IOException, ExtractionException, InterruptedException {
|
2020-11-25 05:26:25 +00:00
|
|
|
|
2021-06-05 19:34:33 +00:00
|
|
|
Page prevpage = Constants.mapper.readValue(prevpageStr, Page.class);
|
|
|
|
|
2021-03-30 09:22:36 +00:00
|
|
|
InfoItemsPage<StreamInfoItem> info = ChannelInfo.getMoreItems(Constants.YOUTUBE_SERVICE,
|
2021-06-05 19:34:33 +00:00
|
|
|
"https://youtube.com/channel/" + channelId, prevpage);
|
2020-11-25 05:26:25 +00:00
|
|
|
|
2021-02-24 09:52:29 +00:00
|
|
|
final List<StreamItem> relatedStreams = new ObjectArrayList<>();
|
2020-11-25 05:26:25 +00:00
|
|
|
|
2021-03-30 09:22:36 +00:00
|
|
|
info.getItems().forEach(o -> {
|
2021-02-24 09:52:29 +00:00
|
|
|
StreamInfoItem item = o;
|
|
|
|
relatedStreams.add(new StreamItem(item.getUrl().substring(23), item.getName(),
|
|
|
|
rewriteURL(item.getThumbnailUrl()), item.getUploaderName(), item.getUploaderUrl().substring(23),
|
|
|
|
item.getTextualUploadDate(), item.getDuration(), item.getViewCount()));
|
|
|
|
});
|
2020-11-25 05:26:25 +00:00
|
|
|
|
2021-06-05 19:34:33 +00:00
|
|
|
String nextpage = null;
|
2021-03-30 09:22:36 +00:00
|
|
|
if (info.hasNextPage()) {
|
|
|
|
Page page = info.getNextPage();
|
2021-06-05 19:34:33 +00:00
|
|
|
nextpage = Constants.mapper.writeValueAsString(page);
|
2021-03-30 09:22:36 +00:00
|
|
|
}
|
2020-11-25 05:26:25 +00:00
|
|
|
|
2021-06-05 19:34:33 +00:00
|
|
|
final StreamsPage streamspage = new StreamsPage(nextpage, relatedStreams);
|
2020-11-25 05:26:25 +00:00
|
|
|
|
2021-02-24 09:52:29 +00:00
|
|
|
return Constants.mapper.writeValueAsBytes(streamspage);
|
2020-11-25 05:26:25 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-07-04 17:53:33 +00:00
|
|
|
public static final byte[] trendingResponse(String region)
|
|
|
|
throws ParsingException, ExtractionException, IOException {
|
2020-11-12 21:19:45 +00:00
|
|
|
|
2021-02-24 09:52:29 +00:00
|
|
|
final List<StreamItem> relatedStreams = new ObjectArrayList<>();
|
2020-11-12 21:19:45 +00:00
|
|
|
|
2021-07-04 17:53:33 +00:00
|
|
|
KioskList kioskList = Constants.YOUTUBE_SERVICE.getKioskList();
|
|
|
|
kioskList.forceContentCountry(new ContentCountry(region));
|
|
|
|
KioskExtractor<?> extractor = kioskList.getDefaultKioskExtractor();
|
|
|
|
extractor.fetchPage();
|
|
|
|
KioskInfo info = KioskInfo.getInfo(extractor);
|
2020-11-12 21:19:45 +00:00
|
|
|
|
2021-02-24 09:52:29 +00:00
|
|
|
info.getRelatedItems().forEach(o -> {
|
|
|
|
StreamInfoItem item = o;
|
|
|
|
relatedStreams.add(new StreamItem(item.getUrl().substring(23), item.getName(),
|
|
|
|
rewriteURL(item.getThumbnailUrl()), item.getUploaderName(), item.getUploaderUrl().substring(23),
|
|
|
|
item.getTextualUploadDate(), item.getDuration(), item.getViewCount()));
|
|
|
|
});
|
2020-11-12 21:19:45 +00:00
|
|
|
|
2021-02-24 09:52:29 +00:00
|
|
|
return Constants.mapper.writeValueAsBytes(relatedStreams);
|
2020-11-12 21:19:45 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 08:15:09 +00:00
|
|
|
public static final byte[] playlistResponse(String playlistId)
|
2021-02-24 09:52:29 +00:00
|
|
|
throws IOException, ExtractionException, InterruptedException {
|
2021-01-04 05:47:28 +00:00
|
|
|
|
2021-02-24 09:52:29 +00:00
|
|
|
final PlaylistInfo info = PlaylistInfo.getInfo("https://www.youtube.com/playlist?list=" + playlistId);
|
2021-01-04 05:47:28 +00:00
|
|
|
|
2021-02-24 09:52:29 +00:00
|
|
|
final List<StreamItem> relatedStreams = new ObjectArrayList<>();
|
2021-01-04 05:47:28 +00:00
|
|
|
|
2021-02-24 09:52:29 +00:00
|
|
|
info.getRelatedItems().forEach(o -> {
|
|
|
|
StreamInfoItem item = o;
|
|
|
|
relatedStreams.add(new StreamItem(item.getUrl().substring(23), item.getName(),
|
|
|
|
rewriteURL(item.getThumbnailUrl()), item.getUploaderName(), item.getUploaderUrl().substring(23),
|
|
|
|
item.getTextualUploadDate(), item.getDuration(), item.getViewCount()));
|
|
|
|
});
|
2021-01-04 05:47:28 +00:00
|
|
|
|
2021-06-05 19:34:33 +00:00
|
|
|
String nextpage = null;
|
2021-03-30 09:36:01 +00:00
|
|
|
if (info.hasNextPage()) {
|
|
|
|
Page page = info.getNextPage();
|
2021-06-05 19:34:33 +00:00
|
|
|
nextpage = Constants.mapper.writeValueAsString(page);
|
2021-03-30 09:36:01 +00:00
|
|
|
}
|
2021-01-04 05:47:28 +00:00
|
|
|
|
2021-02-24 09:52:29 +00:00
|
|
|
final Playlist playlist = new Playlist(info.getName(), rewriteURL(info.getThumbnailUrl()),
|
2021-06-05 19:34:33 +00:00
|
|
|
rewriteURL(info.getBannerUrl()), nextpage,
|
2021-06-01 18:52:48 +00:00
|
|
|
info.getUploaderName().isEmpty() ? null : info.getUploaderName(),
|
|
|
|
info.getUploaderUrl().isEmpty() ? null : info.getUploaderUrl().substring(23),
|
|
|
|
rewriteURL(info.getUploaderAvatarUrl()), (int) info.getStreamCount(), relatedStreams);
|
2021-01-04 05:47:28 +00:00
|
|
|
|
2021-02-24 09:52:29 +00:00
|
|
|
return Constants.mapper.writeValueAsBytes(playlist);
|
2021-01-04 05:47:28 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-06-05 19:34:33 +00:00
|
|
|
public static final byte[] playlistPageResponse(String playlistId, String prevpageStr)
|
2021-02-24 09:52:29 +00:00
|
|
|
throws IOException, ExtractionException, InterruptedException {
|
2021-01-04 05:47:28 +00:00
|
|
|
|
2021-06-05 19:34:33 +00:00
|
|
|
Page prevpage = Constants.mapper.readValue(prevpageStr, Page.class);
|
|
|
|
|
2021-03-30 09:36:01 +00:00
|
|
|
InfoItemsPage<StreamInfoItem> info = PlaylistInfo.getMoreItems(Constants.YOUTUBE_SERVICE,
|
2021-06-05 19:34:33 +00:00
|
|
|
"https://www.youtube.com/playlist?list=" + playlistId, prevpage);
|
2021-01-04 05:47:28 +00:00
|
|
|
|
2021-02-24 09:52:29 +00:00
|
|
|
final List<StreamItem> relatedStreams = new ObjectArrayList<>();
|
2021-01-04 05:47:28 +00:00
|
|
|
|
2021-03-30 09:36:01 +00:00
|
|
|
info.getItems().forEach(o -> {
|
2021-02-24 09:52:29 +00:00
|
|
|
StreamInfoItem item = o;
|
|
|
|
relatedStreams.add(new StreamItem(item.getUrl().substring(23), item.getName(),
|
|
|
|
rewriteURL(item.getThumbnailUrl()), item.getUploaderName(), item.getUploaderUrl().substring(23),
|
|
|
|
item.getTextualUploadDate(), item.getDuration(), item.getViewCount()));
|
|
|
|
});
|
2021-01-04 05:47:28 +00:00
|
|
|
|
2021-06-05 19:34:33 +00:00
|
|
|
String nextpage = null;
|
2021-03-30 09:36:01 +00:00
|
|
|
if (info.hasNextPage()) {
|
|
|
|
Page page = info.getNextPage();
|
2021-06-05 19:34:33 +00:00
|
|
|
nextpage = Constants.mapper.writeValueAsString(page);
|
2021-03-30 09:36:01 +00:00
|
|
|
}
|
2021-01-04 05:47:28 +00:00
|
|
|
|
2021-06-05 19:34:33 +00:00
|
|
|
final StreamsPage streamspage = new StreamsPage(nextpage, relatedStreams);
|
2021-01-04 05:47:28 +00:00
|
|
|
|
2021-02-24 09:52:29 +00:00
|
|
|
return Constants.mapper.writeValueAsBytes(streamspage);
|
2021-01-04 05:47:28 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-07-04 18:57:18 +00:00
|
|
|
public static final byte[] playlistRSSResponse(String playlistId)
|
|
|
|
throws IOException, ExtractionException, InterruptedException, FeedException {
|
|
|
|
|
|
|
|
final PlaylistInfo info = PlaylistInfo.getInfo("https://www.youtube.com/playlist?list=" + playlistId);
|
|
|
|
|
|
|
|
final List<SyndEntry> entries = new ObjectArrayList<>();
|
|
|
|
|
|
|
|
SyndFeed feed = new SyndFeedImpl();
|
|
|
|
feed.setFeedType("rss_2.0");
|
|
|
|
feed.setTitle(info.getName());
|
|
|
|
feed.setAuthor(info.getUploaderName());
|
|
|
|
feed.setLink(info.getUrl());
|
|
|
|
feed.setDescription(String.format("%s - Piped", info.getName()));
|
|
|
|
|
|
|
|
info.getRelatedItems().forEach(o -> {
|
|
|
|
StreamInfoItem item = o;
|
|
|
|
SyndEntry entry = new SyndEntryImpl();
|
|
|
|
entry.setAuthor(item.getUploaderName());
|
|
|
|
entry.setUri(item.getUrl());
|
|
|
|
entry.setTitle(item.getName());
|
|
|
|
entries.add(entry);
|
|
|
|
});
|
|
|
|
|
|
|
|
feed.setEntries(entries);
|
|
|
|
|
|
|
|
return new SyndFeedOutput().outputString(feed).getBytes(StandardCharsets.UTF_8);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-01-12 08:15:09 +00:00
|
|
|
public static final byte[] suggestionsResponse(String query)
|
2021-02-24 09:52:29 +00:00
|
|
|
throws JsonProcessingException, IOException, ExtractionException {
|
2020-11-25 05:26:25 +00:00
|
|
|
|
2021-02-24 09:52:29 +00:00
|
|
|
return Constants.mapper
|
|
|
|
.writeValueAsBytes(Constants.YOUTUBE_SERVICE.getSuggestionExtractor().suggestionList(query));
|
2020-11-25 05:26:25 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-06-14 19:44:23 +00:00
|
|
|
public static final byte[] searchResponse(String q, String filter)
|
|
|
|
throws IOException, ExtractionException, InterruptedException {
|
2020-12-09 13:30:42 +00:00
|
|
|
|
2021-02-24 09:52:29 +00:00
|
|
|
final SearchInfo info = SearchInfo.getInfo(Constants.YOUTUBE_SERVICE,
|
2021-06-14 19:44:23 +00:00
|
|
|
Constants.YOUTUBE_SERVICE.getSearchQHFactory().fromQuery(q, Collections.singletonList(filter), null));
|
2021-02-24 09:52:29 +00:00
|
|
|
|
|
|
|
ObjectArrayList<SearchItem> items = new ObjectArrayList<>();
|
|
|
|
|
|
|
|
info.getRelatedItems().forEach(item -> {
|
|
|
|
switch (item.getInfoType()) {
|
|
|
|
case STREAM:
|
|
|
|
StreamInfoItem stream = (StreamInfoItem) item;
|
|
|
|
items.add(new SearchStream(item.getName(), rewriteURL(item.getThumbnailUrl()),
|
2021-05-06 16:26:03 +00:00
|
|
|
item.getUrl().substring(23), stream.getTextualUploadDate(), stream.getUploaderName(),
|
2021-06-14 19:44:23 +00:00
|
|
|
optionalSubstring(stream.getUploaderUrl(), 23), stream.getViewCount(), stream.getDuration(),
|
2021-05-28 12:08:32 +00:00
|
|
|
stream.isUploaderVerified()));
|
2021-02-24 09:52:29 +00:00
|
|
|
break;
|
|
|
|
case CHANNEL:
|
2021-05-28 12:08:32 +00:00
|
|
|
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()));
|
2021-02-24 09:52:29 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Page nextpage = info.getNextPage();
|
|
|
|
|
2021-06-14 19:44:23 +00:00
|
|
|
return Constants.mapper
|
|
|
|
.writeValueAsBytes(new SearchResults(items, Constants.mapper.writeValueAsString(nextpage)));
|
2020-12-14 07:11:42 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-06-14 19:44:23 +00:00
|
|
|
public static final byte[] searchPageResponse(String q, String filter, String prevpageStr)
|
2021-02-24 09:52:29 +00:00
|
|
|
throws IOException, ExtractionException, InterruptedException {
|
|
|
|
|
2021-06-14 19:44:23 +00:00
|
|
|
Page prevpage = Constants.mapper.readValue(prevpageStr, Page.class);
|
|
|
|
|
2021-02-24 09:52:29 +00:00
|
|
|
InfoItemsPage<InfoItem> pages = SearchInfo.getMoreItems(Constants.YOUTUBE_SERVICE,
|
2021-06-14 19:44:23 +00:00
|
|
|
Constants.YOUTUBE_SERVICE.getSearchQHFactory().fromQuery(q, Collections.singletonList(filter), null),
|
|
|
|
prevpage);
|
2021-02-24 09:52:29 +00:00
|
|
|
|
|
|
|
ObjectArrayList<SearchItem> items = new ObjectArrayList<>();
|
|
|
|
|
|
|
|
pages.getItems().forEach(item -> {
|
|
|
|
switch (item.getInfoType()) {
|
|
|
|
case STREAM:
|
|
|
|
StreamInfoItem stream = (StreamInfoItem) item;
|
|
|
|
items.add(new SearchStream(item.getName(), rewriteURL(item.getThumbnailUrl()),
|
2021-05-06 16:26:03 +00:00
|
|
|
item.getUrl().substring(23), stream.getTextualUploadDate(), stream.getUploaderName(),
|
2021-06-14 19:48:06 +00:00
|
|
|
optionalSubstring(stream.getUploaderUrl(), 23), stream.getViewCount(), stream.getDuration(),
|
2021-05-28 12:08:32 +00:00
|
|
|
stream.isUploaderVerified()));
|
2021-02-24 09:52:29 +00:00
|
|
|
break;
|
|
|
|
case CHANNEL:
|
2021-05-28 12:08:32 +00:00
|
|
|
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()));
|
2021-02-24 09:52:29 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Page nextpage = pages.getNextPage();
|
|
|
|
|
2021-06-14 19:44:23 +00:00
|
|
|
return Constants.mapper
|
|
|
|
.writeValueAsBytes(new SearchResults(items, Constants.mapper.writeValueAsString(nextpage)));
|
2020-12-09 13:30:42 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-04-03 15:08:32 +00:00
|
|
|
public static final byte[] commentsResponse(String videoId) throws Exception {
|
|
|
|
|
|
|
|
CommentsInfo info = commentsCache.get(videoId);
|
|
|
|
|
|
|
|
List<Comment> comments = new ObjectArrayList<>();
|
|
|
|
|
|
|
|
info.getRelatedItems().forEach(comment -> {
|
|
|
|
comments.add(new Comment(comment.getUploaderName(), rewriteURL(comment.getUploaderAvatarUrl()),
|
|
|
|
comment.getCommentId(), comment.getCommentText(), comment.getTextualUploadDate(),
|
2021-05-03 15:36:53 +00:00
|
|
|
comment.getUploaderUrl().substring(19), comment.getLikeCount(), comment.isHeartedByUploader(),
|
2021-04-03 15:08:32 +00:00
|
|
|
comment.isPinned(), comment.isUploaderVerified()));
|
|
|
|
});
|
|
|
|
|
|
|
|
String nextpage = null;
|
|
|
|
|
|
|
|
if (info.getNextPage() != null)
|
|
|
|
nextpage = info.getNextPage().getUrl();
|
|
|
|
|
|
|
|
CommentsPage commentsItem = new CommentsPage(comments, nextpage);
|
|
|
|
|
|
|
|
return Constants.mapper.writeValueAsBytes(commentsItem);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public static final byte[] commentsPageResponse(String videoId, String url) throws Exception {
|
|
|
|
|
|
|
|
CommentsInfo init = commentsCache.get(videoId);
|
|
|
|
|
|
|
|
InfoItemsPage<CommentsInfoItem> info = CommentsInfo.getMoreItems(init, new Page(url));
|
|
|
|
|
|
|
|
List<Comment> comments = new ObjectArrayList<>();
|
|
|
|
|
|
|
|
info.getItems().forEach(comment -> {
|
|
|
|
comments.add(new Comment(comment.getUploaderName(), rewriteURL(comment.getUploaderAvatarUrl()),
|
|
|
|
comment.getCommentId(), comment.getCommentText(), comment.getTextualUploadDate(),
|
2021-05-03 15:36:53 +00:00
|
|
|
comment.getUploaderUrl().substring(19), comment.getLikeCount(), comment.isHeartedByUploader(),
|
2021-04-03 15:08:32 +00:00
|
|
|
comment.isPinned(), comment.isUploaderVerified()));
|
|
|
|
});
|
|
|
|
|
|
|
|
String nextpage = null;
|
|
|
|
|
|
|
|
if (info.getNextPage() != null)
|
|
|
|
nextpage = info.getNextPage().getUrl();
|
|
|
|
|
|
|
|
CommentsPage commentsItem = new CommentsPage(comments, nextpage);
|
|
|
|
|
|
|
|
return Constants.mapper.writeValueAsBytes(commentsItem);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-01-12 08:15:09 +00:00
|
|
|
public static final byte[] registerResponse(String user, String pass) throws IOException {
|
2021-01-04 05:47:28 +00:00
|
|
|
|
2021-02-24 09:52:29 +00:00
|
|
|
return Constants.mapper.writeValueAsBytes(null);
|
2021-01-04 05:47:28 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-11-12 21:19:45 +00:00
|
|
|
private static final String getLBRYStreamURL(String videoId) throws IOException, InterruptedException {
|
|
|
|
|
2021-02-24 09:52:29 +00:00
|
|
|
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);
|
|
|
|
|
|
|
|
if (!lbryId.isEmpty())
|
2021-04-03 15:08:32 +00:00
|
|
|
new JSONObject(Constants.h2client.send(HttpRequest
|
|
|
|
.newBuilder(URI.create("https://api.lbry.tv/api/v1/proxy?m=get"))
|
|
|
|
.POST(BodyPublishers.ofString(
|
|
|
|
String.valueOf(new JSONObject().put("jsonrpc", "2.0").put("method", "get").put("params",
|
|
|
|
new JSONObject().put("uri", "lbry://" + lbryId).put("save_file", true)))))
|
|
|
|
.build(), BodyHandlers.ofString()).body()).getJSONObject("result").getString("streaming_url");
|
2021-02-24 09:52:29 +00:00
|
|
|
|
|
|
|
return null;
|
2020-11-12 21:19:45 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-06-14 19:44:23 +00:00
|
|
|
private static final String optionalSubstring(String s, int index) {
|
|
|
|
return s == null || s.isEmpty() ? null : s.substring(index);
|
|
|
|
}
|
|
|
|
|
2020-11-12 21:19:45 +00:00
|
|
|
private static String rewriteURL(final String old) {
|
|
|
|
|
2021-02-24 09:52:29 +00:00
|
|
|
if (Constants.debug)
|
|
|
|
return old;
|
2020-11-12 21:19:45 +00:00
|
|
|
|
2021-02-24 09:52:29 +00:00
|
|
|
if (old == null || old.isEmpty())
|
|
|
|
return null;
|
2020-12-09 13:29:12 +00:00
|
|
|
|
2021-02-24 09:52:29 +00:00
|
|
|
URL url = null;
|
2020-11-12 21:19:45 +00:00
|
|
|
|
2021-02-24 09:52:29 +00:00
|
|
|
try {
|
|
|
|
url = new URL(old);
|
|
|
|
} catch (MalformedURLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2020-11-12 21:19:45 +00:00
|
|
|
|
2021-02-24 09:52:29 +00:00
|
|
|
final String host = url.getHost();
|
2020-11-12 21:19:45 +00:00
|
|
|
|
2021-02-24 09:52:29 +00:00
|
|
|
String query = url.getQuery();
|
2020-11-12 21:19:45 +00:00
|
|
|
|
2021-02-24 09:52:29 +00:00
|
|
|
boolean hasQuery = query != null;
|
2020-11-12 21:19:45 +00:00
|
|
|
|
2021-02-24 09:52:29 +00:00
|
|
|
String path = url.getPath();
|
2020-11-12 21:19:45 +00:00
|
|
|
|
2021-02-24 09:52:29 +00:00
|
|
|
path = path.replace("-rj", "-rw");
|
2020-11-12 21:19:45 +00:00
|
|
|
|
2021-02-24 09:52:29 +00:00
|
|
|
return Constants.PROXY_PART + path + (hasQuery ? "?" + query + "&host=" : "?host=")
|
|
|
|
+ URLUtils.silentEncode(host);
|
2020-11-12 21:19:45 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|