Example #1
0
        public static DbManger GetInstance()
        {
            if (s_instance == null)
            {
                s_instance = new DbManger();
            }

            return s_instance;
        }
Example #2
0
        private static void Main(string[] args)
        {
            Console.CancelKeyPress += ConsoleOnCancelKeyPress;
            Console.CursorVisible = false;
            Console.Title = "VK";

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            log4net.Config.XmlConfigurator.Configure();

            Status("Загрузка...");
            try
            {
                s_db = DbManger.GetInstance();
                ParseArguments(args);
                ReadConfiguration(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config.ini"));
            }
            catch (Exception e)
            {
                Error(e.Message);
                return;
            }

            Status("Авторизация...");
            var af = new AuthForm(s_appId);
            if (af.ShowDialog() != DialogResult.OK)
            {
                Error("Для работы нужно авторизоваться в сети!");
                return;
            }

            var usrManager = new UsersManager(af.GetAccessToken());
            try
            {
                var currentuser = usrManager.GetUserById(af.GetTokenUserId());
                Console.Title = String.Format("AppID: {0} - {1}", s_appId, currentuser.GetFullName());
            }
            catch (Exception e)
            {
                Error(e.Message);
                return;
            }

            var offset = 0;
            var random = new Random();
            var query = new VkUsersSearchQuery();
            query.SetParameter(UserSearchParam.HomeTown, "Снежинск");
            query.SetParameter(UserSearchParam.Count, RES_COUNT);
            query.SetParameter(UserSearchParam.Fields,
                               new List<VkProfile.EntryType> {VkProfile.EntryType.Sex, VkProfile.EntryType.Contacts});

            Status("Выполнение...");

            for (int age = 10; age < 60; age++)
            {
                query.SetParameter(UserSearchParam.AgeFrom, age);
                query.SetParameter(UserSearchParam.AgeTo, age);
                offset = 0;
                while (true)
                {
                    query.SetParameter(UserSearchParam.Offset, offset);
                    List<VkUser> result;
                    try
                    {
                        result = usrManager.Search(query);
                    }
                    catch (Exception e)
                    {
                        Error(e.Message);
                        break;
                    }
                    if (result.Count == 0) break;

                    foreach (VkUser user in result)
                    {
                        Status("({0}) {1}... ", user.Id, user.GetFullName());
                        using (var session = s_db.OpenSession())
                        {
                            using (var t = session.BeginTransaction())
                            {
                                try
                                {
                                    session.Save(new User
                                        {
                                            FirstName = user.FirstName,
                                            LastName = user.LastName,
                                            VkId = user.Id,
                                            Sex = user.Sex,
                                            HomePhone = user.HomePhone,
                                            MobilePhone = user.MobilePhone,
                                            BirthYear = user.BirthYear
                                        });
                                }
                                catch (Exception e)
                                {
                                    if (e.InnerException != null &&
                                        e.InnerException is System.Data.SqlClient.SqlException)
                                    {
                                        if (
                                            !(e.InnerException as System.Data.SqlClient.SqlException).Message.Contains(
                                                "Violation of UNIQUE KEY"))
                                        {
                                            Error(e.Message);
                                        }
                                        else
                                        {
                                            Error("({0}) {1}: Уже сохранено!", user.Id, user.GetFullName());
                                        }
                                    }
                                    continue;
                                }
                                t.Commit();
                                Print("({0}) {1}: Сохранено", user.Id, user.GetFullName());
                            }
                        }
                    }
                    offset += result.Count;

                    // sleep 1 - 20 seconds
                    var secs = random.Next(1000, 20000);
                    Status("Ожидание {0} сек.", secs/1000);
                    Thread.Sleep(secs);
                }

                var asecs = random.Next(1000, 20000);
                Status("Ожидание {0} сек.", asecs/1000);
                Thread.Sleep(asecs);
            }

            Status("Готово");
        }