Add Playlist RSS (#80)

* WIP Playlist rss.

* Complete adding support.
This commit is contained in:
FireMasterK 2021-07-05 00:27:18 +05:30 committed by GitHub
parent ac3c43b1ed
commit 5a8f5ec8d5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 0 deletions

View file

@ -122,6 +122,13 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher {
} catch (Exception e) {
return getErrorResponse(e);
}
})).map("/rss/playlists/:playlistId", AsyncServlet.ofBlocking(executor, request -> {
try {
return getJsonResponse(ResponseHelper.playlistRSSResponse(request.getPathParameter("playlistId")),
"public, s-maxage=600");
} catch (Exception e) {
return getErrorResponse(e);
}
})).map("/suggestions", AsyncServlet.ofBlocking(executor, request -> {
try {
return getJsonResponse(ResponseHelper.suggestionsResponse(request.getQueryParameter("query")),

View file

@ -7,6 +7,7 @@ import java.net.URL;
import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse.BodyHandlers;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;
@ -36,6 +37,12 @@ import org.schabi.newpipe.extractor.stream.StreamInfoItem;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
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;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import me.kavin.piped.consts.Constants;
@ -284,6 +291,35 @@ public class ResponseHelper {
}
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);
}
public static final byte[] suggestionsResponse(String query)
throws JsonProcessingException, IOException, ExtractionException {