Add search.

This commit is contained in:
FireMasterK 2020-12-09 19:00:42 +05:30
parent 26b92c751a
commit 709478605a
No known key found for this signature in database
GPG key ID: 8DFF5DD33E93DB58
4 changed files with 88 additions and 0 deletions

View file

@ -124,6 +124,20 @@ public class Main {
});
routes.get("/search", (req, res) -> {
QueryStringDecoder query = new QueryStringDecoder(req.uri());
try {
return writeResponse(res, ResponseHelper.searchResponse(query.parameters().get("q").get(0)), 200,
"public, max-age=600");
} catch (Exception e) {
e.printStackTrace();
return writeResponse(res, ExceptionUtils.getStackTrace(e), 500, "private");
}
});
routes.get("/trending", (req, res) -> {
try {

View file

@ -21,6 +21,7 @@ import org.schabi.newpipe.extractor.comments.CommentsInfo;
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.kiosk.KioskInfo;
import org.schabi.newpipe.extractor.search.SearchInfo;
import org.schabi.newpipe.extractor.stream.Stream;
import org.schabi.newpipe.extractor.stream.StreamInfo;
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
@ -37,6 +38,8 @@ import me.kavin.piped.utils.obj.PipedStream;
import me.kavin.piped.utils.obj.StreamItem;
import me.kavin.piped.utils.obj.Streams;
import me.kavin.piped.utils.obj.Subtitle;
import me.kavin.piped.utils.obj.search.SearchItem;
import me.kavin.piped.utils.obj.search.SearchStream;
public class ResponseHelper {
@ -205,6 +208,33 @@ public class ResponseHelper {
}
public static final String searchResponse(String q) throws IOException, ExtractionException, InterruptedException {
final SearchInfo info = SearchInfo.getInfo(Constants.YOUTUBE_SERVICE,
Constants.YOUTUBE_SERVICE.getSearchQHFactory().fromQuery(q));
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()),
item.getUrl().substring(23), stream.getViewCount(), stream.getDuration()));
break;
case CHANNEL:
items.add(new SearchItem(item.getName(), rewriteURL(item.getThumbnailUrl()),
item.getUrl().substring(23)));
break;
default:
break;
}
});
return Constants.mapper.writeValueAsString(items);
}
private static final String getLBRYStreamURL(String videoId) throws IOException, InterruptedException {
String lbryId = new JSONObject(Constants.h2client.send(HttpRequest

View file

@ -0,0 +1,24 @@
package me.kavin.piped.utils.obj.search;
public class SearchItem {
private String name, thumbnail, url;
public SearchItem(String name, String thumbnail, String url) {
this.name = name;
this.thumbnail = thumbnail;
this.url = url;
}
public String getName() {
return name;
}
public String getThumbnail() {
return thumbnail;
}
public String getUrl() {
return url;
}
}

View file

@ -0,0 +1,20 @@
package me.kavin.piped.utils.obj.search;
public class SearchStream extends SearchItem {
private long views, duration;
public SearchStream(String name, String thumbnail, String url, long views, long duration) {
super(name, thumbnail, url);
this.views = views;
this.duration = duration;
}
public long getViews() {
return views;
}
public long getDuration() {
return duration;
}
}