예제 #1
0
        private async Task MainThread()
        {
            while (!Exit)
            {
                var context = new DiscordContext();
                foreach (var platform in context.Joiplatform)
                {
                    foreach (var link in context.CreatorPlatformLink)
                    {
                        Video newestVideo = null;

                        switch (platform.BaseUrl)
                        {
                        case "https://www.pornhub.com":
                            newestVideo = await GetNewestPornhubVideo(link.Identification);

                            break;
                        }

                        if (newestVideo == null || context.IndexedVideo.Any(iv => iv.Identification == newestVideo.ID))
                        {
                            await Task.Delay(3000);

                            continue;
                        }

                        var creator = await context.Creator.FindAsync(link.CreatorId);

                        var discordUser = client.GetUserAsync(Convert.ToUInt64(creator.DiscordUserId.Value));

                        IndexedVideo indexedVideo = new IndexedVideo()
                        {
                            CreationDate   = DateTime.Now,
                            CreatorId      = creator.Id,
                            Identification = newestVideo.ID,
                            Link           = newestVideo.Url,
                            PlatformId     = platform.Id
                        };

                        await context.IndexedVideo.AddAsync(indexedVideo);

                        DiscordEmbedBuilder builder = new DiscordEmbedBuilder()
                        {
                            Color        = DiscordColor.Gold,
                            Title        = $"{newestVideo.Creator} has uploaded a new video!",
                            Url          = newestVideo.Url,
                            ThumbnailUrl = newestVideo.ImageUrl
                        };

                        builder.AddField("Title", newestVideo.Title);
                        if (creator.DiscordUserId != null)
                        {
                            builder.AddField("Creator", (await discordUser).Mention);
                        }

                        var embed = builder.Build();

                        await context.SaveChangesAsync();

                        foreach (var updateChannelId in context.SabrinaSettings.Where(ss => ss.ContentChannel != null).Select(ss => ss.ContentChannel))
                        {
                            var updateChannel = await client.GetChannelAsync(Convert.ToUInt64(updateChannelId));

                            await client.SendMessageAsync(updateChannel, embed : embed);
                        }
                    }
                }
                await Task.Delay(120000);
            }
        }
예제 #2
0
        private async Task MainThread()
        {
            using var context = new DiscordContext();

            while (!_cancellationToken.IsCancellationRequested)
            {
                foreach (var platform in context.Joiplatform.ToArray())
                {
                    foreach (var link in context.CreatorPlatformLink.ToArray())
                    {
                        IEnumerable <string> newestVideos = null;

                        switch (platform.BaseUrl)
                        {
                        case "https://www.pornhub.com":
                            newestVideos = await GetNewestPornhubVideoIds(link.Identification, _cancellationToken);

                            break;
                        }

                        foreach (var newestVideo in newestVideos)
                        {
                            if (newestVideo == null || context.IndexedVideo.Any(iv => iv.Identification == newestVideo))
                            {
                                continue;
                            }

                            await Task.Delay(2000, _cancellationToken);

                            var fullVideo = await Video.FromPornhubId(newestVideo);

                            var creator = await context.Creator.FindAsync(keyValues : new object[] { link.CreatorId }, cancellationToken : _cancellationToken);

                            DiscordUser discordUser = null;

                            if (creator.DiscordUserId != null)
                            {
                                try
                                {
                                    discordUser = await _client.GetUserAsync(Convert.ToUInt64(creator.DiscordUserId.Value));
                                }
                                catch (Exception)
                                { }
                            }

                            IndexedVideo indexedVideo = new IndexedVideo()
                            {
                                CreationDate   = DateTime.Now,
                                CreatorId      = creator.Id,
                                Identification = fullVideo.ID,
                                Link           = fullVideo.Url,
                                PlatformId     = platform.Id
                            };

                            DiscordEmbedBuilder builder = new DiscordEmbedBuilder()
                            {
                                Color        = DiscordColor.Gold,
                                Title        = $"{fullVideo.Creator} has uploaded a new video!",
                                Url          = fullVideo.Url,
                                ImageUrl     = fullVideo.ImageUrl,
                                ThumbnailUrl = _logoUrl
                            };

                            builder.AddField("Title", fullVideo.Title);
                            if (creator.DiscordUserId != null && discordUser != null)
                            {
                                builder.AddField("Creator", discordUser.Mention);
                            }

                            builder.AddField("Link", fullVideo.Url);

                            var embed = builder.Build();

                            foreach (var updateChannelId in context.SabrinaSettings.Where(ss => ss.ContentChannel != null).Select(ss => ss.ContentChannel))
                            {
                                if (_client.Guilds.Any(g => g.Value.Channels.Any(c => c.Key == Convert.ToUInt64(updateChannelId))))
                                {
                                    var updateChannel = await _client.GetChannelAsync(Convert.ToUInt64(updateChannelId));

                                    await _client.SendMessageAsync(updateChannel, embed : embed);
                                }
                            }

                            await context.IndexedVideo.AddAsync(indexedVideo, _cancellationToken);
                        }

                        await context.SaveChangesAsync(_cancellationToken);

                        await Task.Delay(3000, _cancellationToken);
                    }
                    await Task.Delay(3000, _cancellationToken);
                }

                await Task.Delay(120000, _cancellationToken);
            }
        }