Exemple #1
0
        private async Task NewVideo(RabbotContext db, YouTubeVideoDto video)
        {
            foreach (var dbGuild in db.Guilds.AsQueryable().Where(p => p.GuildId == 432908323042623508))
            {
                try
                {
                    if (dbGuild.StreamChannelId == null)
                    {
                        continue;
                    }

                    var guild = _client.Guilds.FirstOrDefault(p => p.Id == dbGuild.GuildId);
                    if (guild == null)
                    {
                        continue;
                    }
                    if (!(guild.Channels.FirstOrDefault(p => p.Id == dbGuild.StreamChannelId) is SocketTextChannel channel))
                    {
                        continue;
                    }

                    var youTubeUrl = $"https://www.youtube.com/watch?v={video.Id}";
                    await channel.SendMessageAsync($"**{video.ChannelName}** hat ein neues Video hochgeladen! {guild.EveryoneRole.Mention}\n{youTubeUrl}");
                }
                catch (Exception ex)
                {
                    _logger.Error(ex, $"Error while sending YouTube notification");
                }
            }
        }
 public YouTubeVideoDtoBuilder()
 {
     _youTubeVideoDto = WithDefaults();
 }
Exemple #3
0
        private async Task CheckLastVideo(List <string> channelIds, int intervallTime)
        {
            while (true)
            {
                try
                {
                    await Task.Delay(intervallTime * 1000);

                    foreach (var channelId in channelIds)
                    {
                        YouTubeVideoDto video = null;
                        using (XmlReader reader = XmlReader.Create($"https://www.youtube.com/feeds/videos.xml?channel_id={channelId}"))
                        {
                            bool skip = false;
                            using (XmlReader descriptionReader = XmlReader.Create($"https://www.youtube.com/feeds/videos.xml?channel_id={channelId}"))
                            {
                                while (descriptionReader.Read())
                                {
                                    if (descriptionReader.NodeType == XmlNodeType.Element)
                                    {
                                        if (descriptionReader.Name == "media:description")
                                        {
                                            XElement el          = XNode.ReadFrom(descriptionReader) as XElement;
                                            var      description = el.FirstNode.ToString();

                                            if (description.Contains(Constants.AnnouncementIgnoreTag))
                                            {
                                                skip = true;
                                            }
                                        }
                                    }
                                }
                            }

                            video = SyndicationFeed.Load(reader).GetFirstVideo();

                            if (skip)
                            {
                                continue;
                            }
                        }
                        if (video != null)
                        {
                            using (var db = Database.Open())
                            {
                                var dbVideo = db.YouTubeVideos.FirstOrDefault(p => p.VideoId == video.Id);
                                if (dbVideo == null)
                                {
                                    await db.YouTubeVideos.AddAsync(new YouTubeVideoEntity { VideoId = video.Id, VideoTitle = video.Title });

                                    await db.SaveChangesAsync();
                                    await NewVideo(db, video);
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    _logger.Error(e, $"Error while checking last YouTube video");
                }
            }
        }