use of com.sx4.bot.entities.youtube.YouTubeVideo in project Sx4 by sx4-discord-bot.
the class YouTubeNotificationCommand method preview.
@Command(value = "preview", description = "Previews your YouTube notification message")
@CommandId(465)
@Examples({ "youtube notification preview 5e45ce6d3688b30ee75201ae" })
public void preview(Sx4CommandEvent event, @Argument(value = "id") ObjectId id) {
Document data = event.getMongo().getYouTubeNotification(Filters.and(Filters.eq("_id", id), Filters.eq("guildId", event.getGuild().getIdLong())), Projections.include("uploaderId", "message"));
if (data == null) {
event.replyFailure("I could not find that notification").queue();
return;
}
String channelId = data.getString("uploaderId");
Request request = new Request.Builder().url("https://www.youtube.com/feeds/videos.xml?channel_id=" + channelId).build();
event.getHttpClient().newCall(request).enqueue((HttpCallback) response -> {
if (response.code() == 404) {
event.replyFailure("The YouTube channel for this notification no longer exists").queue();
return;
}
JSONObject channel = XML.toJSONObject(response.body().string());
JSONObject feed = channel.getJSONObject("feed");
JSONArray entries = feed.optJSONArray("entry");
YouTubeVideo video;
if (entries == null || entries.isEmpty()) {
OffsetDateTime now = OffsetDateTime.now(ZoneOffset.UTC);
video = new YouTubeVideo("dQw4w9WgXcQ", "This channel had no uploads", now, now);
} else {
JSONObject entry = entries.getJSONObject(0);
video = new YouTubeVideo(entry.getString("yt:videoId"), entry.getString("title"), entry.getString("updated"), entry.getString("published"));
}
String channelName = feed.getString("title");
Document message = new JsonFormatter(data.get("message", YouTubeManager.DEFAULT_MESSAGE)).addVariable("video", video).addVariable("channel", new YouTubeChannel(channelId, channelName)).parse();
try {
MessageUtility.fromWebhookMessage(event.getTextChannel(), MessageUtility.fromJson(message).build()).queue();
} catch (IllegalArgumentException e) {
event.replyFailure(e.getMessage()).queue();
}
});
}
use of com.sx4.bot.entities.youtube.YouTubeVideo in project Sx4 by sx4-discord-bot.
the class YouTubeEndpoint method postYouTube.
@POST
@Path("youtube")
public Response postYouTube(final String body) {
JSONObject json = XML.toJSONObject(body);
JSONObject feed = json.getJSONObject("feed");
if (feed.has("at:deleted-entry")) {
JSONObject entry = feed.getJSONObject("at:deleted-entry");
JSONObject channel = entry.getJSONObject("at:by");
String videoId = entry.getString("ref").substring(9), videoDeletedAt = entry.getString("when");
String channelId = channel.getString("uri").substring(32), channelName = channel.getString("name");
this.bot.getYouTubeManager().onYouTube(new YouTubeDeleteEvent(new YouTubeChannel(channelId, channelName), videoId, videoDeletedAt));
} else {
JSONObject entry = feed.getJSONObject("entry");
String videoTitle = entry.getString("title"), videoId = entry.getString("yt:videoId"), videoUpdatedAt = entry.getString("updated"), videoPublishedAt = entry.getString("published");
String channelId = entry.getString("yt:channelId"), channelName = entry.getJSONObject("author").getString("name");
YouTubeChannel channel = new YouTubeChannel(channelId, channelName);
YouTubeVideo video = new YouTubeVideo(videoId, videoTitle, videoUpdatedAt, videoPublishedAt);
Document data = this.bot.getMongo().getYouTubeNotificationLog(Filters.eq("videoId", videoId), Projections.include("title"));
String oldTitle = data == null ? null : data.getString("title");
if (data == null && Duration.between(video.getPublishedAt(), OffsetDateTime.now(ZoneOffset.UTC)).toDays() <= 1) {
this.bot.getYouTubeManager().onYouTube(new YouTubeUploadEvent(channel, video));
} else if (data != null && oldTitle.equals(videoTitle)) {
this.bot.getYouTubeManager().onYouTube(new YouTubeUpdateEvent(channel, video));
} else {
this.bot.getYouTubeManager().onYouTube(new YouTubeUpdateTitleEvent(channel, video, oldTitle));
}
}
return Response.status(204).build();
}
Aggregations