Search in sources :

Example 1 with YouTubeUploadEvent

use of com.sx4.bot.events.youtube.YouTubeUploadEvent 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();
}
Also used : JSONObject(org.json.JSONObject) YouTubeChannel(com.sx4.bot.entities.youtube.YouTubeChannel) YouTubeUpdateTitleEvent(com.sx4.bot.events.youtube.YouTubeUpdateTitleEvent) YouTubeVideo(com.sx4.bot.entities.youtube.YouTubeVideo) Document(org.bson.Document) YouTubeUploadEvent(com.sx4.bot.events.youtube.YouTubeUploadEvent) YouTubeUpdateEvent(com.sx4.bot.events.youtube.YouTubeUpdateEvent) YouTubeDeleteEvent(com.sx4.bot.events.youtube.YouTubeDeleteEvent)

Aggregations

YouTubeChannel (com.sx4.bot.entities.youtube.YouTubeChannel)1 YouTubeVideo (com.sx4.bot.entities.youtube.YouTubeVideo)1 YouTubeDeleteEvent (com.sx4.bot.events.youtube.YouTubeDeleteEvent)1 YouTubeUpdateEvent (com.sx4.bot.events.youtube.YouTubeUpdateEvent)1 YouTubeUpdateTitleEvent (com.sx4.bot.events.youtube.YouTubeUpdateTitleEvent)1 YouTubeUploadEvent (com.sx4.bot.events.youtube.YouTubeUploadEvent)1 Document (org.bson.Document)1 JSONObject (org.json.JSONObject)1