mirror of
https://github.com/JonasunderscoreJones/ZtereoMUSIC.git
synced 2025-10-24 21:19:18 +02:00
change: switch from optionals to pairs for error returns
This commit is contained in:
parent
e53d50e455
commit
4864ac49b9
3 changed files with 29 additions and 34 deletions
|
|
@ -12,9 +12,9 @@ import codes.ztereohype.ztereomusic.networking.YoutubeSearch;
|
|||
import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager;
|
||||
import net.dv8tion.jda.api.entities.*;
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||
import net.shadew.util.data.Pair;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
|
|
@ -70,24 +70,24 @@ public class Play implements Command {
|
|||
boolean spotifyUrlFound = matchedSpotifyUrl.find();
|
||||
|
||||
if (spotifyUrlFound) {
|
||||
Optional<String> songSearchQuery = SpotifyApiHelper.query(mergedArgs, messageChannel);
|
||||
Pair<Boolean, String> songSearchQuery = SpotifyApiHelper.query(mergedArgs);
|
||||
|
||||
if (songSearchQuery.isPresent()) {
|
||||
mergedArgs = songSearchQuery.get();
|
||||
if (songSearchQuery.first()) {
|
||||
mergedArgs = songSearchQuery.second();
|
||||
} else {
|
||||
return; // SpotifyApiHelper takes care of answering why it failed
|
||||
messageChannel.sendMessage(songSearchQuery.second()).queue();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
String identifier;
|
||||
// spotify urls need to be queried through youtube
|
||||
if (!urlFound || spotifyUrlFound) {
|
||||
Optional<String> query = YoutubeSearch.query(mergedArgs);
|
||||
|
||||
if (query.isPresent()) {
|
||||
identifier = query.get();
|
||||
Pair<Boolean, String> query = YoutubeSearch.query(mergedArgs);
|
||||
if (query.first()) {
|
||||
identifier = query.second();
|
||||
} else {
|
||||
messageEvent.getChannel().sendMessage("I found no matches for that song!").queue();
|
||||
messageEvent.getChannel().sendMessage(query.second()).queue();
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -2,10 +2,9 @@ package codes.ztereohype.ztereomusic.networking;
|
|||
|
||||
import codes.ztereohype.ztereomusic.ZtereoMUSIC;
|
||||
import lombok.SneakyThrows;
|
||||
import net.dv8tion.jda.api.entities.MessageChannel;
|
||||
import net.shadew.json.Json;
|
||||
import net.shadew.json.JsonPath;
|
||||
import net.shadew.json.JsonSyntaxException;
|
||||
import net.shadew.util.data.Pair;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
|
|
@ -50,7 +49,6 @@ public class SpotifyApiHelper {
|
|||
@SneakyThrows
|
||||
private static Optional<String> getToken() {
|
||||
HttpClient client = HttpClient.newHttpClient();
|
||||
|
||||
HttpRequest request = HttpRequest.newBuilder(
|
||||
URI.create("https://accounts.spotify.com/api/token?grant_type=client_credentials"))
|
||||
.POST(HttpRequest.BodyPublishers.ofString(""))
|
||||
|
|
@ -63,21 +61,21 @@ public class SpotifyApiHelper {
|
|||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
String results = JSON.parse(response.body()).query(tokenPath).asString();
|
||||
return Optional.ofNullable(results);
|
||||
} catch (IOException | InterruptedException | JsonSyntaxException e) {
|
||||
} catch (IOException | InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
public static Optional<String> query(String songUrl, MessageChannel messageChannel) {
|
||||
// boolean is isSuccessful and string is content -> error if failure and content if success
|
||||
public static Pair<Boolean, String> query(String songUrl) {
|
||||
if (spotifyToken == null) {
|
||||
System.out.println("Null Spotify token detected");
|
||||
messageChannel.sendMessage("I don't have a spotify token for now. Try again later.").queue();
|
||||
return Optional.empty();
|
||||
return Pair.of(false, "I don't have a spotify token for now. Try again later.");
|
||||
}
|
||||
|
||||
if (songUrl.contains("/playlist/")) {
|
||||
messageChannel.sendMessage("Playlists aren't supported for now, please send the individual song links.").queue();
|
||||
if (songUrl.contains("playlist")) {
|
||||
return Pair.of(false, "Playlists aren't supported for now, please send the individual song links.");
|
||||
}
|
||||
|
||||
Matcher matchedSpotifyIdentifier = IDENTIFIER_PATTERN.matcher(songUrl);
|
||||
|
|
@ -85,8 +83,7 @@ public class SpotifyApiHelper {
|
|||
if (matchedSpotifyIdentifier.find()) {
|
||||
spotifyIdentifier = matchedSpotifyIdentifier.group();
|
||||
} else {
|
||||
messageChannel.sendMessage("Could not parse Spotify link. Try entering the song title directly.").queue();
|
||||
return Optional.empty();
|
||||
return Pair.of(false, "Could not parse Spotify link. Try entering the song title directly.");
|
||||
}
|
||||
|
||||
String query = "https://api.spotify.com/v1/tracks?ids="
|
||||
|
|
@ -102,19 +99,17 @@ public class SpotifyApiHelper {
|
|||
.build();
|
||||
|
||||
JsonPath titlePath = JsonPath.parse("tracks[0].name");
|
||||
JsonPath authorPath = JsonPath.parse("tracks[0].artists[0].name");
|
||||
// JsonPath authorPath = JsonPath.parse("tracks[0].artists[0].name");
|
||||
try {
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
|
||||
String title = JSON.parse(response.body()).query(titlePath).asString();
|
||||
String author = JSON.parse(response.body()).query(authorPath).asString();
|
||||
String songSearchQuery = title + " " + author + "official audio";
|
||||
// String author = JSON.parse(response.body()).query(authorPath).asString();
|
||||
|
||||
return Optional.of(songSearchQuery);
|
||||
} catch (IOException | InterruptedException | JsonSyntaxException e) {
|
||||
return Pair.of(true, title);
|
||||
} catch (IOException | InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
messageChannel.sendMessage("Something wrong happened with the spotify request.").queue();
|
||||
return Optional.empty();
|
||||
return Pair.of(false,"Something wrong happened with the spotify request.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,16 +4,16 @@ import codes.ztereohype.ztereomusic.ZtereoMUSIC;
|
|||
import net.shadew.json.Json;
|
||||
import net.shadew.json.JsonPath;
|
||||
import net.shadew.json.JsonSyntaxException;
|
||||
import net.shadew.util.data.Pair;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.util.Optional;
|
||||
|
||||
public class YoutubeSearch {
|
||||
private static final String API_KEY = ZtereoMUSIC.getInstance().getConfig().getPropreties().get("yt_api_key");
|
||||
private static final Json JSON = Json.json();
|
||||
|
||||
public static Optional<String> query(String title) {
|
||||
public static Pair<Boolean, 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;
|
||||
|
||||
|
|
@ -22,7 +22,7 @@ public class YoutubeSearch {
|
|||
jsonResponse = new String(new URL(query).openStream().readAllBytes());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return Optional.empty();
|
||||
return Pair.of(false, "There was a request error contacting youtube.");
|
||||
}
|
||||
|
||||
JsonPath resultsNumberPath = JsonPath.parse("pageInfo.totalResults");
|
||||
|
|
@ -30,12 +30,12 @@ public class YoutubeSearch {
|
|||
|
||||
try {
|
||||
int results = JSON.parse(jsonResponse).query(resultsNumberPath).asInt();
|
||||
if (results == 0) return Optional.empty();
|
||||
if (results == 0) return Pair.of(false, "I found no matches for that song.");
|
||||
|
||||
return Optional.ofNullable(JSON.parse(jsonResponse).query(videoPath).asString());
|
||||
return Pair.of(true, JSON.parse(jsonResponse).query(videoPath).asString());
|
||||
} catch (JsonSyntaxException e) {
|
||||
e.printStackTrace();
|
||||
return Optional.empty();
|
||||
return Pair.of(false, "There was an error parsing YouTube's reply.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue