Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import de.slimecloud.slimeball.main.SlimeBot;
import de.slimecloud.slimeball.util.MathUtil;
import lombok.Cleanup;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.Request;
Expand All @@ -27,6 +28,7 @@ public class Youtube {

private final OkHttpClient client = new OkHttpClient().newBuilder().build();

@Getter
private final String[] keys;
private final SlimeBot bot;
private final YoutubeConfig config;
Expand All @@ -46,23 +48,23 @@ public void startListener() {
channels.forEach((channel, delay) -> {
bot.getExecutor().scheduleAtFixedRate(() -> {
try {
check(channel);
check(channel, currentKey);
} catch (Exception e) {
logger.error("Failed to check for new video for channel " + channel, e);
}
}, 0, delay, TimeUnit.SECONDS);
});
}

private void check(String youtubeChannelId) throws IOException {
Set<Video> videos = getLastVideo(youtubeChannelId, 5);
public void check(String youtubeChannelId, int startKey) throws IOException {
Set<Video> videos = getLastVideo(youtubeChannelId, 5, startKey);

Collection<String> known = bot.getIdMemory().getMemory("youtube");
List<String> newIds = new ArrayList<>();

for (Video video : videos) {
if (!known.contains(video.id())) {
new YoutubeVideoEvent(youtubeChannelId, video).callEvent();
new YoutubeVideoEvent(this, youtubeChannelId, video).callEvent();
newIds.add(video.id());
}
}
Expand All @@ -76,7 +78,7 @@ private String getNextKey() {
}

@NotNull
public Set<Video> getLastVideo(@NotNull String youtubeChannelId, int limit) throws IOException {
public Set<Video> getLastVideo(@NotNull String youtubeChannelId, int limit, int startKey) throws IOException {
Request request = new Request.Builder()
.url(String.format("https://www.googleapis.com/youtube/v3/search?key=%s&channelId=%s&part=snippet,id&order=date&maxResults=" + limit, getNextKey(), youtubeChannelId))
.get()
Expand All @@ -89,7 +91,7 @@ public Set<Video> getLastVideo(@NotNull String youtubeChannelId, int limit) thro
JsonObject json = JsonParser.parseString(response.body().string()).getAsJsonObject();

if (code != HttpStatus.SC_OK) {
YoutubeApiErrorEvent event = new YoutubeApiErrorEvent(response, json, new HashSet<>());
YoutubeApiErrorEvent event = new YoutubeApiErrorEvent(this, youtubeChannelId, startKey, response, json, new HashSet<>());
event.callEvent();
return event.getVideos();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
@RequiredArgsConstructor
public class YoutubeApiErrorEvent extends Event {

private final Youtube youtube;
private final String channelId;
private final int startKey;
private final Response response;
private final JsonObject jsonResponse;
private final Set<Video> videos;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,22 @@ public class YoutubeListener {
public void onUpload(YoutubeVideoEvent event) {
logger.info("Video Uploaded: {}", event.getVideo());
bot.getJda().getGuilds().forEach(g -> bot.loadGuild(g).getYoutube().ifPresent(config ->
config.getChannel(event.getYoutubeChannelId()).ifPresentOrElse(channel -> {
String msg = event.isLive() ? config.getLiveMessage().get(event.getYoutubeChannelId()) : config.getVideoMessage().get(event.getYoutubeChannelId());
config.getChannel(event.getChannelId()).ifPresentOrElse(channel -> {
String msg = event.isLive() ? config.getLiveMessage().get(event.getChannelId()) : config.getVideoMessage().get(event.getChannelId());

channel.sendMessage(msg
.replace("%role%", config.getRole(event.getYoutubeChannelId()).map(IMentionable::getAsMention).orElse(""))
.replace("%role%", config.getRole(event.getChannelId()).map(IMentionable::getAsMention).orElse(""))
.replace("%uploader%", event.getVideo().getChannel().getTitle())
.replace("%url%", event.getVideo().getUrl())
.replace("%title%", event.getVideo().snippet().title())
).queue();
}, () -> logger.warn("Cannot send Youtube Notification because channel {} not found", config.getChannelId(event.getYoutubeChannelId()).orElse(null)))
}, () -> logger.warn("Cannot send Youtube Notification because channel {} not found", config.getChannelId(event.getChannelId()).orElse(null)))
));
}

@EventHandler
public void onApiError(YoutubeApiErrorEvent event) {

logger.warn("Youtube API error {} '{}'", event.getCode(), event.getJsonResponse().getAsJsonObject("error").get("message").getAsString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
@RequiredArgsConstructor
public class YoutubeVideoEvent extends Event {

private final String youtubeChannelId;
private final Youtube youtube;
private final String channelId;
private final Video video;

public boolean isLive() {
Expand Down