Apply fixes from Error Prone via https://lift.sonatype.com

This commit is contained in:
Sonatype Lift Bot 2023-01-30 17:40:25 +00:00
parent 7878693bab
commit 8f5208bf34
22 changed files with 198 additions and 195 deletions

View file

@ -1,7 +1,18 @@
package me.kavin.piped.server.handlers;
import static me.kavin.piped.consts.Constants.YOUTUBE_SERVICE;
import static me.kavin.piped.consts.Constants.mapper;
import static me.kavin.piped.utils.CollectionUtils.collectRelatedItems;
import static me.kavin.piped.utils.URLUtils.rewriteURL;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.errorprone.annotations.Var;
import io.sentry.Sentry;
import java.io.IOException;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import me.kavin.piped.consts.Constants;
import me.kavin.piped.utils.*;
import me.kavin.piped.utils.obj.*;
@ -20,25 +31,14 @@ import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
import java.io.IOException;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import static me.kavin.piped.consts.Constants.YOUTUBE_SERVICE;
import static me.kavin.piped.consts.Constants.mapper;
import static me.kavin.piped.utils.CollectionUtils.collectRelatedItems;
import static me.kavin.piped.utils.URLUtils.rewriteURL;
public class ChannelHandlers {
public static byte[] channelResponse(String channelPath) throws Exception {
Sentry.setExtra("channelPath", channelPath);
final ChannelInfo info = ChannelInfo.getInfo("https://youtube.com/" + channelPath);
ChannelInfo info = ChannelInfo.getInfo("https://youtube.com/" + channelPath);
final List<ContentItem> relatedStreams = collectRelatedItems(info.getRelatedItems());
List<ContentItem> relatedStreams = collectRelatedItems(info.getRelatedItems());
Multithreading.runAsync(() -> info.getRelatedItems().forEach(infoItem -> {
if (
@ -117,7 +117,7 @@ public class ChannelHandlers {
}
});
String nextpage = null;
@Var String nextpage = null;
if (info.hasNextPage()) {
Page page = info.getNextPage();
nextpage = mapper.writeValueAsString(page);
@ -133,7 +133,7 @@ public class ChannelHandlers {
}
}).toList();
final Channel channel = new Channel(info.getId(), info.getName(), rewriteURL(info.getAvatarUrl()),
var channel = new Channel(info.getId(), info.getName(), rewriteURL(info.getAvatarUrl()),
rewriteURL(info.getBannerUrl()), info.getDescription(), info.getSubscriberCount(), info.isVerified(),
nextpage, relatedStreams, tabs);
@ -155,15 +155,15 @@ public class ChannelHandlers {
ListExtractor.InfoItemsPage<StreamInfoItem> info = ChannelInfo.getMoreItems(YOUTUBE_SERVICE,
"https://youtube.com/channel/" + channelId, prevpage);
final List<ContentItem> relatedStreams = collectRelatedItems(info.getItems());
List<ContentItem> relatedStreams = collectRelatedItems(info.getItems());
String nextpage = null;
@Var String nextpage = null;
if (info.hasNextPage()) {
Page page = info.getNextPage();
nextpage = mapper.writeValueAsString(page);
}
final StreamsPage streamspage = new StreamsPage(nextpage, relatedStreams);
var streamspage = new StreamsPage(nextpage, relatedStreams);
return mapper.writeValueAsBytes(streamspage);
@ -181,7 +181,7 @@ public class ChannelHandlers {
List<ContentItem> items = collectRelatedItems(info.getRelatedItems());
String nextpage = null;
@Var String nextpage = null;
if (info.hasNextPage()) {
Page page = info.getNextPage();
nextpage = mapper.writeValueAsString(page);
@ -204,7 +204,7 @@ public class ChannelHandlers {
var info = ChannelTabInfo.getMoreItems(YOUTUBE_SERVICE, tabHandler, prevPage);
String nextpage = null;
@Var String nextpage = null;
if (info.hasNextPage()) {
Page page = info.getNextPage();
nextpage = mapper.writeValueAsString(page);

View file

@ -1,5 +1,13 @@
package me.kavin.piped.server.handlers;
import static java.nio.charset.StandardCharsets.UTF_8;
import static me.kavin.piped.consts.Constants.YOUTUBE_SERVICE;
import static me.kavin.piped.consts.Constants.mapper;
import static me.kavin.piped.utils.CollectionUtils.collectRelatedItems;
import static me.kavin.piped.utils.URLUtils.rewriteURL;
import static me.kavin.piped.utils.URLUtils.substringYouTube;
import com.google.errorprone.annotations.Var;
import com.rometools.rome.feed.synd.SyndEntry;
import com.rometools.rome.feed.synd.SyndEntryImpl;
import com.rometools.rome.feed.synd.SyndFeed;
@ -8,6 +16,9 @@ import com.rometools.rome.io.FeedException;
import com.rometools.rome.io.SyndFeedOutput;
import io.sentry.Sentry;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import me.kavin.piped.consts.Constants;
import me.kavin.piped.server.handlers.auth.AuthPlaylistHandlers;
import me.kavin.piped.utils.ExceptionHandler;
@ -22,17 +33,6 @@ import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.playlist.PlaylistInfo;
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import static java.nio.charset.StandardCharsets.UTF_8;
import static me.kavin.piped.consts.Constants.YOUTUBE_SERVICE;
import static me.kavin.piped.consts.Constants.mapper;
import static me.kavin.piped.utils.CollectionUtils.collectRelatedItems;
import static me.kavin.piped.utils.URLUtils.rewriteURL;
import static me.kavin.piped.utils.URLUtils.substringYouTube;
public class PlaylistHandlers {
public static byte[] playlistResponse(String playlistId) throws Exception {
@ -50,17 +50,17 @@ public class PlaylistHandlers {
Sentry.setExtra("playlistId", playlistId);
final PlaylistInfo info = PlaylistInfo.getInfo("https://www.youtube.com/playlist?list=" + playlistId);
PlaylistInfo info = PlaylistInfo.getInfo("https://www.youtube.com/playlist?list=" + playlistId);
final List<ContentItem> relatedStreams = collectRelatedItems(info.getRelatedItems());
List<ContentItem> relatedStreams = collectRelatedItems(info.getRelatedItems());
String nextpage = null;
@Var String nextpage = null;
if (info.hasNextPage()) {
Page page = info.getNextPage();
nextpage = mapper.writeValueAsString(page);
}
final Playlist playlist = new Playlist(info.getName(), rewriteURL(info.getThumbnailUrl()),
var playlist = new Playlist(info.getName(), rewriteURL(info.getThumbnailUrl()),
rewriteURL(info.getBannerUrl()), nextpage,
info.getUploaderName().isEmpty() ? null : info.getUploaderName(),
substringYouTube(info.getUploaderUrl()), rewriteURL(info.getUploaderAvatarUrl()),
@ -84,15 +84,15 @@ public class PlaylistHandlers {
ListExtractor.InfoItemsPage<StreamInfoItem> info = PlaylistInfo.getMoreItems(YOUTUBE_SERVICE,
"https://www.youtube.com/playlist?list=" + playlistId, prevpage);
final List<ContentItem> relatedStreams = collectRelatedItems(info.getItems());
List<ContentItem> relatedStreams = collectRelatedItems(info.getItems());
String nextpage = null;
@Var String nextpage = null;
if (info.hasNextPage()) {
Page page = info.getNextPage();
nextpage = mapper.writeValueAsString(page);
}
final StreamsPage streamspage = new StreamsPage(nextpage, relatedStreams);
var streamspage = new StreamsPage(nextpage, relatedStreams);
return mapper.writeValueAsBytes(streamspage);
@ -112,9 +112,9 @@ public class PlaylistHandlers {
private static byte[] playlistYouTubeRSSResponse(String playlistId)
throws IOException, ExtractionException, FeedException {
final PlaylistInfo info = PlaylistInfo.getInfo("https://www.youtube.com/playlist?list=" + playlistId);
PlaylistInfo info = PlaylistInfo.getInfo("https://www.youtube.com/playlist?list=" + playlistId);
final List<SyndEntry> entries = new ObjectArrayList<>();
List<SyndEntry> entries = new ObjectArrayList<>();
SyndFeed feed = new SyndFeedImpl();
feed.setFeedType("rss_2.0");

View file

@ -53,7 +53,7 @@ public class SearchHandlers {
Sentry.setExtra("query", q);
final SearchInfo info = SearchInfo.getInfo(YOUTUBE_SERVICE,
SearchInfo info = SearchInfo.getInfo(YOUTUBE_SERVICE,
YOUTUBE_SERVICE.getSearchQHFactory().fromQuery(q, Collections.singletonList(filter), null));
List<ContentItem> items = collectRelatedItems(info.getRelatedItems());

View file

@ -1,11 +1,28 @@
package me.kavin.piped.server.handlers;
import static java.nio.charset.StandardCharsets.UTF_8;
import static me.kavin.piped.consts.Constants.YOUTUBE_SERVICE;
import static me.kavin.piped.consts.Constants.mapper;
import static me.kavin.piped.utils.URLUtils.rewriteURL;
import static me.kavin.piped.utils.URLUtils.substringYouTube;
import static org.schabi.newpipe.extractor.NewPipe.getPreferredContentCountry;
import static org.schabi.newpipe.extractor.NewPipe.getPreferredLocalization;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getJsonPostResponse;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.prepareDesktopJsonBuilder;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.errorprone.annotations.Var;
import com.grack.nanojson.JsonObject;
import com.grack.nanojson.JsonWriter;
import io.sentry.ITransaction;
import io.sentry.Sentry;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import me.kavin.piped.consts.Constants;
import me.kavin.piped.utils.*;
import me.kavin.piped.utils.obj.*;
@ -26,29 +43,12 @@ import org.schabi.newpipe.extractor.stream.Description;
import org.schabi.newpipe.extractor.stream.StreamInfo;
import org.schabi.newpipe.extractor.utils.JsonUtils;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import static java.nio.charset.StandardCharsets.UTF_8;
import static me.kavin.piped.consts.Constants.YOUTUBE_SERVICE;
import static me.kavin.piped.consts.Constants.mapper;
import static me.kavin.piped.utils.URLUtils.rewriteURL;
import static me.kavin.piped.utils.URLUtils.substringYouTube;
import static org.schabi.newpipe.extractor.NewPipe.getPreferredContentCountry;
import static org.schabi.newpipe.extractor.NewPipe.getPreferredLocalization;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getJsonPostResponse;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.prepareDesktopJsonBuilder;
public class StreamHandlers {
public static byte[] streamsResponse(String videoId) throws Exception {
Sentry.setExtra("videoId", videoId);
final var futureStream = Multithreading.supplyAsync(() -> {
var futureStream = Multithreading.supplyAsync(() -> {
Sentry.setExtra("videoId", videoId);
ITransaction transaction = Sentry.startTransaction("StreamInfo fetch", "fetch");
try {
@ -65,7 +65,7 @@ public class StreamHandlers {
return null;
});
final var futureLbryId = Multithreading.supplyAsync(() -> {
var futureLbryId = Multithreading.supplyAsync(() -> {
Sentry.setExtra("videoId", videoId);
try {
return LbryHelper.getLBRYId(videoId);
@ -75,7 +75,7 @@ public class StreamHandlers {
return null;
});
final var futureLBRY = Multithreading.supplyAsync(() -> {
var futureLBRY = Multithreading.supplyAsync(() -> {
Sentry.setExtra("videoId", videoId);
ITransaction transaction = Sentry.startTransaction("LBRY Stream fetch", "fetch");
try {
@ -94,7 +94,7 @@ public class StreamHandlers {
return null;
});
final var futureDislikeRating = Multithreading.supplyAsync(() -> {
var futureDislikeRating = Multithreading.supplyAsync(() -> {
Sentry.setExtra("videoId", videoId);
ITransaction transaction = Sentry.startTransaction("Dislike Rating", "fetch");
try {
@ -107,8 +107,8 @@ public class StreamHandlers {
return null;
});
StreamInfo info = null;
Throwable exception = null;
@Var StreamInfo info = null;
@Var Throwable exception = null;
try {
info = futureStream.get(10, TimeUnit.SECONDS);
@ -174,7 +174,7 @@ public class StreamHandlers {
streams.thumbnailUrl = rewriteURL(streams.thumbnailUrl);
streams.uploaderAvatar = rewriteURL(streams.uploaderAvatar);
String lbryId;
@Var String lbryId;
try {
lbryId = futureLbryId.get(2, TimeUnit.SECONDS);
@ -186,7 +186,7 @@ public class StreamHandlers {
streams.lbryId = lbryId;
}
String lbryURL;
@Var String lbryURL;
try {
lbryURL = futureLBRY.get(3, TimeUnit.SECONDS);
@ -199,7 +199,7 @@ public class StreamHandlers {
// Attempt to get dislikes calculating with the RYD API rating
if (streams.dislikes < 0 && streams.likes >= 0) {
double rating;
@Var double rating;
try {
rating = futureDislikeRating.get(3, TimeUnit.SECONDS);
} catch (Exception e) {
@ -224,7 +224,7 @@ public class StreamHandlers {
Streams streams = CollectionUtils.collectStreamInfo(info);
String lbryURL = null;
@Var String lbryURL = null;
try {
lbryURL = futureLBRY.get(3, TimeUnit.SECONDS);
@ -254,7 +254,7 @@ public class StreamHandlers {
});
}
String lbryId;
@Var String lbryId;
try {
lbryId = futureLbryId.get(2, TimeUnit.SECONDS);
@ -266,7 +266,7 @@ public class StreamHandlers {
// Attempt to get dislikes calculating with the RYD API rating
if (streams.dislikes < 0 && streams.likes >= 0) {
double rating;
@Var double rating;
try {
rating = futureDislikeRating.get(3, TimeUnit.SECONDS);
} catch (Exception e) {
@ -284,16 +284,16 @@ public class StreamHandlers {
public static byte[] resolveClipId(String clipId) throws Exception {
final byte[] body = JsonWriter.string(prepareDesktopJsonBuilder(
byte[] body = JsonWriter.string(prepareDesktopJsonBuilder(
getPreferredLocalization(), getPreferredContentCountry())
.value("url", "https://www.youtube.com/clip/" + clipId)
.done())
.getBytes(UTF_8);
final JsonObject jsonResponse = getJsonPostResponse("navigation/resolve_url",
JsonObject jsonResponse = getJsonPostResponse("navigation/resolve_url",
body, getPreferredLocalization());
final String videoId = JsonUtils.getString(jsonResponse, "endpoint.watchEndpoint.videoId");
String videoId = JsonUtils.getString(jsonResponse, "endpoint.watchEndpoint.videoId");
return mapper.writeValueAsBytes(new VideoResolvedResponse(videoId));
}
@ -308,7 +308,7 @@ public class StreamHandlers {
info.getRelatedItems().forEach(comment -> {
try {
String repliespage = null;
@Var String repliespage = null;
if (comment.getReplies() != null)
repliespage = mapper.writeValueAsString(comment.getReplies());
@ -321,13 +321,13 @@ public class StreamHandlers {
}
});
String nextpage = null;
@Var String nextpage = null;
if (info.hasNextPage()) {
Page page = info.getNextPage();
nextpage = mapper.writeValueAsString(page);
}
CommentsPage commentsItem = new CommentsPage(comments, nextpage, info.isCommentsDisabled());
var commentsItem = new CommentsPage(comments, nextpage, info.isCommentsDisabled());
return mapper.writeValueAsBytes(commentsItem);
@ -346,7 +346,7 @@ public class StreamHandlers {
info.getItems().forEach(comment -> {
try {
String repliespage = null;
@Var String repliespage = null;
if (comment.getReplies() != null)
repliespage = mapper.writeValueAsString(comment.getReplies());
@ -359,13 +359,13 @@ public class StreamHandlers {
}
});
String nextpage = null;
@Var String nextpage = null;
if (info.hasNextPage()) {
Page page = info.getNextPage();
nextpage = mapper.writeValueAsString(page);
}
CommentsPage commentsItem = new CommentsPage(comments, nextpage, false);
var commentsItem = new CommentsPage(comments, nextpage, false);
return mapper.writeValueAsBytes(commentsItem);

View file

@ -29,7 +29,7 @@ public class TrendingHandlers {
extractor.fetchPage();
KioskInfo info = KioskInfo.getInfo(extractor);
final List<ContentItem> relatedStreams = collectRelatedItems(info.getRelatedItems());
List<ContentItem> relatedStreams = collectRelatedItems(info.getRelatedItems());
return mapper.writeValueAsBytes(relatedStreams);
}

View file

@ -1,6 +1,13 @@
package me.kavin.piped.server.handlers.auth;
import static java.nio.charset.StandardCharsets.UTF_8;
import static me.kavin.piped.consts.Constants.YOUTUBE_SERVICE;
import static me.kavin.piped.consts.Constants.mapper;
import static me.kavin.piped.utils.URLUtils.rewriteURL;
import static me.kavin.piped.utils.URLUtils.substringYouTube;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.errorprone.annotations.Var;
import com.rometools.rome.feed.synd.SyndEntry;
import com.rometools.rome.feed.synd.SyndEntryImpl;
import com.rometools.rome.feed.synd.SyndFeed;
@ -10,6 +17,9 @@ import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import jakarta.persistence.criteria.JoinType;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
import me.kavin.piped.consts.Constants;
import me.kavin.piped.utils.*;
import me.kavin.piped.utils.obj.ContentItem;
@ -29,16 +39,6 @@ import org.schabi.newpipe.extractor.playlist.PlaylistInfo;
import org.schabi.newpipe.extractor.stream.StreamInfo;
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
import static java.nio.charset.StandardCharsets.UTF_8;
import static me.kavin.piped.consts.Constants.YOUTUBE_SERVICE;
import static me.kavin.piped.consts.Constants.mapper;
import static me.kavin.piped.utils.URLUtils.rewriteURL;
import static me.kavin.piped.utils.URLUtils.substringYouTube;
public class AuthPlaylistHandlers {
public static byte[] playlistPipedResponse(String playlistId) throws Exception {
@ -56,7 +56,7 @@ public class AuthPlaylistHandlers {
return mapper.writeValueAsBytes(mapper.createObjectNode()
.put("error", "Playlist not found"));
final List<ContentItem> relatedStreams = new ObjectArrayList<>();
List<ContentItem> relatedStreams = new ObjectArrayList<>();
var videos = playlistVideosCompletableFuture.get();
@ -67,7 +67,7 @@ public class AuthPlaylistHandlers {
video.getDuration(), -1, -1, channel.isVerified(), false));
}
final Playlist playlist = new Playlist(pl.getName(), rewriteURL(pl.getThumbnail()), null, null, pl.getOwner().getUsername(),
var playlist = new Playlist(pl.getName(), rewriteURL(pl.getThumbnail()), null, null, pl.getOwner().getUsername(),
null, null, videos.size(), relatedStreams);
return mapper.writeValueAsBytes(playlist);
@ -86,7 +86,7 @@ public class AuthPlaylistHandlers {
var pl = playlistCompletableFuture.get();
final List<SyndEntry> entries = new ObjectArrayList<>();
List<SyndEntry> entries = new ObjectArrayList<>();
SyndFeed feed = new SyndFeedImpl();
feed.setFeedType("rss_2.0");
@ -199,7 +199,7 @@ public class AuthPlaylistHandlers {
return mapper.writeValueAsBytes(new AcceptedResponse());
}
public static byte[] addToPlaylistResponse(String session, String playlistId, List<String> videoIds) throws IOException, ExtractionException {
public static byte[] addToPlaylistResponse(String session, String playlistId, @Var List<String> videoIds) throws IOException, ExtractionException {
videoIds = videoIds.stream()
.filter(StringUtils::isNotBlank)
@ -232,12 +232,12 @@ public class AuthPlaylistHandlers {
var videos = playlist.getVideos();
boolean added = false;
@Var boolean added = false;
for (String videoId : videoIds) {
if (StringUtils.isEmpty(videoId)) continue;
var video = playlistVideos.stream().filter(v -> v.getId().equals(videoId))
@Var var video = playlistVideos.stream().filter(v -> v.getId().equals(videoId))
.findFirst()
.orElse(null);
@ -247,7 +247,7 @@ public class AuthPlaylistHandlers {
String channelId = StringUtils.substringAfter(info.getUploaderUrl(), "/channel/");
var channel = DatabaseHelper.getChannelFromId(s, channelId);
@Var var channel = DatabaseHelper.getChannelFromId(s, channelId);
if (channel == null) {
channel = DatabaseHelper.saveChannel(channelId);
@ -358,7 +358,7 @@ public class AuthPlaylistHandlers {
if (user == null)
ExceptionHandler.throwErrorResponse(new AuthenticationFailureResponse());
final String url = "https://www.youtube.com/playlist?list=" + playlistId;
String url = "https://www.youtube.com/playlist?list=" + playlistId;
PlaylistInfo info = PlaylistInfo.getInfo(url);
@ -366,7 +366,7 @@ public class AuthPlaylistHandlers {
List<StreamInfoItem> videos = new ObjectArrayList<>(info.getRelatedItems());
Page nextpage = info.getNextPage();
@Var Page nextpage = info.getNextPage();
while (nextpage != null) {
var page = PlaylistInfo.getMoreItems(YOUTUBE_SERVICE, url, nextpage);

View file

@ -173,7 +173,7 @@ public class FeedHandlers {
)
.orderBy(cb.desc(root.get("uploaded")));
final List<SyndEntry> entries = s.createQuery(criteria)
List<SyndEntry> entries = s.createQuery(criteria)
.setTimeout(20)
.setMaxResults(100)
.stream()
@ -270,7 +270,7 @@ public class FeedHandlers {
feed.setUri(Constants.FRONTEND_URL + "/feed");
feed.setPublishedDate(new Date());
final List<SyndEntry> entries = new ObjectArrayList<>();
List<SyndEntry> entries = new ObjectArrayList<>();
for (Video video : videos) {
var channel = video.getChannel();
@ -342,7 +342,7 @@ public class FeedHandlers {
{
var query = cb.createQuery();
var root = query.from(me.kavin.piped.utils.obj.db.Channel.class);
var root = query.from(Channel.class);
query.select(root.get("id"))
.where(root.get("id").in(channelIds));

View file

@ -1,9 +1,16 @@
package me.kavin.piped.server.handlers.auth;
import static me.kavin.piped.consts.Constants.mapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.base.Splitter;
import com.google.errorprone.annotations.Var;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Root;
import java.io.IOException;
import java.util.Set;
import java.util.UUID;
import me.kavin.piped.consts.Constants;
import me.kavin.piped.utils.DatabaseHelper;
import me.kavin.piped.utils.DatabaseSessionFactory;
@ -18,17 +25,11 @@ import org.hibernate.StatelessSession;
import org.springframework.security.crypto.argon2.Argon2PasswordEncoder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import java.io.IOException;
import java.util.Set;
import java.util.UUID;
import static me.kavin.piped.consts.Constants.mapper;
public class UserHandlers {
private static final Argon2PasswordEncoder argon2PasswordEncoder = Argon2PasswordEncoder.defaultsForSpringSecurity_v5_8();
private static final BCryptPasswordEncoder bcryptPasswordEncoder = new BCryptPasswordEncoder();
public static byte[] registerResponse(String user, String pass) throws IOException {
public static byte[] registerResponse(@Var String user, String pass) throws IOException {
if (Constants.DISABLE_REGISTRATION)
ExceptionHandler.throwErrorResponse(new DisabledRegistrationResponse());
@ -55,15 +56,14 @@ public class UserHandlers {
String sha1Hash = DigestUtils.sha1Hex(pass).toUpperCase();
String prefix = sha1Hash.substring(0, 5);
String suffix = sha1Hash.substring(5);
String[] entries = RequestUtils
.sendGet("https://api.pwnedpasswords.com/range/" + prefix, "github.com/TeamPiped/Piped-Backend")
.split("\n");
Iterable<String> entries = Splitter.on('\n').split(RequestUtils
.sendGet("https://api.pwnedpasswords.com/range/" + prefix, "github.com/TeamPiped/Piped-Backend"));
for (String entry : entries)
if (StringUtils.substringBefore(entry, ":").equals(suffix))
ExceptionHandler.throwErrorResponse(new CompromisedPasswordResponse());
}
User newuser = new User(user, argon2PasswordEncoder.encode(pass), Set.of());
var newuser = new User(user, argon2PasswordEncoder.encode(pass), Set.of());
var tr = s.beginTransaction();
s.persist(newuser);
@ -80,7 +80,7 @@ public class UserHandlers {
bcryptPasswordEncoder.matches(pass, hash);
}
public static byte[] loginResponse(String user, String pass)
public static byte[] loginResponse(@Var String user, String pass)
throws IOException {
if (user == null || pass == null)

View file

@ -1,6 +1,15 @@
package me.kavin.piped.utils;
import static me.kavin.piped.utils.URLUtils.rewriteURL;
import com.google.errorprone.annotations.Var;
import com.rometools.rome.feed.synd.*;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import me.kavin.piped.consts.Constants;
import me.kavin.piped.utils.obj.db.Channel;
import me.kavin.piped.utils.obj.db.Video;
@ -9,15 +18,6 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringEscapeUtils;
import org.hibernate.StatelessSession;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import static me.kavin.piped.utils.URLUtils.rewriteURL;
public class ChannelHelpers {
public static boolean isValidId(String id) {
@ -26,7 +26,7 @@ public class ChannelHelpers {
public static void updateChannel(StatelessSession s, Channel channel, String name, String avatarUrl, boolean uploaderVerified) {
boolean changed = false;
@Var boolean changed = false;
if (name != null && !name.equals(channel.getUploader())) {
channel.setUploader(name);

View file

@ -16,13 +16,13 @@ import static me.kavin.piped.utils.URLUtils.*;
public class CollectionUtils {
public static Streams collectStreamInfo(StreamInfo info) {
final List<Subtitle> subtitles = new ObjectArrayList<>();
final List<ChapterSegment> chapters = new ObjectArrayList<>();
List<Subtitle> subtitles = new ObjectArrayList<>();
List<ChapterSegment> chapters = new ObjectArrayList<>();
info.getStreamSegments().forEach(segment -> chapters.add(new ChapterSegment(segment.getTitle(), rewriteURL(segment.getPreviewUrl()),
segment.getStartTimeSeconds())));
final List<PreviewFrames> previewFrames = new ObjectArrayList<>();
List<PreviewFrames> previewFrames = new ObjectArrayList<>();
info.getPreviewFrames().forEach(frame -> previewFrames.add(new PreviewFrames(frame.getUrls().stream().map(URLUtils::rewriteURL).toList(), frame.getFrameWidth(),
frame.getFrameHeight(), frame.getTotalCount(), frame.getDurationPerFrame(), frame.getFramesPerPageX(),
@ -33,8 +33,8 @@ public class CollectionUtils {
subtitle.getFormat().getMimeType(), subtitle.getDisplayLanguageName(),
subtitle.getLanguageTag(), subtitle.isAutoGenerated())));
final List<PipedStream> videoStreams = new ObjectArrayList<>();
final List<PipedStream> audioStreams = new ObjectArrayList<>();
List<PipedStream> videoStreams = new ObjectArrayList<>();
List<PipedStream> audioStreams = new ObjectArrayList<>();
boolean livestream = info.getStreamType() == StreamType.LIVE_STREAM;
@ -55,7 +55,7 @@ public class CollectionUtils {
stream.getInitEnd(), stream.getIndexStart(), stream.getIndexEnd(), stream.getCodec(), stream.getAudioTrackId(), stream.getAudioTrackName())));
}
final List<ContentItem> relatedStreams = collectRelatedItems(info.getRelatedItems());
List<ContentItem> relatedStreams = collectRelatedItems(info.getRelatedItems());
return new Streams(info.getName(), info.getDescription().getContent(),
info.getTextualUploadDate(), info.getUploaderName(), substringYouTube(info.getUploaderUrl()),
@ -85,7 +85,7 @@ public class CollectionUtils {
private static StreamItem collectRelatedStream(Object o) {
StreamInfoItem item = (StreamInfoItem) o;
var item = (StreamInfoItem) o;
return new StreamItem(substringYouTube(item.getUrl()), item.getName(),
rewriteURL(item.getThumbnailUrl()),
@ -99,7 +99,7 @@ public class CollectionUtils {
private static PlaylistItem collectRelatedPlaylist(Object o) {
PlaylistInfoItem item = (PlaylistInfoItem) o;
var item = (PlaylistInfoItem) o;
return new PlaylistItem(substringYouTube(item.getUrl()), item.getName(),
rewriteURL(item.getThumbnailUrl()),
@ -110,7 +110,7 @@ public class CollectionUtils {
private static ChannelItem collectRelatedChannel(Object o) {
ChannelInfoItem item = (ChannelInfoItem) o;
var item = (ChannelInfoItem) o;
return new ChannelItem(substringYouTube(item.getUrl()), item.getName(),
rewriteURL(item.getThumbnailUrl()),

View file

@ -1,23 +1,23 @@
package me.kavin.piped.utils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.errorprone.annotations.Var;
import io.sentry.Sentry;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutionException;
import me.kavin.piped.consts.Constants;
import me.kavin.piped.utils.resp.InvalidRequestResponse;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.schabi.newpipe.extractor.exceptions.ContentNotAvailableException;
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutionException;
public class ExceptionHandler {
public static Exception handle(Exception e) {
return handle(e, null);
}
public static Exception handle(Exception e, String path) {
public static Exception handle(@Var Exception e, String path) {
if (e.getCause() != null && (e instanceof ExecutionException || e instanceof CompletionException))
e = (Exception) e.getCause();

View file

@ -1,5 +1,11 @@
package me.kavin.piped.utils;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.errorprone.annotations.Var;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import me.kavin.piped.consts.Constants;
import me.kavin.piped.utils.obj.db.PubSub;
import okhttp3.FormBody;
@ -7,17 +13,13 @@ import okio.Buffer;
import org.hibernate.StatelessSession;
import rocks.kavin.reqwest4j.ReqwestUtils;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;
public class PubSubHelper {
public static void subscribePubSub(String channelId) throws IOException {
if (!ChannelHelpers.isValidId(channelId))
return;
PubSub pubsub = DatabaseHelper.getPubSubFromId(channelId);
@Var PubSub pubsub = DatabaseHelper.getPubSubFromId(channelId);
if (pubsub == null || System.currentTimeMillis() - pubsub.getSubbedAt() > TimeUnit.DAYS.toMillis(4)) {
@ -47,13 +49,13 @@ public class PubSubHelper {
var resp = ReqwestUtils.fetch(callback, "POST", buffer.readByteArray(), Map.of());
if (resp.status() != 202)
System.out.println("Failed to subscribe: " + resp.status() + "\n" + new String(resp.body()));
System.out.println("Failed to subscribe: " + resp.status() + "\n" + new String(resp.body(), UTF_8));
}
}
public static void updatePubSub(String channelId) {
var pubsub = DatabaseHelper.getPubSubFromId(channelId);
@Var var pubsub = DatabaseHelper.getPubSubFromId(channelId);
try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) {
s.beginTransaction();
if (pubsub == null) {

View file

@ -1,17 +1,17 @@
package me.kavin.piped.utils;
import static java.nio.charset.StandardCharsets.UTF_8;
import static me.kavin.piped.consts.Constants.mapper;
import com.fasterxml.jackson.databind.JsonNode;
import java.io.IOException;
import java.util.Map;
import me.kavin.piped.consts.Constants;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import rocks.kavin.reqwest4j.ReqwestUtils;
import rocks.kavin.reqwest4j.Response;
import java.io.IOException;
import java.util.Map;
import static me.kavin.piped.consts.Constants.mapper;
public class RequestUtils {
public static Response sendGetRaw(String url) throws IOException {
@ -21,14 +21,14 @@ public class RequestUtils {
public static String sendGet(String url) throws IOException {
return new String(
ReqwestUtils.fetch(url, "GET", null, Map.of())
.body()
.body(), UTF_8
);
}
public static String sendGet(String url, String ua) throws IOException {
return new String(
ReqwestUtils.fetch(url, "GET", null, Map.of("User-Agent", ua))
.body()
.body(), UTF_8
);
}

View file

@ -1,24 +1,25 @@
package me.kavin.piped.utils;
import com.grack.nanojson.JsonObject;
import com.grack.nanojson.JsonWriter;
import me.kavin.piped.consts.Constants;
import me.kavin.piped.utils.obj.db.Video;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.StatelessSession;
import org.schabi.newpipe.extractor.stream.StreamInfo;
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
import java.util.concurrent.TimeUnit;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.schabi.newpipe.extractor.NewPipe.getPreferredContentCountry;
import static org.schabi.newpipe.extractor.NewPipe.getPreferredLocalization;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getJsonPostResponse;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.prepareDesktopJsonBuilder;
import com.google.errorprone.annotations.Var;
import com.grack.nanojson.JsonObject;
import com.grack.nanojson.JsonWriter;
import java.util.concurrent.TimeUnit;
import me.kavin.piped.consts.Constants;
import me.kavin.piped.utils.obj.db.Channel;
import me.kavin.piped.utils.obj.db.Video;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.StatelessSession;
import org.schabi.newpipe.extractor.stream.StreamInfo;
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
public class VideoHelpers {
public static void handleNewVideo(String url, long time, me.kavin.piped.utils.obj.db.Channel channel) {
public static void handleNewVideo(String url, long time, Channel channel) {
try {
handleNewVideo(StreamInfo.getInfo(url), time, channel);
} catch (Exception e) {
@ -26,7 +27,7 @@ public class VideoHelpers {
}
}
public static void handleNewVideo(StreamInfo info, long time, me.kavin.piped.utils.obj.db.Channel channel) throws Exception {
public static void handleNewVideo(StreamInfo info, long time, @Var Channel channel) throws Exception {
if (channel == null)
channel = DatabaseHelper.getChannelFromId(
@ -43,7 +44,7 @@ public class VideoHelpers {
try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) {
if (!DatabaseHelper.doesVideoExist(s, info.getId())) {
Video video = new Video(info.getId(), info.getName(), info.getViewCount(), info.getDuration(),
var video = new Video(info.getId(), info.getName(), info.getViewCount(), info.getDuration(),
Math.max(infoTime, time), info.getThumbnailUrl(), info.isShortFormContent(), channel);
var tr = s.beginTransaction();
@ -65,13 +66,13 @@ public class VideoHelpers {
public static boolean isShort(String videoId) throws Exception {
final byte[] body = JsonWriter.string(prepareDesktopJsonBuilder(
byte[] body = JsonWriter.string(prepareDesktopJsonBuilder(
getPreferredLocalization(), getPreferredContentCountry())
.value("url", "https://www.youtube.com/shorts/" + videoId)
.done())
.getBytes(UTF_8);
final JsonObject jsonResponse = getJsonPostResponse("navigation/resolve_url",
JsonObject jsonResponse = getJsonPostResponse("navigation/resolve_url",
body, getPreferredLocalization());
return jsonResponse.getObject("endpoint").has("reelWatchEndpoint");

View file

@ -2,7 +2,7 @@ package me.kavin.piped.utils;
import lombok.AllArgsConstructor;
@AllArgsConstructor
SuppressWarnings
public class WaitingListener {
private final long maxWaitTime;

View file

@ -1,6 +1,14 @@
package me.kavin.piped.utils.matrix;
import static me.kavin.piped.consts.Constants.mapper;
import static me.kavin.piped.utils.obj.MatrixHelper.*;
import com.fasterxml.jackson.databind.JsonNode;
import com.google.errorprone.annotations.Var;
import java.io.IOException;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import me.kavin.piped.consts.Constants;
import me.kavin.piped.utils.*;
import me.kavin.piped.utils.obj.MatrixHelper;
@ -16,14 +24,6 @@ import okhttp3.RequestBody;
import org.hibernate.StatelessSession;
import org.schabi.newpipe.extractor.stream.StreamInfo;
import java.io.IOException;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import static me.kavin.piped.consts.Constants.mapper;
import static me.kavin.piped.utils.obj.MatrixHelper.*;
public class SyncRunner implements Runnable {
private final OkHttpClient client;
@ -45,7 +45,7 @@ public class SyncRunner implements Runnable {
public void run() {
try {
String user_id = null;
@Var String user_id = null;
if (!UNAUTHENTICATED) {
// whoami to get the user id
@ -62,7 +62,7 @@ public class SyncRunner implements Runnable {
// Join room and get the room id
System.out.println("Room ID: " + ROOM_ID);
String filter_id = null;
@Var String filter_id = null;
// We have to filter on client-side if unauthenticated
if (!UNAUTHENTICATED) {
@ -72,12 +72,12 @@ public class SyncRunner implements Runnable {
System.out.println("Filter ID: " + filter_id);
String next_batch = null;
@Var String next_batch = null;
//noinspection InfiniteLoopStatement
while (true) {
try {
String url;
@Var String url;
if (UNAUTHENTICATED) {
url = this.url + "/_matrix/client/v3/events?room_id=" + URLUtils.silentEncode(ROOM_ID);
@ -141,7 +141,7 @@ public class SyncRunner implements Runnable {
Streams streams = CollectionUtils.collectStreamInfo(info);
FederatedGeoBypassResponse bypassResponse = new FederatedGeoBypassResponse(bypassRequest.getVideoId(), Constants.YOUTUBE_COUNTRY, streams);
var bypassResponse = new FederatedGeoBypassResponse(bypassRequest.getVideoId(), Constants.YOUTUBE_COUNTRY, streams);
MatrixHelper.sendEvent("video.piped.stream.bypass.response", bypassResponse);

View file

@ -2,7 +2,7 @@ package me.kavin.piped.utils.obj;
import lombok.NoArgsConstructor;
@NoArgsConstructor
SuppressWarnings
public class ChannelItem extends ContentItem {
public final String type = "channel";

View file

@ -4,7 +4,7 @@ import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import lombok.NoArgsConstructor;
@NoArgsConstructor
SuppressWarnings
@JsonTypeInfo(use = JsonTypeInfo.Id.DEDUCTION, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(name = "stream", value = StreamItem.class),

View file

@ -2,7 +2,7 @@ package me.kavin.piped.utils.obj;
import lombok.NoArgsConstructor;
@NoArgsConstructor
SuppressWarnings
public class PlaylistItem extends ContentItem {
public final String type = "playlist";

View file

@ -5,8 +5,8 @@ import lombok.NoArgsConstructor;
import java.util.List;
@AllArgsConstructor
@NoArgsConstructor
SuppressWarnings
SuppressWarnings
public class PreviewFrames {
public List<String> urls;

View file

@ -6,10 +6,10 @@ import lombok.NoArgsConstructor;
import java.util.List;
@NoArgsConstructor
@AllArgsConstructor
SuppressWarnings
SuppressWarnings
@Getter
public class FederatedGeoBypassRequest {
private String videoId;
private List<String> allowedCountries;
private String SuppressWarnings
private List<String> SuppressWarnings
}

View file

@ -5,13 +5,13 @@ import lombok.NoArgsConstructor;
import me.kavin.piped.consts.Constants;
import me.kavin.piped.utils.obj.Streams;
@NoArgsConstructor
SuppressWarnings
@Getter
public class FederatedGeoBypassResponse {
private String videoId;
private String country;
private String videoProxyUrl;
private Streams data;
private String SuppressWarnings
private String SuppressWarnings
private String SuppressWarnings
private Streams SuppressWarnings
public FederatedGeoBypassResponse(String videoId, String country, Streams data) {
this.videoId = videoId;