mirror of
https://github.com/JonasunderscoreJones/ZtereoMUSIC.git
synced 2025-10-25 13:39:19 +02:00
NEW: PLAY COMMAND SEARCHES ON YOUTUBE FOR STUFF!
This commit is contained in:
parent
02d72e2514
commit
cc0db55b36
2 changed files with 44 additions and 5 deletions
|
|
@ -5,6 +5,7 @@ import codes.ztereohype.ztereomusic.audio.TrackManager;
|
||||||
import codes.ztereohype.ztereomusic.audio.TrackManagers;
|
import codes.ztereohype.ztereomusic.audio.TrackManagers;
|
||||||
import codes.ztereohype.ztereomusic.command.Command;
|
import codes.ztereohype.ztereomusic.command.Command;
|
||||||
import codes.ztereohype.ztereomusic.command.CommandMeta;
|
import codes.ztereohype.ztereomusic.command.CommandMeta;
|
||||||
|
import codes.ztereohype.ztereomusic.networking.YoutubeSearch;
|
||||||
import com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler;
|
import com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler;
|
||||||
import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager;
|
import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager;
|
||||||
import com.sedmelluq.discord.lavaplayer.tools.FriendlyException;
|
import com.sedmelluq.discord.lavaplayer.tools.FriendlyException;
|
||||||
|
|
@ -14,6 +15,7 @@ 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.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
@ -52,11 +54,16 @@ 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
|
// youtube api shit
|
||||||
messageEvent.getMessage().reply("please send a youtube link").queue();
|
try {
|
||||||
return;
|
identifier = YoutubeSearch.getVideoUrl(mergedArgs);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
identifier = "error";
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// set identifier to the parsed url
|
// set identifier to the parsed url
|
||||||
identifier = mergedArgs;
|
identifier = mergedArgs;
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,41 @@
|
||||||
package codes.ztereohype.ztereomusic.networking;
|
package codes.ztereohype.ztereomusic.networking;
|
||||||
|
|
||||||
|
import codes.ztereohype.ztereomusic.ZtereoMUSIC;
|
||||||
|
import net.shadew.json.Json;
|
||||||
|
import net.shadew.json.JsonNode;
|
||||||
|
import net.shadew.json.JsonPath;
|
||||||
|
import net.shadew.json.JsonSyntaxException;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
public class YoutubeSearch {
|
public class YoutubeSearch {
|
||||||
public String getVideoUrl(String title) throws IOException {
|
public static String getVideoUrl(String title) throws IOException {
|
||||||
return "WIP";
|
String apiKey = ZtereoMUSIC.getInstance().getConfig().getPropreties().get("yt_api_key");
|
||||||
|
String query;
|
||||||
|
|
||||||
|
title = (title.contains(" ")) ? title.replace(" ","+") : title;
|
||||||
|
query = "https://www.googleapis.com/youtube/v3/search?part=snippet&q=%22"+title+"%22&type=video&key="+apiKey;
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
try {
|
||||||
|
parsedResponse = json.parse(jsonResponse);
|
||||||
|
videoId = parsedResponse.query(path).asString();
|
||||||
|
} catch (JsonSyntaxException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
videoId = "error";
|
||||||
|
}
|
||||||
|
|
||||||
|
return videoId;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue