🚑 Fix Youtube Search

This commit is contained in:
SebClem 2020-10-31 16:26:21 +01:00
parent 3a878c7be8
commit 6cffd0ff73
2 changed files with 32 additions and 16 deletions

View File

@ -1,5 +1,10 @@
package net.Broken.audio.Youtube;
import com.sedmelluq.discord.lavaplayer.source.youtube.YoutubeAudioTrack;
import com.sedmelluq.discord.lavaplayer.source.youtube.YoutubeSearchProvider;
import com.sedmelluq.discord.lavaplayer.track.AudioItem;
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
import com.sedmelluq.discord.lavaplayer.track.BasicAudioPlaylist;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jsoup.Jsoup;
@ -12,6 +17,7 @@ import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class YoutubeSearchRework {
@ -29,9 +35,21 @@ public class YoutubeSearchRework {
public List<SearchResult> searchVideo(String search, int maxResult, boolean playlist) throws IOException {
search = URLEncoder.encode(search, StandardCharsets.UTF_8.toString());
String url = "https://www.youtube.com/results?search_query=" + search + "&sp="+ (playlist ? "EgIQAw%3D%3D" :"EgIQAQ%3D%3D");
Document doc = getYoutubeSearchDocument(url);
return extractVideoInfo(doc, maxResult, playlist);
YoutubeSearchProvider searchProvider = new YoutubeSearchProvider();
BasicAudioPlaylist rawResult = (BasicAudioPlaylist) searchProvider.loadSearchResult(search, audioTrackInfo -> new YoutubeAudioTrack(audioTrackInfo, null));
List<SearchResult> results = new ArrayList<>();
for (AudioTrack track : rawResult.getTracks()) {
String imageUrl = "https://i.ytimg.com/vi/" + track.getIdentifier() + "/hqdefault.jpg";
long hour = TimeUnit.MILLISECONDS.toHours(track.getInfo().length);
String hms = hour == 0 ? "" : Long.toString(hour);
hms += String.format("%02d:%02d",
TimeUnit.MILLISECONDS.toMinutes(track.getInfo().length) % TimeUnit.HOURS.toMinutes(1),
TimeUnit.MILLISECONDS.toSeconds(track.getInfo().length) % TimeUnit.MINUTES.toSeconds(1));
results.add(new SearchResult(track.getIdentifier(), track.getInfo().title, "", "", "", track.getInfo().author, imageUrl, hms));
}
return results;
}
@ -86,9 +104,7 @@ public class YoutubeSearchRework {
if (!playlist) {
duration = videoDiv.selectFirst(".video-time").text();
imageUrl = "https://i.ytimg.com/vi/" + id + "/hqdefault.jpg";
}
else{
} else {
String listUrl = titleDiv.attributes().get("href");
int listIndex = listUrl.indexOf("&list=");
listUrl = listUrl.substring(0, listIndex).replace("/watch?v=", "");

View File

@ -446,7 +446,7 @@ function search() {
"<li class=\"collection-item avatar\">" +
" <img src=\"" + item["imageUrl"] + "\" alt=\"\" class=\"\">" +
" <a class=\"title truncate\" href='" + url + item["id"] + "' target=\"_blank\"><b>" + item["title"] + "</b></a>" +
" <p class='truncate grey-text text-darken-1'>" + item["channelTittle"] + " &#9553 " + item["publishedAt"] + " <br>" + item["duration"] +
" <p class='truncate grey-text text-darken-1'>" + item["channelTittle"] + " <br>" + item["duration"] +
" </p>" +
" <a href=\"#!\" class=\"secondary-content btn waves-effect waves-light green add-btn-list scale-transition\" id='" + item["id"] + "'><i class=\"material-icons \">add_circle_outline</i></a>" +
" </div>" +