Small Java 17 improvements. (#337)

This commit is contained in:
Kavin 2022-08-18 08:43:40 +05:30 committed by GitHub
parent cca5441402
commit adbd2b347f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 43 deletions

View File

@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
java: [ 11, 17 ]
java: [ 17 ]
steps:
- uses: actions/checkout@v3

View File

@ -57,5 +57,5 @@ jar {
group = 'me.kavin.piped'
version = '1.0'
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17

View File

@ -8,6 +8,7 @@ import me.kavin.piped.utils.obj.db.*;
import org.hibernate.SharedSessionContract;
import org.hibernate.StatelessSession;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
@ -55,7 +56,7 @@ public class DatabaseHelper {
}
}
public static List<Channel> getChannelsFromIds(SharedSessionContract s, List<String> id) {
public static List<Channel> getChannelsFromIds(SharedSessionContract s, Collection<String> id) {
CriteriaBuilder cb = s.getCriteriaBuilder();
CriteriaQuery<Channel> cr = cb.createQuery(Channel.class);
Root<Channel> root = cr.from(Channel.class);
@ -97,7 +98,7 @@ public class DatabaseHelper {
return s.createQuery(cr).uniqueResult();
}
public static List<PlaylistVideo> getPlaylistVideosFromIds(SharedSessionContract s, List<String> id) {
public static List<PlaylistVideo> getPlaylistVideosFromIds(SharedSessionContract s, Collection<String> id) {
CriteriaBuilder cb = s.getCriteriaBuilder();
CriteriaQuery<PlaylistVideo> cr = cb.createQuery(PlaylistVideo.class);
Root<PlaylistVideo> root = cr.from(PlaylistVideo.class);

View File

@ -116,7 +116,7 @@ public class ResponseHelper {
segment.getStartTimeSeconds())));
info.getSubtitles()
.forEach(subtitle -> subtitles.add(new Subtitle(rewriteURL(subtitle.getUrl()),
.forEach(subtitle -> subtitles.add(new Subtitle(rewriteURL(subtitle.getContent()),
subtitle.getFormat().getMimeType(), subtitle.getDisplayLanguageName(),
subtitle.getLanguageTag(), subtitle.isAutoGenerated())));
@ -137,17 +137,17 @@ public class ResponseHelper {
boolean livestream = info.getStreamType() == StreamType.LIVE_STREAM;
if (!livestream) {
info.getVideoOnlyStreams().forEach(stream -> videoStreams.add(new PipedStream(rewriteVideoURL(stream.getUrl()),
info.getVideoOnlyStreams().forEach(stream -> videoStreams.add(new PipedStream(rewriteVideoURL(stream.getContent()),
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(rewriteVideoURL(stream.getUrl()), String.valueOf(stream.getFormat()),
.add(new PipedStream(rewriteVideoURL(stream.getContent()), String.valueOf(stream.getFormat()),
stream.getResolution(), stream.getFormat().getMimeType(), false)));
info.getAudioStreams()
.forEach(stream -> audioStreams.add(new PipedStream(rewriteVideoURL(stream.getUrl()),
.forEach(stream -> audioStreams.add(new PipedStream(rewriteVideoURL(stream.getContent()),
String.valueOf(stream.getFormat()), stream.getAverageBitrate() + " kbps",
stream.getFormat().getMimeType(), false, stream.getBitrate(), stream.getInitStart(),
stream.getInitEnd(), stream.getIndexStart(), stream.getIndexEnd(), stream.getCodec())));
@ -528,22 +528,18 @@ public class ResponseHelper {
info.getRelatedItems().forEach(item -> {
switch (item.getInfoType()) {
case STREAM:
items.add(collectRelatedStream(item));
break;
case CHANNEL:
case STREAM -> items.add(collectRelatedStream(item));
case CHANNEL -> {
ChannelInfoItem channel = (ChannelInfoItem) item;
items.add(new SearchChannel(item.getName(), rewriteURL(item.getThumbnailUrl()),
substringYouTube(item.getUrl()), channel.getDescription(), channel.getSubscriberCount(),
channel.getStreamCount(), channel.isVerified()));
break;
case PLAYLIST:
}
case PLAYLIST -> {
PlaylistInfoItem playlist = (PlaylistInfoItem) item;
items.add(new SearchPlaylist(item.getName(), rewriteURL(item.getThumbnailUrl()),
substringYouTube(item.getUrl()), playlist.getUploaderName(), playlist.getStreamCount()));
break;
default:
break;
}
}
});
@ -569,22 +565,18 @@ public class ResponseHelper {
pages.getItems().forEach(item -> {
switch (item.getInfoType()) {
case STREAM:
items.add(collectRelatedStream(item));
break;
case CHANNEL:
case STREAM -> items.add(collectRelatedStream(item));
case CHANNEL -> {
ChannelInfoItem channel = (ChannelInfoItem) item;
items.add(new SearchChannel(item.getName(), rewriteURL(item.getThumbnailUrl()),
substringYouTube(item.getUrl()), channel.getDescription(), channel.getSubscriberCount(),
channel.getStreamCount(), channel.isVerified()));
break;
case PLAYLIST:
}
case PLAYLIST -> {
PlaylistInfoItem playlist = (PlaylistInfoItem) item;
items.add(new SearchPlaylist(item.getName(), rewriteURL(item.getThumbnailUrl()),
substringYouTube(item.getUrl()), playlist.getUploaderName(), playlist.getStreamCount()));
break;
default:
break;
}
}
});
@ -1031,8 +1023,6 @@ public class ResponseHelper {
))
.orderBy(cb.desc(root.get("uploaded")));
List<StreamItem> feedItems = new ObjectArrayList<>();
List<Video> videos = s.createQuery(criteria)
.setTimeout(20)
.setMaxResults(100)
@ -1123,9 +1113,7 @@ public class ResponseHelper {
channelIds.stream()
.filter(id -> !existing.contains(id))
.forEach(id -> Multithreading.runAsyncLimited(() -> {
saveChannel(id);
}));
.forEach(id -> Multithreading.runAsyncLimited(() -> saveChannel(id)));
}
} catch (Exception e) {
@ -1209,7 +1197,7 @@ public class ResponseHelper {
.sorted(Comparator.comparing(me.kavin.piped.utils.obj.db.Channel::getUploader, String.CASE_INSENSITIVE_ORDER))
.map(channel -> new SubscriptionChannel("/channel/" + channel.getUploaderId(),
channel.getUploader(), rewriteURL(channel.getUploaderAvatar()), channel.isVerified()))
.collect(Collectors.toUnmodifiableList());
.toList();
return mapper.writeValueAsBytes(subscriptionItems);
}
@ -1242,7 +1230,7 @@ public class ResponseHelper {
.sorted(Comparator.comparing(me.kavin.piped.utils.obj.db.Channel::getUploader, String.CASE_INSENSITIVE_ORDER))
.map(channel -> new SubscriptionChannel("/channel/" + channel.getUploaderId(),
channel.getUploader(), rewriteURL(channel.getUploaderAvatar()), channel.isVerified()))
.collect(Collectors.toUnmodifiableList());
.toList();
return mapper.writeValueAsBytes(subscriptionItems);
}
@ -1353,18 +1341,16 @@ public class ResponseHelper {
nextpage = page.getNextPage();
}
List<String> channelIds = videos.stream()
Set<String> channelIds = videos.stream()
.map(StreamInfoItem::getUploaderUrl)
.map(URLUtils::substringYouTube)
.map(s -> s.substring("/channel/".length()))
.collect(Collectors.toUnmodifiableSet())
.stream()
.collect(Collectors.toUnmodifiableList());
.collect(Collectors.toUnmodifiableSet());
List<String> videoIds = videos.stream()
.map(StreamInfoItem::getUrl)
.map(URLUtils::substringYouTube)
.map(s -> s.substring("/watch?v=".length()))
.collect(Collectors.toUnmodifiableList());
.toList();
try (Session s = DatabaseSessionFactory.createSession()) {
@ -1380,10 +1366,10 @@ public class ResponseHelper {
Map<String, PlaylistVideo> videoMap = new Object2ObjectOpenHashMap<>();
var playlistVideos = DatabaseHelper.getPlaylistVideosFromIds(s, videoIds);
videoIds.forEach(id -> {
playlistVideos.stream().filter(video -> video.getId().equals(id)).findFirst()
.ifPresent(playlistVideo -> videoMap.put(id, playlistVideo));
});
videoIds.forEach(id ->
playlistVideos.stream().filter(video -> video.getId().equals(id)).findFirst()
.ifPresent(playlistVideo -> videoMap.put(id, playlistVideo))
);
videos.forEach(video -> {
var channelId = substringYouTube(video.getUploaderUrl()).substring("/channel/".length());