mirror of
https://github.com/JonasunderscoreJones/ZtereoMUSIC.git
synced 2025-10-25 13:39:19 +02:00
Refactor YouTube interface, make error handling safer
This commit is contained in:
parent
9cf78e4386
commit
d31d36538e
2 changed files with 25 additions and 26 deletions
|
|
@ -12,12 +12,14 @@ import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager;
|
|||
import com.sedmelluq.discord.lavaplayer.tools.FriendlyException;
|
||||
import com.sedmelluq.discord.lavaplayer.track.AudioPlaylist;
|
||||
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
|
||||
import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager;
|
||||
import net.dv8tion.jda.api.entities.*;
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||
import net.dv8tion.jda.api.managers.AudioManager;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
|
|
@ -55,15 +57,14 @@ public class Play implements Command {
|
|||
Matcher matchedUrls = urlPattern.matcher(mergedArgs);
|
||||
boolean urlFound = matchedUrls.find();
|
||||
|
||||
// todo: clean up this bullshit try catch thing
|
||||
String identifier;
|
||||
if (!urlFound) {
|
||||
// youtube api shit
|
||||
try {
|
||||
identifier = YoutubeSearch.getVideoUrl(mergedArgs);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
identifier = "error";
|
||||
Optional<String> query = YoutubeSearch.query(mergedArgs);
|
||||
if (query.isPresent()) {
|
||||
identifier = query.get();
|
||||
} else {
|
||||
messageEvent.getChannel().sendMessage("I found no matches for that song!").queue();
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
// set identifier to the parsed url
|
||||
|
|
|
|||
|
|
@ -10,32 +10,30 @@ import java.io.IOException;
|
|||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Optional;
|
||||
|
||||
public class YoutubeSearch {
|
||||
public static String getVideoUrl(String title) throws IOException {
|
||||
String apiKey = ZtereoMUSIC.getInstance().getConfig().getPropreties().get("yt_api_key");
|
||||
String query;
|
||||
private static final String API_KEY = ZtereoMUSIC.getInstance().getConfig().getPropreties().get("yt_api_key");
|
||||
private static final Json JSON = Json.json();
|
||||
|
||||
title = (title.contains(" ")) ? title.replace(" ","+") : title;
|
||||
query = "https://www.googleapis.com/youtube/v3/search?part=snippet&q=%22"+title+"%22&type=video&key="+apiKey;
|
||||
public static Optional<String> query(String title) {
|
||||
String query = "https://www.googleapis.com/youtube/v3/search?part=snippet&q=%22"
|
||||
+ title.replace(' ', '+') + "%22&type=video&key=" + API_KEY;
|
||||
|
||||
// todo: add safety here, sounds a bit unsafe to me tbh but idk
|
||||
InputStream inputStream = new URL(query).openStream();
|
||||
String jsonResponse = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
|
||||
|
||||
//todo: clean up this bullshit try/catch shit and error
|
||||
Json json = Json.json();
|
||||
JsonNode parsedResponse;
|
||||
JsonPath path = JsonPath.parse("items[0].id.videoId");
|
||||
String videoId;
|
||||
String jsonResponse;
|
||||
try {
|
||||
parsedResponse = json.parse(jsonResponse);
|
||||
videoId = parsedResponse.query(path).asString();
|
||||
jsonResponse = new String(new URL(query).openStream().readAllBytes());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return Optional.empty();
|
||||
}
|
||||
JsonPath path = JsonPath.parse("items[0].id.videoId");
|
||||
|
||||
try {
|
||||
return Optional.ofNullable(JSON.parse(jsonResponse).query(path).asString());
|
||||
} catch (JsonSyntaxException e) {
|
||||
e.printStackTrace();
|
||||
videoId = "error";
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
return videoId;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue