Update youtube api

This commit is contained in:
SebClem 2021-02-17 13:37:55 +01:00
parent 26f6edfee7
commit 96d40814b1
3 changed files with 18 additions and 25 deletions

View File

@ -51,7 +51,6 @@ dependencies {
compile("com.sedmelluq:lavaplayer:1.3.71") compile("com.sedmelluq:lavaplayer:1.3.71")
// compile 'net.dv8tion:JDA:4.0.0_46'
compile 'net.dv8tion:JDA:4.2.0_228' compile 'net.dv8tion:JDA:4.2.0_228'
compile group: 'org.json', name: 'json', version: '20201115' compile group: 'org.json', name: 'json', version: '20201115'
compile 'org.springframework.security:spring-security-web:5.0.1.RELEASE' compile 'org.springframework.security:spring-security-web:5.0.1.RELEASE'
@ -63,8 +62,8 @@ dependencies {
compile 'mysql:mysql-connector-java' compile 'mysql:mysql-connector-java'
compile 'org.reflections:reflections:0.9.11' compile 'org.reflections:reflections:0.9.11'
compile 'org.apache.commons:commons-lang3:3.11' compile 'org.apache.commons:commons-lang3:3.11'
compile 'com.google.api-client:google-api-client:1.23.0' compile 'com.google.api-client:google-api-client:1.31.2'
compile 'com.google.apis:google-api-services-youtube:v3-rev192-1.23.0' compile 'com.google.apis:google-api-services-youtube:v3-rev20210210-1.31.0'
compile group: 'org.jsoup', name: 'jsoup', version: '1.13.1' compile group: 'org.jsoup', name: 'jsoup', version: '1.13.1'

View File

@ -130,7 +130,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(".ytd-compact-autoplay-renderer .yt-simple-endpoint");
if (elements.size() == 0) { if (elements.size() == 0) {
throw new RelatedIdNotFound(); throw new RelatedIdNotFound();
} }

View File

@ -1,7 +1,7 @@
package net.Broken.audio.Youtube; package net.Broken.audio.Youtube;
import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.client.json.gson.GsonFactory;
import com.google.api.services.youtube.YouTube; import com.google.api.services.youtube.YouTube;
import com.google.api.services.youtube.model.*; import com.google.api.services.youtube.model.*;
import com.google.api.services.youtube.model.SearchResult; import com.google.api.services.youtube.model.SearchResult;
@ -10,6 +10,7 @@ import org.apache.logging.log4j.Logger;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import static org.hibernate.engine.jdbc.Size.LobMultiplier.M; import static org.hibernate.engine.jdbc.Size.LobMultiplier.M;
@ -34,7 +35,7 @@ public class YoutubeTools {
private YouTube getYoutubeService() { private YouTube getYoutubeService() {
YouTube.Builder builder = new YouTube.Builder(new NetHttpTransport(), new JacksonFactory(), request -> { YouTube.Builder builder = new YouTube.Builder(new NetHttpTransport(), new GsonFactory(), request -> {
}); });
builder.setApplicationName("BotDiscord"); builder.setApplicationName("BotDiscord");
return builder.build(); return builder.build();
@ -48,19 +49,11 @@ public class YoutubeTools {
YouTube youtube = getYoutubeService(); YouTube youtube = getYoutubeService();
HashMap<String, String> parameters = new HashMap<>();
parameters.put("part", "snippet");
parameters.put("relatedToVideoId", videoId);
parameters.put("type", "video");
YouTube.Search.List searchListRelatedVideosRequest = youtube.search().list(parameters.get("part"));
if (parameters.containsKey("relatedToVideoId") && parameters.get("relatedToVideoId") != "") {
searchListRelatedVideosRequest.setRelatedToVideoId(parameters.get("relatedToVideoId"));
}
if (parameters.containsKey("type") && !parameters.get("type").equals("")) { YouTube.Search.List searchListRelatedVideosRequest = youtube.search().list(Collections.singletonList("snippet"));
searchListRelatedVideosRequest.setType(parameters.get("type")); searchListRelatedVideosRequest.setRelatedToVideoId(videoId);
} searchListRelatedVideosRequest.setType(Collections.singletonList("video"));
searchListRelatedVideosRequest.setKey(apiKey); searchListRelatedVideosRequest.setKey(apiKey);
@ -68,7 +61,8 @@ public class YoutubeTools {
for (SearchResult item : response.getItems()) { for (SearchResult item : response.getItems()) {
if (!history.contains(item.getId().getVideoId())) { if (!history.contains(item.getId().getVideoId())) {
return item.getId().getVideoId(); if(item.getSnippet() != null)
return item.getId().getVideoId();
} else } else
logger.debug("ID already on history"); logger.debug("ID already on history");
} }
@ -81,11 +75,11 @@ public class YoutubeTools {
public ArrayList<net.Broken.audio.Youtube.SearchResult> search(String query, long max, boolean playlist) throws IOException { public ArrayList<net.Broken.audio.Youtube.SearchResult> search(String query, long max, boolean playlist) throws IOException {
YouTube youTube = getYoutubeService(); YouTube youTube = getYoutubeService();
YouTube.Search.List searchList = youTube.search().list("snippet"); YouTube.Search.List searchList = youTube.search().list(Collections.singletonList("snippet"));
if(playlist) if(playlist)
searchList.setType("playlist"); searchList.setType(Collections.singletonList("playlist"));
else else
searchList.setType("video"); searchList.setType(Collections.singletonList("video"));
searchList.setSafeSearch("none"); searchList.setSafeSearch("none");
searchList.setMaxResults(max); searchList.setMaxResults(max);
searchList.setQ(query); searchList.setQ(query);
@ -102,8 +96,8 @@ public class YoutubeTools {
idString.append(item.getId().getPlaylistId()).append(","); idString.append(item.getId().getPlaylistId()).append(",");
} }
HashMap<String, Playlist> playlistHashMap = new HashMap<>(); HashMap<String, Playlist> playlistHashMap = new HashMap<>();
YouTube.Playlists.List list = youTube.playlists().list("contentDetails"); YouTube.Playlists.List list = youTube.playlists().list(Collections.singletonList("contentDetails"));
list.setId(idString.toString()); list.setId(Collections.singletonList(idString.toString()));
list.setKey(apiKey); list.setKey(apiKey);
PlaylistListResponse playlistResponse = list.execute(); PlaylistListResponse playlistResponse = list.execute();
for( Playlist item : playlistResponse.getItems()){ for( Playlist item : playlistResponse.getItems()){
@ -122,8 +116,8 @@ public class YoutubeTools {
idString.append(item.getId().getVideoId()).append(","); idString.append(item.getId().getVideoId()).append(",");
} }
HashMap<String, Video> videoHashMap = new HashMap<>(); HashMap<String, Video> videoHashMap = new HashMap<>();
YouTube.Videos.List video = youTube.videos().list("contentDetails"); YouTube.Videos.List video = youTube.videos().list(Collections.singletonList("contentDetails"));
video.setId(idString.toString()); video.setId(Collections.singletonList(idString.toString()));
video.setKey(apiKey); video.setKey(apiKey);
VideoListResponse videoResponse = video.execute(); VideoListResponse videoResponse = video.execute();
for(Video item : videoResponse.getItems()){ for(Video item : videoResponse.getItems()){