Add support for Youtube Music links

This commit is contained in:
anthorob committed 2023-05-24 13:23:04 -04:00
1 parent b2d1daf9c3
commit b4fe1c7b73
2 files changed
+15 -5

No files matched your search

@@ -14,6 +14,7 @@ public class SourceFinder {
public enum AudioSourceType { public enum AudioSourceType {
Youtube, Youtube,
YTMusic,
Soundcloud, Soundcloud,
Bandcamp, Bandcamp,
Vimeo, Vimeo,
@@ -22,17 +23,22 @@ public class SourceFinder {
} }
private static final Pattern youtubePattern = Pattern.compile("^(?:https?://)?(?:(?:www\\.)?)?(?:youtube\\.com|youtu\\.be)/.*?(?:embed|e|v|watch.*?v=)?/?([-_a-z0-9]{10,})?(?:&?index=\\d+)?(?>(?:playlist\\?|&)?list=([^#\\\\&?]{12,}))?", Pattern.CASE_INSENSITIVE); private static final Pattern youtubePattern = Pattern.compile("^(?:https?://)?(?:www\\.)?(?:youtube\\.com|youtu\\.be)/.*?(?:embed|e|v|watch.*?v=)?/?([-_a-zA-Z0-9]{10,})?(?:&?index=\\d+)?(?>(?:playlist\\?|&)?list=([^#\\\\&?]{12,}))?", Pattern.CASE_INSENSITIVE);
private static final Pattern youtubeMusicPattern = Pattern.compile("^(?:https?://)?(?:www\\.)?music\\.(?:youtube\\.com|youtu\\.be)/.*?(?:embed|e|v|watch.*?v=)?/?([-_a-zA-Z0-9]{10,})?(?:&?index=\\d+)?(?>(?:playlist\\?|&)?list=([^#\\\\&?]{12,}))?", Pattern.CASE_INSENSITIVE);
private static final Pattern soundcloudPattern = Pattern.compile("^(?:https?://)?(?:www\\.)?(?:m\\.)?soundcloud\\.com/([a-zA-Z0-9-_]+)/([a-zA-Z0-9-_]+)(?:\\\\?.*|)$", Pattern.CASE_INSENSITIVE); private static final Pattern soundcloudPattern = Pattern.compile("^(?:https?://)?(?:www\\.)?(?:m\\.)?soundcloud\\.com/([a-zA-Z0-9-_]+)/([a-zA-Z0-9-_]+)(?:\\\\?.*|)$", Pattern.CASE_INSENSITIVE);
private static Matcher twitchMatcher; private static Matcher twitchMatcher;
public static AudioSourceType getSource(String url) { public static AudioSourceType getSource(String url) {
Matcher youtubeMatcher = youtubePattern.matcher(url); Matcher youtubeMatcher = youtubePattern.matcher(url);
Matcher youtubeMusicMatcher = youtubeMusicPattern.matcher(url);
Matcher soundcloudMatcher = soundcloudPattern.matcher(url); Matcher soundcloudMatcher = soundcloudPattern.matcher(url);
if (youtubeMatcher.find()) if (youtubeMatcher.find())
return AudioSourceType.Youtube; return AudioSourceType.Youtube;
if (youtubeMusicMatcher.find())
return AudioSourceType.YTMusic;
if (soundcloudMatcher.find()) if (soundcloudMatcher.find())
return AudioSourceType.Soundcloud; return AudioSourceType.Soundcloud;
@@ -46,7 +52,7 @@ public class SourceFinder {
String identifier; String identifier;
identifier = switch (getSource(url)) { identifier = switch (getSource(url)) {
case Youtube, Soundcloud -> url; case Youtube, YTMusic, Soundcloud -> url;
case YTSearch -> "ytsearch:" + url; case YTSearch -> "ytsearch:" + url;
default -> null; default -> null;
}; };
@@ -83,7 +83,7 @@ public class MessageFunction {
if (type == SourceFinder.AudioSourceType.Youtube || type == SourceFinder.AudioSourceType.YTSearch) { if (type == SourceFinder.AudioSourceType.Youtube || type == SourceFinder.AudioSourceType.YTSearch) {
url = String.format("https://youtu.be/%s", track.getIdentifier()); url = String.format("https://youtu.be/%s", track.getIdentifier());
thumbnail = String.format("https://img.youtube.com/vi/%s/default.jpg", track.getIdentifier()); thumbnail = String.format("https://i3.ytimg.com/vi/%s/hqdefault.jpg", track.getIdentifier());
} }
if (thumbnail != null) { if (thumbnail != null) {
@@ -118,8 +118,12 @@ public class MessageFunction {
String thumbnail = null; String thumbnail = null;
if (type == SourceFinder.AudioSourceType.Youtube || type == SourceFinder.AudioSourceType.YTSearch) { if (type == SourceFinder.AudioSourceType.Youtube || type == SourceFinder.AudioSourceType.YTSearch) {
url = String.format("https://youtu.be/%s", playlist.getSelectedTrack().getIdentifier()); url = String.format("https://youtu.be/%s", playlist.getTracks().get(0).getIdentifier());
thumbnail = String.format("https://img.youtube.com/vi/%s/default.jpg", playlist.getSelectedTrack().getIdentifier()); thumbnail = String.format("https://i3.ytimg.com/vi/%s/hqdefault.jpg", playlist.getTracks().get(0).getIdentifier());
}
if (type == SourceFinder.AudioSourceType.YTMusic) {
thumbnail = String.format("https://i3.ytimg.com/vi/%s/hqdefault.jpg", playlist.getTracks().get(0).getIdentifier());
} }
if (thumbnail != null) { if (thumbnail != null) {