Example #1
0
        private Boyan ProcessUrlBoyan(Message message)
        {
            HelperMethods.IsUrl(message.Text, out var url);
            var uri = new Uri(url.TrimEnd('/', '?'));

            string cleanUrl;
            if (uri.Host.Contains("youtube.com"))
            {
                cleanUrl = uri.Host.Replace("www.", "") + uri.PathAndQuery;
            }
            else
            {
                cleanUrl = uri.Host.Replace("www.", "") + uri.AbsolutePath;
            }

            var boyan = Boyans.Items.Where(x => x.Url != null).OrderByDescending(x => x.DateCreated).FirstOrDefault(x => x.Url.Contains(cleanUrl));

            if(boyan == null)
            {
                var newBoyan = new Boyan { Url = cleanUrl, MessageId = message.MessageId.ToString(), ChatId = message.Chat.Id.ToString(), DateCreated = DateTimeOffset.UtcNow, AuthorId = message.From.Id.ToString() };
                Boyans.Items.Add(newBoyan);
            }

            return boyan;
        }
Example #2
0
        private async Task <string> BanImageBoyan(Message message)
        {
            var isBanning = IsBanning(message.Caption, message);

            if (isBanning == false)
            {
                return(null);
            }

            var biggestPhoto = message.Photo.OrderByDescending(x => x.FileSize).FirstOrDefault();

            var tempFolderPath = Path.Combine(AppContext.BaseDirectory, "Tmp");

            if (Directory.Exists(tempFolderPath) == false)
            {
                Directory.CreateDirectory(tempFolderPath);
            }

            var filePath = Path.Combine(tempFolderPath, $"{Guid.NewGuid()}.jpg");

            using (var stream = System.IO.File.Create(filePath))
            {
                var file = await Bot.GetInfoAndDownloadFileAsync(biggestPhoto.FileId, stream);
            }

            var hash = new ImgHash();

            hash.GenerateFromPath(filePath);

            Boyan boyan = null;

            foreach (var imageBoyan in Boyans.Items.Where(x => x.ImageHash != null))
            {
                var similarity = hash.CompareWith(imageBoyan.ImageHash);
                if (similarity >= 99)
                {
                    boyan = imageBoyan;
                    break;
                }
            }

            string reply;

            if (boyan != null)
            {
                boyan.IsBanned = true;
                reply          = "Небоян забанен!";
            }
            else
            {
                reply = "Боян не найден!";
            }

            return(reply);
        }
Example #3
0
        private async Task<Boyan> ProcessImageBoyan(Message message)
        {
            var biggestPhoto = message.Photo.OrderByDescending(x => x.FileSize).FirstOrDefault();

            var tempFolderPath = Path.Combine(AppContext.BaseDirectory, "Tmp");
            if(Directory.Exists(tempFolderPath) == false)
                Directory.CreateDirectory(tempFolderPath);

            var filePath = Path.Combine(tempFolderPath, $"{Guid.NewGuid()}.jpg");
            using (var stream = File.Create(filePath))
            {
                var file = await Bot.GetInfoAndDownloadFileAsync(biggestPhoto.FileId, stream);
            }

            var hash = new ImgHash();
            hash.GenerateFromPath(filePath);

            Boyan boyan = null;
            foreach (var imageBoyan in Boyans.Items.Where(x => x.ImageHash != null))
            {
                var similarity = hash.CompareWith(imageBoyan.ImageHash);
                if (similarity >= 99)
                {
                    boyan = imageBoyan;
                    break;
                }
            }

            if(boyan == null)
            {
                var newBoyan = new Boyan { ImageHash = hash.HashData, MessageId = message.MessageId.ToString(), ChatId = message.Chat.Id.ToString(), DateCreated = DateTimeOffset.UtcNow };
                Boyans.Items.Add(newBoyan);
            }

            return boyan;
        }