Beispiel #1
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("Готово");
        }
Beispiel #2
0
        public List<VkUser> Search(VkUsersSearchQuery query)
        {
            var cmdParams = new Dictionary<string, object>(0);
            foreach (KeyValuePair<UserSearchParam, object> param in query.GetParameters())
            {
                switch (param.Key)
                {
                    case UserSearchParam.Query:
                        cmdParams.Add("q", param.Value);
                        break;
                    case UserSearchParam.Sort:
                        cmdParams.Add("sort", param.Value);
                        break;
                    case UserSearchParam.Offset:
                        cmdParams.Add("offset", param.Value);
                        break;
                    case UserSearchParam.Count:
                        cmdParams.Add("count", param.Value);
                        break;
                    case UserSearchParam.Fields:
                        var sb = new StringBuilder();
                        foreach (VkProfile.EntryType type in param.Value as IEnumerable<VkProfile.EntryType>)
                        {
                            sb.Append(String.Format("{0},", VkProfile.EntryTypeToString(type)));
                        }
                        sb.Remove(sb.Length - 1, 1);
                        cmdParams.Add("fields", sb.ToString());
                        break;
                    case UserSearchParam.CityId:
                        cmdParams.Add("city", param.Value);
                        break;
                    case UserSearchParam.CountryId:
                        cmdParams.Add("country", param.Value);
                        break;
                    case UserSearchParam.HomeTown:
                        cmdParams.Add("hometown", param.Value);
                        break;
                    case UserSearchParam.Sex:
                        cmdParams.Add("sex", param.Value);
                        break;
                    case UserSearchParam.AgeFrom:
                        cmdParams.Add("age_from", param.Value);
                        break;
                    case UserSearchParam.AgeTo:
                        cmdParams.Add("age_to", param.Value);
                        break;
                    case UserSearchParam.BirthDay:
                        cmdParams.Add("birth_day", param.Value);
                        break;
                    case UserSearchParam.BirthMonth:
                        cmdParams.Add("birth_month", param.Value);
                        break;
                    case UserSearchParam.BirthYear:
                        cmdParams.Add("birth_year", param.Value);
                        break;
                    case UserSearchParam.Online:
                        cmdParams.Add("online", param.Value);
                        break;
                    case UserSearchParam.Interests:
                        cmdParams.Add("interests", param.Value);
                        break;
                    case UserSearchParam.Gid:
                        cmdParams.Add("gid", param.Value);
                        break;
                    default:
                        continue;
                }
            }

            var cmd = GetVkXmlCommand("users.search", cmdParams, true);
            string response;
            try
            {
                response = cmd.GetResponseAsString();
            }
            catch (Exception e)
            {
                Logger.ErrorFormat("({0}) {1}", e.GetType().Name, e.Message);
                throw new VkMethodInvocationException((int)VkMethodInvocationErrorCode.FailedGetResponse,
                                                  "Не удалось получить ответ от сервера");
            }
            var xml = new XmlDocument();
            try
            {
                xml.LoadXml(response);
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat("XML parsing failed. ({0}) {1}", ex.GetType().Name, ex.Message);
                Logger.Debug(response);
                throw new VkMethodInvocationException((int)VkMethodInvocationErrorCode.IncorrectReplyFormat, "Сервер вернул некорректный ответ");
            }
            if (VkErrorResponse.HasErrorInResponse(xml))
            {
                var err = VkErrorResponse.Parse(xml);
                Logger.DebugFormat("VK Error ({0}) : {1}", err.ErrorCode, err.Message);
                throw new VkMethodInvocationException(err.ErrorCode, err.Message);
            }

            var result = new List<VkUser>();
            // todo: Сделать парсинг дополнительных полей анкеты

            foreach (XmlElement resultNode in xml.DocumentElement.GetElementsByTagName("user"))
            {
                Int64 uid;
                if (resultNode["uid"] == null) continue;
                try
                {
                    uid = Int64.Parse(resultNode["uid"].InnerText);
                }
                catch (Exception)
                {
                    Logger.ErrorFormat("Cant parse UID {0}", resultNode["uid"].InnerText);
                    continue;
                }
                var usr = new VkUser(uid);
                if (resultNode["first_name"] != null)
                {
                    usr.FirstName = resultNode["first_name"].InnerText;
                }
                if (resultNode["last_name"] != null)
                {
                    usr.LastName = resultNode["last_name"].InnerText;
                }
                if (resultNode["mobile_phone"] != null && resultNode["mobile_phone"].InnerText.Length > 0)
                {
                    usr.MobilePhone = resultNode["mobile_phone"].InnerText;
                }
                if (resultNode["home_phone"] != null && resultNode["home_phone"].InnerText.Length > 0)
                {
                    usr.HomePhone = resultNode["home_phone"].InnerText;
                }
                if (resultNode["sex"] != null)
                {
                    try
                    {
                        usr.Sex = Int32.Parse(resultNode["sex"].InnerText);
                    }
                    catch (Exception)
                    {
                        usr.Sex = 0;
                    }
                }
                if (query[UserSearchParam.BirthYear] != null)
                {
                    usr.BirthYear = Int32.Parse(query[UserSearchParam.BirthYear].ToString());
                }
                result.Add(usr);
            }

            return result;
        }