🚑 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; 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.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.jsoup.Jsoup; import org.jsoup.Jsoup;
@ -12,6 +17,7 @@ import java.net.URLEncoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit;
public class YoutubeSearchRework { public class YoutubeSearchRework {
@ -29,9 +35,21 @@ public class YoutubeSearchRework {
public List<SearchResult> searchVideo(String search, int maxResult, boolean playlist) throws IOException { public List<SearchResult> searchVideo(String search, int maxResult, boolean playlist) throws IOException {
search = URLEncoder.encode(search, StandardCharsets.UTF_8.toString()); 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); YoutubeSearchProvider searchProvider = new YoutubeSearchProvider();
return extractVideoInfo(doc, maxResult, playlist); 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;
} }
@ -48,15 +66,15 @@ public class YoutubeSearchRework {
List<SearchResult> results = new ArrayList<>(); List<SearchResult> results = new ArrayList<>();
int i = 0; int i = 0;
for (Element videoDiv : videosDivs) { for (Element videoDiv : videosDivs) {
if(i >= maxResult) if (i >= maxResult)
break; break;
Element titleDiv = videoDiv.selectFirst(".yt-uix-tile-link"); Element titleDiv = videoDiv.selectFirst(".yt-uix-tile-link");
String id; String id;
if(!playlist) if (!playlist)
id = titleDiv.attributes().get("href").replace("/watch?v=", ""); id = titleDiv.attributes().get("href").replace("/watch?v=", "");
else{ else {
String listUrl = titleDiv.attributes().get("href"); String listUrl = titleDiv.attributes().get("href");
int listIndex = listUrl.indexOf("list="); int listIndex = listUrl.indexOf("list=");
id = listUrl.substring(listIndex).replace("list=", ""); id = listUrl.substring(listIndex).replace("list=", "");
} }
@ -65,7 +83,7 @@ public class YoutubeSearchRework {
Elements metas = videoDiv.selectFirst(".yt-lockup-meta-info").getElementsByTag("li"); Elements metas = videoDiv.selectFirst(".yt-lockup-meta-info").getElementsByTag("li");
String view = ""; String view = "";
String date = ""; String date = "";
if(!playlist){ if (!playlist) {
for (Element metaElem : metas) { for (Element metaElem : metas) {
if (metaElem.text().contains("view")) { if (metaElem.text().contains("view")) {
@ -83,14 +101,12 @@ public class YoutubeSearchRework {
String imageUrl; String imageUrl;
String duration; String duration;
if(!playlist){ if (!playlist) {
duration = videoDiv.selectFirst(".video-time").text(); duration = videoDiv.selectFirst(".video-time").text();
imageUrl = "https://i.ytimg.com/vi/" + id + "/hqdefault.jpg"; imageUrl = "https://i.ytimg.com/vi/" + id + "/hqdefault.jpg";
} } else {
else{
String listUrl = titleDiv.attributes().get("href"); String listUrl = titleDiv.attributes().get("href");
int listIndex = listUrl.indexOf("&list="); int listIndex = listUrl.indexOf("&list=");
listUrl = listUrl.substring(0, listIndex).replace("/watch?v=", ""); listUrl = listUrl.substring(0, listIndex).replace("/watch?v=", "");
imageUrl = "https://i.ytimg.com/vi/" + listUrl + "/hqdefault.jpg"; imageUrl = "https://i.ytimg.com/vi/" + listUrl + "/hqdefault.jpg";
duration = videoDiv.selectFirst(".formatted-video-count-label").text(); duration = videoDiv.selectFirst(".formatted-video-count-label").text();
@ -107,7 +123,7 @@ public class YoutubeSearchRework {
public String getRelatedVideo(String sourceVideoId) throws IOException, RelatedIdNotFound { public String getRelatedVideo(String sourceVideoId) throws IOException, RelatedIdNotFound {
sourceVideoId = URLEncoder.encode(sourceVideoId, StandardCharsets.UTF_8.toString()); sourceVideoId = URLEncoder.encode(sourceVideoId, StandardCharsets.UTF_8.toString());
String url = "https://www.youtube.com/watch?v=" + sourceVideoId; String url = "https://www.youtube.com/watch?v=" + sourceVideoId;
Document doc = getYoutubeSearchDocument(url); Document doc = getYoutubeSearchDocument(url);
return extractRelatedVideoId(doc); return extractRelatedVideoId(doc);
@ -115,7 +131,7 @@ public class YoutubeSearchRework {
private String extractRelatedVideoId(Document doc) throws RelatedIdNotFound { private String extractRelatedVideoId(Document doc) throws RelatedIdNotFound {
Elements elements = doc.select(".autoplay-bar .content-link"); Elements elements = doc.select(".autoplay-bar .content-link");
if(elements.size() == 0){ if (elements.size() == 0) {
throw new RelatedIdNotFound(); throw new RelatedIdNotFound();
} }
Element elem = elements.get(0); Element elem = elements.get(0);

View File

@ -446,7 +446,7 @@ function search() {
"<li class=\"collection-item avatar\">" + "<li class=\"collection-item avatar\">" +
" <img src=\"" + item["imageUrl"] + "\" alt=\"\" class=\"\">" + " <img src=\"" + item["imageUrl"] + "\" alt=\"\" class=\"\">" +
" <a class=\"title truncate\" href='" + url + item["id"] + "' target=\"_blank\"><b>" + item["title"] + "</b></a>" + " <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>" + " </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>" + " <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>" + " </div>" +