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.tools.FriendlyException;
|
||||||
import com.sedmelluq.discord.lavaplayer.track.AudioPlaylist;
|
import com.sedmelluq.discord.lavaplayer.track.AudioPlaylist;
|
||||||
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
|
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.entities.*;
|
||||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||||
import net.dv8tion.jda.api.managers.AudioManager;
|
import net.dv8tion.jda.api.managers.AudioManager;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
|
@ -55,15 +57,14 @@ public class Play implements Command {
|
||||||
Matcher matchedUrls = urlPattern.matcher(mergedArgs);
|
Matcher matchedUrls = urlPattern.matcher(mergedArgs);
|
||||||
boolean urlFound = matchedUrls.find();
|
boolean urlFound = matchedUrls.find();
|
||||||
|
|
||||||
// todo: clean up this bullshit try catch thing
|
|
||||||
String identifier;
|
String identifier;
|
||||||
if (!urlFound) {
|
if (!urlFound) {
|
||||||
// youtube api shit
|
Optional<String> query = YoutubeSearch.query(mergedArgs);
|
||||||
try {
|
if (query.isPresent()) {
|
||||||
identifier = YoutubeSearch.getVideoUrl(mergedArgs);
|
identifier = query.get();
|
||||||
} catch (IOException e) {
|
} else {
|
||||||
e.printStackTrace();
|
messageEvent.getChannel().sendMessage("I found no matches for that song!").queue();
|
||||||
identifier = "error";
|
return;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// set identifier to the parsed url
|
// set identifier to the parsed url
|
||||||
|
|
|
||||||
|
|
@ -10,32 +10,30 @@ import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
public class YoutubeSearch {
|
public class YoutubeSearch {
|
||||||
public static String getVideoUrl(String title) throws IOException {
|
private static final String API_KEY = ZtereoMUSIC.getInstance().getConfig().getPropreties().get("yt_api_key");
|
||||||
String apiKey = ZtereoMUSIC.getInstance().getConfig().getPropreties().get("yt_api_key");
|
private static final Json JSON = Json.json();
|
||||||
String query;
|
|
||||||
|
|
||||||
title = (title.contains(" ")) ? title.replace(" ","+") : title;
|
public static Optional<String> query(String title) {
|
||||||
query = "https://www.googleapis.com/youtube/v3/search?part=snippet&q=%22"+title+"%22&type=video&key="+apiKey;
|
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
|
String jsonResponse;
|
||||||
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;
|
|
||||||
try {
|
try {
|
||||||
parsedResponse = json.parse(jsonResponse);
|
jsonResponse = new String(new URL(query).openStream().readAllBytes());
|
||||||
videoId = parsedResponse.query(path).asString();
|
} 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) {
|
} catch (JsonSyntaxException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
videoId = "error";
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
return videoId;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue