Exemple #1
0
        private async Task SyncChats(long idGroup)
        {
            await Start(idGroup, SyncType.Chats);

            var vkApi = await _vkPoolService.GetGroupVkApi(idGroup);

            ulong offset = 0, count = 100;

            do
            {
                var conversationsInfo = await vkApi.Messages.GetConversationsAsync(new VkNet.Model.RequestParams.GetConversationsParams()
                {
                    Count    = count,
                    Offset   = offset,
                    Extended = true,
                    Fields   = new string[] { "bdate", "city", "country", "domain", "photo_50", "sex", "blacklisted" }
                });// VkConnector.Methods.Messages.GetConversations(groupAccessToken, idGroup, "bdate,city,country,domain,photo_50,sex,blacklisted", offset, count);

                if (conversationsInfo == null)
                {
                    System.Threading.Thread.Sleep(1000);
                    continue;
                }

                await Progress(idGroup, SyncType.Chats, (ulong)conversationsInfo.Count, offset);

                foreach (var vkMember in conversationsInfo.Profiles)
                {
                    using (var transaction = await _context.Database.BeginTransactionAsync())
                    {
                        try
                        {
                            var vkUser = await _context.VkUsers.FirstOrDefaultAsync(x => x.IdVk == vkMember.Id);

                            if (vkUser == null)
                            {
                                vkUser = VkUsers.FromUser(vkMember);
                                await _context.AddAsync(vkUser);
                            }
                            else
                            {
                                vkUser.Update(vkMember);
                            }
                            await _context.SaveChangesAsync();

                            var subscriber = await _context.Subscribers
                                             .Where(x => x.IdGroup == idGroup)
                                             .FirstOrDefaultAsync(x => x.IdVkUser == vkMember.Id);

                            if (subscriber == null)
                            {
                                subscriber = new Subscribers()
                                {
                                    IdGroup   = idGroup,
                                    IdVkUser  = vkMember.Id,
                                    IsBlocked = vkMember.Blacklisted,
                                    DtAdd     = DateTime.UtcNow
                                };
                                await _context.AddAsync(subscriber);
                            }
                            subscriber.IsChatAllowed = conversationsInfo.Items
                                                       .Where(x => x.Conversation != null && x.Conversation.Peer != null && x.Conversation.Peer.Id == vkMember.Id)
                                                       .Select(x => x.Conversation?.CanWrite?.Allowed)
                                                       .FirstOrDefault();

                            await _context.SaveChangesAsync();

                            transaction.Commit();
                        }
                        catch
                        {
                            transaction.Rollback();
                        }
                    }
                }

                offset += count;
                if (offset >= (ulong)conversationsInfo.Count)
                {
                    break;
                }

                await Progress(idGroup, SyncType.Chats, (ulong)conversationsInfo.Count, offset);
            } while (true);
            await Finish(idGroup, SyncType.Chats);
        }
Exemple #2
0
        public static async Task <Subscribers> CreateSubscriber(DatabaseContext _context, VkPoolService _vkPoolService, long idGroup, long?idVkUser)
        {
            if (!idVkUser.HasValue || idVkUser.Value <= 0)
            {
                return(null);
            }
            var subscriber = await _context.Subscribers.FirstOrDefaultAsync(x => x.IdGroup == idGroup && x.IdVkUser == idVkUser.Value);

            if (subscriber != null)
            {
                return(subscriber);
            }

            var vkUser = await _context.VkUsers.FirstOrDefaultAsync(x => x.IdVk == idVkUser.Value);

            if (vkUser == null)
            {
                try
                {
                    var vkApi = await _vkPoolService.GetGroupVkApi(idGroup);

                    if (vkApi == null)
                    {
                        return(null);
                    }

                    var users = await vkApi.Users.GetAsync(new long[] { idVkUser.Value }, VkNet.Enums.Filters.ProfileFields.BirthDate |
                                                           VkNet.Enums.Filters.ProfileFields.City |
                                                           VkNet.Enums.Filters.ProfileFields.Country |
                                                           VkNet.Enums.Filters.ProfileFields.FirstName |
                                                           VkNet.Enums.Filters.ProfileFields.LastName |
                                                           VkNet.Enums.Filters.ProfileFields.Nickname |
                                                           VkNet.Enums.Filters.ProfileFields.Domain |
                                                           VkNet.Enums.Filters.ProfileFields.Photo50 |
                                                           VkNet.Enums.Filters.ProfileFields.Photo400Orig |
                                                           VkNet.Enums.Filters.ProfileFields.Sex |
                                                           VkNet.Enums.Filters.ProfileFields.Blacklisted);

                    if (users == null || !users.Any())
                    {
                        return(null);
                    }

                    vkUser = VkUsers.FromUser(users.FirstOrDefault());
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Ошибка при запросе users.get с id={idVkUser}{Environment.NewLine}{ex.Message}{Environment.NewLine}{ex.StackTrace}");
                    return(null);
                }

                _context.VkUsers.Update(vkUser);
            }

            subscriber = new Subscribers()
            {
                IdVkUser = vkUser.IdVk,
                IdGroup  = idGroup,
                DtAdd    = DateTime.UtcNow
            };
            await _context.Subscribers.AddAsync(subscriber);

            await _context.SaveChangesAsync();

            return(subscriber);
        }
Exemple #3
0
        private async Task SyncSubscribers(long idGroup)
        {
            await Start(idGroup, SyncType.Subscribers);

            var vkApi = await _vkPoolService.GetGroupVkApi(idGroup);

            long offset = 0, count = 1000;

            do
            {
                var vkMembers = await vkApi.Groups.GetMembersAsync(new VkNet.Model.RequestParams.GroupsGetMembersParams()
                {
                    GroupId = idGroup.ToString(),
                    Count   = count,
                    Offset  = offset,
                    Fields  = UsersFields.BirthDate | UsersFields.City | UsersFields.Country | UsersFields.Domain | UsersFields.Photo50 | UsersFields.Photo400Orig | UsersFields.Sex | UsersFields.Sex,
                });

                await Progress(idGroup, SyncType.Subscribers, vkMembers.TotalCount, (ulong)offset);

                foreach (var vkMember in vkMembers)
                {
                    using (var transaction = await _context.Database.BeginTransactionAsync())
                    {
                        try
                        {
                            var vkUser = await _context.VkUsers.FirstOrDefaultAsync(x => x.IdVk == vkMember.Id);

                            if (vkUser == null)
                            {
                                vkUser = VkUsers.FromUser(vkMember);
                                await _context.VkUsers.AddAsync(vkUser);

                                await _context.SaveChangesAsync();
                            }
                            else
                            {
                                vkUser.Update(vkMember);
                            }

                            var subscriber = await _context.Subscribers
                                             .Where(x => x.IdGroup == idGroup)
                                             .FirstOrDefaultAsync(x => x.IdVkUser == vkMember.Id);

                            if (subscriber == null)
                            {
                                subscriber = new Subscribers()
                                {
                                    IdGroup   = idGroup,
                                    IdVkUser  = vkMember.Id,
                                    IsBlocked = vkMember.Blacklisted,
                                    DtAdd     = DateTime.UtcNow
                                };
                                await _context.Subscribers.AddAsync(subscriber);
                            }
                            subscriber.IsSubscribedToGroup = true;

                            await _context.SaveChangesAsync();

                            transaction.Commit();
                        }
                        catch
                        {
                            transaction.Rollback();
                        }
                    }
                }

                offset += count;
                if (offset >= vkMembers.Count)
                {
                    break;
                }

                await Progress(idGroup, SyncType.Subscribers, vkMembers.TotalCount, (ulong)offset);
            } while (true);
            await Finish(idGroup, SyncType.Subscribers);
        }
        // Main function
        private static void Main(string[] args)
        {
            // ------------------------------------------------------- //
            //      Request initialization (Uncomment wanted param)    //
            // ------------------------------------------------------- //
            var initParameters = new RequestParams();

            VkUsers.RequestParamsInit(initParameters);

            initParameters.City = 282;      // 282 - Minsk
                                            //initParameters.Country = 3;   // 3 - BY. All list here: https://vk.com/select_ajax.php?act=a_get_countries
                                            //initParameters.Sex = 1;       // 1 — женщина; 2 — мужчина;
                                            //initParameters.Status = 1;

            /*  1 — не женат (не замужем);
             *  2 — встречается;
             *  3 — помолвлен(-а);
             *  4 — женат (замужем);
             *  5 — всё сложно;
             *  6 — в активном поиске;
             *  7 — влюблен(-а).*/
            //initParameters.AgeFrom = 20;
            //initParameters.AgeTo = 25;
            //initParameters.Online = 1;
            //initParameters.Interests = "Drinking";
            //initParameters.Position = "CEO";


            Console.WriteLine("Please enter year from (ex. 1960): ");
            _yearFrom = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Please enter year to   (ex. 2002): ");
            _yearTo = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Please enter start delay (>300): ");
            _delayFrom = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Please enter max delay  (unlim): ");
            _delayTo = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Please enter number of city (ex. 282 it`s Minsk: ");
            initParameters.City = Convert.ToInt32(Console.ReadLine());

            //_yearFrom = 1970;
            //_yearTo = 2002;
            //_delayFrom = 1000;
            //_delayTo = 1800;
            initParameters.City = 282;

            Console.WriteLine("-----------------------------:-------------------------------------------------");
            Console.WriteLine("Qty names in dictionaries    : {0}", Utils.GetUsersNames().Length);
            Console.WriteLine("Qty of tokens                : {0}\n", File.ReadAllLines(ParseMethods.TokensFile).Length);
            Console.WriteLine("Parsing Name                 : {0}", initParameters.Name);
            Console.WriteLine("Parsing Count                : {0}", initParameters.Count);
            Console.WriteLine("Parsing City                 : {0}", initParameters.City);
            Console.WriteLine("Parsing Country              : {0}", initParameters.Country);
            Console.WriteLine("Parsing Sex                  : {0}", initParameters.Sex);
            Console.WriteLine("Parsing Status               : {0}", initParameters.Status);
            Console.WriteLine("Parsing Age from             : {0}", initParameters.AgeFrom);
            Console.WriteLine("Parsing Age to               : {0}", initParameters.AgeTo);
            Console.WriteLine("Parsing Online status        : {0}", initParameters.Status);
            Console.WriteLine("Parsing Interests            : {0}", initParameters.Interests);
            Console.WriteLine("Parsing Position             : {0}\n", initParameters.Position);
            Console.WriteLine("Parsing Year from            : {0}", _yearFrom);
            Console.WriteLine("Parsing Year to              : {0}", _yearTo);
            Console.WriteLine("Parsing Delay from           : {0}", _delayFrom);
            Console.WriteLine("Parsing Delay to             : {0}", _delayTo);
            Console.WriteLine("-----------------------------:-------------------------------------------------\n");

            Console.WriteLine("Please press Enter to start parsing");
            Console.ReadKey();

            if (File.Exists(OutputFile + "_" + initParameters.City + ".txt") == false)
            {
                File.WriteAllText(OutputFile + "_" + initParameters.City + ".txt", string.Format("Parsing started at: {0}\n", DateTime.Now));
            }

            // ------------------------------------------------------- //
            //     Methods initialization (Uncomment wanted method)    //
            // ------------------------------------------------------- //
            //ParseMethods.ParsingUsersByBdayDirect(out _usersFound, out _parsingStatus, initParameters, YearFrom, YearTo, DelayFrom, DelayTo, false, true, OutputFile);
            //ParseMethods.ParsingUsersByBdayRandom(out _usersFound, out _parsingStatus, initParameters, YearFrom, YearTo, DelayFrom, DelayTo, false, true, OutputFile);
            ParseMethods.ParsingUsersByBdayFullRandom(out _usersFound, out _parsingStatus, initParameters, _yearFrom, _yearTo, _delayFrom, _delayTo, false, OutputFile);
            //ParseMethods.ParsingUsersByDictionaryName(out _usersFound, out _parsingStatus, initParameters, 20, 25, DelayFrom, DelayTo, false, OutputFile);
            //ParseMethods.ParsingUsersByExecuteRandom(out _usersFound, out _parsingStatus, initParameters, _yearFrom, _yearTo, _delayFrom, _delayTo, OutputFile);

            Console.WriteLine("Qty of founded users: {0}", _usersFound);
            Console.WriteLine("-------------------------------------\n");
            Console.WriteLine(_parsingStatus);
            Console.WriteLine("Press any key to exit...");

            if (File.Exists(string.Format(OutputFile + "_" + initParameters.City + ".txt")))
            {
                File.Copy(string.Format(OutputFile + "_" + initParameters.City + ".txt"),
                          string.Format(OutputFile + DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Hour +
                                        DateTime.Now.Minute + "_" + initParameters.City + "_finished.txt"));
                File.Delete(string.Format(OutputFile + "_" + initParameters.City + ".txt"));
            }

            Console.ReadKey();
        }