Merge pull request #416 from TeamPiped/code-improvements

Minor code improvements
This commit is contained in:
Kavin 2022-10-31 10:46:49 +00:00 committed by GitHub
commit 2ab8fde0bb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 68 additions and 64 deletions

View file

@ -21,7 +21,6 @@ dependencies {
implementation 'com.fasterxml.jackson.core:jackson-core:2.13.4' implementation 'com.fasterxml.jackson.core:jackson-core:2.13.4'
implementation 'com.fasterxml.jackson.core:jackson-annotations:2.13.4' implementation 'com.fasterxml.jackson.core:jackson-annotations:2.13.4'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.4.2' implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.4.2'
implementation 'org.json:json:20220924'
implementation 'com.github.ben-manes.caffeine:caffeine:3.1.1' implementation 'com.github.ben-manes.caffeine:caffeine:3.1.1'
implementation 'com.rometools:rome:1.18.0' implementation 'com.rometools:rome:1.18.0'
implementation 'com.github.ipfs:java-ipfs-http-client:v1.3.3' implementation 'com.github.ipfs:java-ipfs-http-client:v1.3.3'

View file

@ -48,6 +48,9 @@ public class SearchHandlers {
public static byte[] searchResponse(String q, String filter) public static byte[] searchResponse(String q, String filter)
throws IOException, ExtractionException { throws IOException, ExtractionException {
if (StringUtils.isEmpty(q) || StringUtils.isEmpty(filter))
ExceptionHandler.throwErrorResponse(new InvalidRequestResponse("query and filter are required parameters"));
Sentry.setExtra("query", q); Sentry.setExtra("query", q);
final SearchInfo info = SearchInfo.getInfo(YOUTUBE_SERVICE, final SearchInfo info = SearchInfo.getInfo(YOUTUBE_SERVICE,
@ -65,8 +68,8 @@ public class SearchHandlers {
public static byte[] searchPageResponse(String q, String filter, String prevpageStr) public static byte[] searchPageResponse(String q, String filter, String prevpageStr)
throws IOException, ExtractionException { throws IOException, ExtractionException {
if (StringUtils.isEmpty(prevpageStr)) if (StringUtils.isEmpty(q) || StringUtils.isEmpty(prevpageStr))
ExceptionHandler.throwErrorResponse(new InvalidRequestResponse("nextpage is a required parameter")); ExceptionHandler.throwErrorResponse(new InvalidRequestResponse("query and nextpage are required parameter"));
Page prevpage = mapper.readValue(prevpageStr, Page.class); Page prevpage = mapper.readValue(prevpageStr, Page.class);

View file

@ -119,16 +119,15 @@ public class FeedHandlers {
) )
.orderBy(cb.desc(root.get("uploaded"))); .orderBy(cb.desc(root.get("uploaded")));
List<StreamItem> feedItems = new ObjectArrayList<>(); List<StreamItem> feedItems = s.createQuery(criteria).setTimeout(20).stream()
.parallel().map(video -> {
var channel = video.getChannel();
for (Video video : s.createQuery(criteria).setTimeout(20).list()) { return new StreamItem("/watch?v=" + video.getId(), video.getTitle(),
var channel = video.getChannel(); rewriteURL(video.getThumbnail()), channel.getUploader(), "/channel/" + channel.getUploaderId(),
rewriteURL(channel.getUploaderAvatar()), null, null, video.getDuration(), video.getViews(),
feedItems.add(new StreamItem("/watch?v=" + video.getId(), video.getTitle(), video.getUploaded(), channel.isVerified(), video.isShort());
rewriteURL(video.getThumbnail()), channel.getUploader(), "/channel/" + channel.getUploaderId(), }).toList();
rewriteURL(channel.getUploaderAvatar()), null, null, video.getDuration(), video.getViews(),
video.getUploaded(), channel.isVerified(), video.isShort()));
}
return mapper.writeValueAsBytes(feedItems); return mapper.writeValueAsBytes(feedItems);
} }
@ -173,29 +172,27 @@ public class FeedHandlers {
) )
.orderBy(cb.desc(root.get("uploaded"))); .orderBy(cb.desc(root.get("uploaded")));
List<Video> videos = s.createQuery(criteria) final List<SyndEntry> entries = s.createQuery(criteria)
.setTimeout(20) .setTimeout(20)
.setMaxResults(100) .setMaxResults(100)
.list(); .stream()
.map(video -> {
var channel = video.getChannel();
SyndEntry entry = new SyndEntryImpl();
final List<SyndEntry> entries = new ObjectArrayList<>(); SyndPerson person = new SyndPersonImpl();
person.setName(channel.getUploader());
person.setUri(Constants.FRONTEND_URL + "/channel/" + channel.getUploaderId());
for (Video video : videos) { entry.setAuthors(Collections.singletonList(person));
var channel = video.getChannel();
SyndEntry entry = new SyndEntryImpl();
SyndPerson person = new SyndPersonImpl(); entry.setLink(Constants.FRONTEND_URL + "/watch?v=" + video.getId());
person.setName(channel.getUploader()); entry.setUri(Constants.FRONTEND_URL + "/watch?v=" + video.getId());
person.setUri(Constants.FRONTEND_URL + "/channel/" + channel.getUploaderId()); entry.setTitle(video.getTitle());
entry.setPublishedDate(new Date(video.getUploaded()));
entry.setAuthors(Collections.singletonList(person)); return entry;
}).toList();
entry.setLink(Constants.FRONTEND_URL + "/watch?v=" + video.getId());
entry.setUri(Constants.FRONTEND_URL + "/watch?v=" + video.getId());
entry.setTitle(video.getTitle());
entry.setPublishedDate(new Date(video.getUploaded()));
entries.add(entry);
}
feed.setEntries(entries); feed.setEntries(entries);
@ -232,16 +229,15 @@ public class FeedHandlers {
)) ))
.orderBy(cb.desc(root.get("uploaded"))); .orderBy(cb.desc(root.get("uploaded")));
List<StreamItem> feedItems = new ObjectArrayList<>(); List<StreamItem> feedItems = s.createQuery(criteria).setTimeout(20).stream()
.parallel().map(video -> {
var channel = video.getChannel();
for (Video video : s.createQuery(criteria).setTimeout(20).list()) { return new StreamItem("/watch?v=" + video.getId(), video.getTitle(),
var channel = video.getChannel(); rewriteURL(video.getThumbnail()), channel.getUploader(), "/channel/" + channel.getUploaderId(),
rewriteURL(channel.getUploaderAvatar()), null, null, video.getDuration(), video.getViews(),
feedItems.add(new StreamItem("/watch?v=" + video.getId(), video.getTitle(), video.getUploaded(), channel.isVerified(), video.isShort());
rewriteURL(video.getThumbnail()), channel.getUploader(), "/channel/" + channel.getUploaderId(), }).toList();
rewriteURL(channel.getUploaderAvatar()), null, null, video.getDuration(), video.getViews(),
video.getUploaded(), channel.isVerified(), video.isShort()));
}
updateSubscribedTime(filtered); updateSubscribedTime(filtered);
addMissingChannels(filtered); addMissingChannels(filtered);
@ -451,10 +447,9 @@ public class FeedHandlers {
query.select(root) query.select(root)
.where(root.get("uploader_id").in(subquery)); .where(root.get("uploader_id").in(subquery));
var channels = s.createQuery(query).list(); List<SubscriptionChannel> subscriptionItems = s.createQuery(query)
List<SubscriptionChannel> subscriptionItems = channels
.stream().parallel() .stream().parallel()
.filter(Objects::nonNull)
.sorted(Comparator.comparing(me.kavin.piped.utils.obj.db.Channel::getUploader, String.CASE_INSENSITIVE_ORDER)) .sorted(Comparator.comparing(me.kavin.piped.utils.obj.db.Channel::getUploader, String.CASE_INSENSITIVE_ORDER))
.map(channel -> new SubscriptionChannel("/channel/" + channel.getUploaderId(), .map(channel -> new SubscriptionChannel("/channel/" + channel.getUploaderId(),
channel.getUploader(), rewriteURL(channel.getUploaderAvatar()), channel.isVerified())) channel.getUploader(), rewriteURL(channel.getUploaderAvatar()), channel.isVerified()))
@ -488,9 +483,7 @@ public class FeedHandlers {
query.select(root); query.select(root);
query.where(root.get("uploader_id").in(filtered)); query.where(root.get("uploader_id").in(filtered));
var channels = s.createQuery(query).list(); List<SubscriptionChannel> subscriptionItems = s.createQuery(query)
List<SubscriptionChannel> subscriptionItems = channels
.stream().parallel() .stream().parallel()
.sorted(Comparator.comparing(me.kavin.piped.utils.obj.db.Channel::getUploader, String.CASE_INSENSITIVE_ORDER)) .sorted(Comparator.comparing(me.kavin.piped.utils.obj.db.Channel::getUploader, String.CASE_INSENSITIVE_ORDER))
.map(channel -> new SubscriptionChannel("/channel/" + channel.getUploaderId(), .map(channel -> new SubscriptionChannel("/channel/" + channel.getUploaderId(),

View file

@ -5,10 +5,14 @@ import okhttp3.MediaType;
import okhttp3.Request; import okhttp3.Request;
import okhttp3.RequestBody; import okhttp3.RequestBody;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import java.io.IOException; import java.io.IOException;
import static me.kavin.piped.consts.Constants.h2client;
import static me.kavin.piped.consts.Constants.mapper;
import static me.kavin.piped.utils.RequestUtils.sendGet;
import static me.kavin.piped.utils.URLUtils.silentEncode;
public class LbryHelper { public class LbryHelper {
public static String getLBRYId(String videoId) throws IOException { public static String getLBRYId(String videoId) throws IOException {
@ -16,9 +20,10 @@ public class LbryHelper {
if (Constants.DISABLE_LBRY) if (Constants.DISABLE_LBRY)
return null; return null;
return new JSONObject( return mapper.readTree(sendGet("https://api.lbry.com/yt/resolve?video_ids=" + silentEncode(videoId)))
RequestUtils.sendGet("https://api.lbry.com/yt/resolve?video_ids=" + URLUtils.silentEncode(videoId)) .at("/data/videos")
).getJSONObject("data").getJSONObject("videos").optString(videoId, null); .path(videoId)
.asText(null);
} }
public static String getLBRYStreamURL(String lbryId) public static String getLBRYStreamURL(String lbryId)
@ -29,24 +34,28 @@ public class LbryHelper {
var request = new Request.Builder() var request = new Request.Builder()
.url("https://api.lbry.tv/api/v1/proxy?m=get") .url("https://api.lbry.tv/api/v1/proxy?m=get")
.post(RequestBody.create(String.valueOf( .post(RequestBody.create(mapper.writeValueAsBytes(
new JSONObject().put("id", System.currentTimeMillis()) mapper.createObjectNode()
.put("jsonrpc", "2.0") .put("id", System.currentTimeMillis())
.put("method", "get") .put("id", System.currentTimeMillis())
.put("params", .put("jsonrpc", "2.0")
new JSONObject() .put("method", "get")
.put("uri", "lbry://" + lbryId) .set("params",
.put("save_file", true))) mapper.createObjectNode()
, MediaType.get("application/json"))) .put("uri", "lbry://" + lbryId)
.put("save_file", true)
)
), MediaType.get("application/json")))
.build(); .build();
var resp = Constants.h2client.newCall(request).execute(); try (var resp = h2client.newCall(request).execute()) {
if (resp.isSuccessful()) {
var json = new JSONObject(resp.body().string()); return mapper.readTree(resp.body().byteStream())
.at("/result/streaming_url")
resp.close(); .asText(null);
}
return json.getJSONObject("result").getString("streaming_url"); }
return null;
} }
} }