Ejemplo n.º 1
0
        public bool GetCurrentFriends(AccountViewModel accountViewModel)
        {
            const string functionName = "Обновление текущего списка друзей";

            var account = _accountManager.GetAccountById(accountViewModel.Id);

            try
            {
                _notice.AddNotice(functionName, account.Id, "Начинаем обновлять список друзей");

                var userAgent = new GetUserAgentQueryHandler(new DataBaseContext()).Handle(new GetUserAgentQuery
                {
                    UserAgentId = account.UserAgentId
                });

                var groupId = account.GroupSettingsId;

                if (groupId == null)
                {
                    _notice.AddNotice(functionName, account.Id, "Ошибка! Группа не выбрана.");
                    return(false);
                }

                var settings           = _accountSettingsManager.GetSettings((long)groupId);
                var accountInformation = _accountManager.GetAccountInformation((long)groupId);

                var friends = new GetCurrentFriendsBySeleniumEngine().Execute(new GetCurrentFriendsBySeleniumModel
                {
                    Cookie            = account.Cookie,
                    AccountFacebookId = account.FacebookId,
                    Driver            = _seleniumManager.RegisterNewDriver(new AccountViewModel
                    {
                        Proxy         = account.Proxy,
                        ProxyLogin    = account.ProxyLogin,
                        ProxyPassword = account.ProxyPassword,
                        UserAgentId   = userAgent.Id
                    }),
                    Proxy     = _accountManager.GetAccountProxy(account),
                    UserAgent = userAgent.UserAgentString
                });

                _notice.AddNotice(functionName, account.Id, "Список текущих друзей получен, количество - " + friends.Count);

                _notice.AddNotice(functionName, account.Id, string.Format("Учитываем погрешность {0}%", settings.AllowedRemovalPercentage));

                var notError = _friendManager.RecountError(accountInformation.CountCurrentFriends, friends.Count, settings.AllowedRemovalPercentage);

                if (!notError)
                {
                    _notice.AddNotice(functionName, account.Id, string.Format("Ошибка получения друзей. Получено - {0}, текущее колиство в базе - {1}, погрешность - {2}%", friends.Count, accountInformation.CountCurrentFriends, settings.AllowedRemovalPercentage));

                    return(false);
                }

                var outgoingFriendships =
                    new GetFriendsByAccountIdQueryHandler(new DataBaseContext()).Handle(new GetFriendsByAccountIdQuery
                {
                    AccountId   = account.Id,
                    FriendsType = FriendTypes.Outgoig
                });


                if (friends.Count == 0)
                {
                    _notice.AddNotice(functionName, account.Id, "Нет друзей. Обновление друзей завершено.");

                    return(true);
                }

                _notice.AddNotice(functionName, account.Id, "Сверяем друзей с исходящими заявками");
                foreach (var newFriend in friends)
                {
                    if (outgoingFriendships.All(data => data.FacebookId != newFriend.FacebookId))
                    {
                        continue;
                    }

                    _notice.AddNotice(functionName, account.Id, newFriend.FriendName + "добавился в друзья ");

                    new DeleteAnalysisFriendByIdHandler(new DataBaseContext()).Handle(new DeleteAnalysisFriendById
                    {
                        AnalysisFriendFacebookId = newFriend.FacebookId
                    });

                    new AddOrUpdateAccountStatisticsCommandHandler(new DataBaseContext()).Handle(
                        new AddOrUpdateAccountStatisticsCommand
                    {
                        AccountId = account.Id,
                        CountOrdersConfirmedFriends = 1
                    });

                    _notice.AddNotice(functionName, account.Id, "Сверяем друзей с исходящими заявками");
                }

                // drop blocked users
                _notice.AddNotice(functionName, account.Id, "Сверяем друзей с черным списком");

                var newFriendList = (from friend in friends
                                     let isBlocked =
                                         new CheckForFriendBlacklistedQueryHandler().Handle(new CheckForFriendBlacklistedQuery
                {
                    FriendFacebookId = friend.FacebookId,
                    GroupSettingsId = (long)groupId
                })
                                         where !isBlocked
                                         select new FriendData
                {
                    FacebookId = friend.FacebookId,
                    FriendName = friend.FriendName,
                    Href = friend.Uri,
                    Gender = friend.Gender
                }).ToList();

                _notice.AddNotice(functionName, account.Id, "Совпадений с черным списком - " + (friends.Count - newFriendList.Count));

                _notice.AddNotice(functionName, account.Id, "Сохраняем друзей");

                new SaveUserFriendsCommandHandler(new DataBaseContext()).Handle(new SaveUserFriendsCommand
                {
                    AccountId = account.Id,
                    Friends   = newFriendList
                });

                new AddAccountInformationCommandHandler(new DataBaseContext()).Handle(new AddAccountInformationCommand
                {
                    AccountId = account.Id,
                    AccountInformationData = new AccountInformationDataDbModel
                    {
                        CountCurrentFriends = friends.Count
                    }
                });

                _notice.AddNotice(functionName, account.Id, "Обновление друзей завершено.");
            }
            catch (Exception ex)
            {
                _notice.AddNotice(functionName, account.Id, string.Format("Ошибка - {0}", ex.Message));
            }
            return(true);
        }
Ejemplo n.º 2
0
        public UnreadFriendMessageList GetUnreadMessages(AccountViewModel account)
        {
            var unreadMessages = new List <FacebookMessageModel>();
            var userAgent      = new GetUserAgentQueryHandler(new DataBaseContext()).Handle(new GetUserAgentQuery
            {
                UserAgentId = account.UserAgentId
            });

            var newCorrespondenceRequests = new GetCorrespondenceRequestsEndine().Execute(new GetCorrespondenceRequestsModel
            {
                AccountFacebookId = account.FacebookId,
                Cookie            = account.Cookie,
                Proxy             = _accountManager.GetAccountProxy(account),
                Driver            = _seleniumManager.RegisterNewDriver(new AccountViewModel
                {
                    ProxyLogin    = account.ProxyLogin,
                    ProxyPassword = account.ProxyPassword,
                    Proxy         = account.Proxy,
                    UserAgentId   = account.UserAgentId
                })
            });

            unreadMessages.AddRange(newCorrespondenceRequests);

            var newUnreadMessages = new GetUnreadMessagesEngine().Execute(new GetUnreadMessagesModel
            {
                AccountId     = account.FacebookId,
                Cookie        = account.Cookie,
                Proxy         = _accountManager.GetAccountProxy(account),
                UrlParameters = new GetUrlParametersQueryHandler(new DataBaseContext()).Handle(new GetUrlParametersQuery
                {
                    NameUrlParameter = NamesUrlParameter.GetUnreadMessages
                }),
                UserAgent = userAgent.UserAgentString
            });

            foreach (var facebookMessageModel in newUnreadMessages)
            {
                if (
                    newCorrespondenceRequests.Any(
                        model => model.FriendFacebookId == facebookMessageModel.FriendFacebookId))
                {
                    continue;
                }

                unreadMessages.Add(facebookMessageModel);
            }

            if (unreadMessages.Count == 0)
            {
                return(new UnreadFriendMessageList
                {
                    UnreadMessages = unreadMessages.Select(model => new UnreadFriendMessageModel
                    {
                        FriendFacebookId = model.FriendFacebookId,
                        UnreadMessage = model.UnreadMessage,
                        LastMessage = model.LastMessage,
                        CountAllMessages = model.CountAllMessages,
                        CountUnreadMessages = model.CountUnreadMessages,
                        LastReadMessageDateTime = model.LastReadMessageDateTime,
                        LastUnreadMessageDateTime = model.LastUnreadMessageDateTime,
                        FriendGender = model.Gender,
                        FriendName = model.Name,
                        FriendHref = model.Href,
                        FriendType = model.FriendType
                    }).ToList()
                });
            }

            new SaveUnreadMessagesCommandHandler(new DataBaseContext()).Handle(new SaveUnreadMessagesCommand()
            {
                AccountId      = account.Id,
                UnreadMessages = unreadMessages.Select(model => new FacebookMessageDbModel
                {
                    AccountId           = model.AccountId,
                    FriendFacebookId    = model.FriendFacebookId,
                    Gender              = model.Gender,
                    CountAllMessages    = model.CountAllMessages,
                    CountUnreadMessages = model.CountUnreadMessages,
                    Href                      = model.Href,
                    LastMessage               = model.LastMessage,
                    LastReadMessageDateTime   = model.LastReadMessageDateTime,
                    LastUnreadMessageDateTime = model.LastUnreadMessageDateTime,
                    Name                      = model.Name,
                    UnreadMessage             = model.UnreadMessage,
                    FriendType                = model.FriendType
                }).ToList()
            });

            var ureadMessages = unreadMessages.Select(model => new UnreadFriendMessageModel
            {
                FriendFacebookId          = model.FriendFacebookId,
                UnreadMessage             = model.UnreadMessage,
                LastMessage               = model.LastMessage,
                CountAllMessages          = model.CountAllMessages,
                CountUnreadMessages       = model.CountUnreadMessages,
                LastReadMessageDateTime   = model.LastReadMessageDateTime,
                LastUnreadMessageDateTime = model.LastUnreadMessageDateTime,
                FriendGender              = model.Gender,
                FriendName = model.Name,
                FriendHref = model.Href
            }).ToList();

            return(new UnreadFriendMessageList
            {
                UnreadMessages = ureadMessages
            });
        }
Ejemplo n.º 3
0
        public UnreadFriendMessageList GetUnreadMessages(AccountViewModel account)
        {
            const string functionName   = "Получение непрочитанных сообщений";
            var          unreadMessages = new List <FacebookMessageModel>();
            List <UnreadFriendMessageModel> ureadMessages = null;

            try
            {
                var userAgent = new GetUserAgentQueryHandler(new DataBaseContext()).Handle(new GetUserAgentQuery
                {
                    UserAgentId = account.UserAgentId
                });

                var newCorrespondenceRequests = new GetCorrespondenceRequestsEndine().Execute(
                    new GetCorrespondenceRequestsModel
                {
                    AccountFacebookId = account.FacebookId,
                    Cookie            = account.Cookie,
                    Proxy             = _accountManager.GetAccountProxy(account),
                    Driver            = _seleniumManager.RegisterNewDriver(new AccountViewModel
                    {
                        ProxyLogin    = account.ProxyLogin,
                        ProxyPassword = account.ProxyPassword,
                        Proxy         = account.Proxy,
                        UserAgentId   = account.UserAgentId
                    })
                });

                unreadMessages.AddRange(newCorrespondenceRequests);

                var newUnreadMessages = new GetUnreadMessagesEngine().Execute(new GetUnreadMessagesModel
                {
                    AccountId     = account.FacebookId,
                    Cookie        = account.Cookie,
                    Proxy         = _accountManager.GetAccountProxy(account),
                    UrlParameters = new GetUrlParametersQueryHandler(new DataBaseContext()).Handle(
                        new GetUrlParametersQuery
                    {
                        NameUrlParameter = NamesUrlParameter.GetUnreadMessages
                    }),
                    UserAgent          = userAgent.UserAgentString,
                    NumbersOfDialogues = 10
                });

                foreach (var facebookMessageModel in newUnreadMessages)
                {
                    if (newCorrespondenceRequests.Any(model => model.FriendFacebookId == facebookMessageModel.FriendFacebookId))
                    {
                        continue;
                    }

                    if (facebookMessageModel.LastMessage.Equals(string.Empty))
                    {
                        //todo: MARK UNREAD
                    }

                    unreadMessages.Add(facebookMessageModel);
                }

                if (unreadMessages.Count == 0)
                {
                    return(new UnreadFriendMessageList
                    {
                        UnreadMessages = unreadMessages.Select(model => new UnreadFriendMessageModel
                        {
                            FriendFacebookId = model.FriendFacebookId,
                            UnreadMessage = model.UnreadMessage,
                            LastMessage = model.LastMessage,
                            CountAllMessages = model.CountAllMessages,
                            CountUnreadMessages = model.CountUnreadMessages,
                            LastReadMessageDateTime = model.LastReadMessageDateTime,
                            LastUnreadMessageDateTime = model.LastUnreadMessageDateTime,
                            FriendGender = model.Gender,
                            FriendName = model.Name,
                            FriendHref = model.Href,
                            FriendType = model.FriendType
                        }).ToList()
                    });
                }

                new SaveUnreadMessagesCommandHandler(new DataBaseContext()).Handle(new SaveUnreadMessagesCommand()
                {
                    AccountId      = account.Id,
                    UnreadMessages = unreadMessages.Select(model => new FacebookMessageDbModel
                    {
                        AccountId           = model.AccountId,
                        FriendFacebookId    = model.FriendFacebookId,
                        Gender              = model.Gender,
                        CountAllMessages    = model.CountAllMessages,
                        CountUnreadMessages = model.CountUnreadMessages,
                        Href                      = model.Href,
                        LastMessage               = model.LastMessage,
                        LastReadMessageDateTime   = model.LastReadMessageDateTime,
                        LastUnreadMessageDateTime = model.LastUnreadMessageDateTime,
                        Name                      = model.Name,
                        UnreadMessage             = model.UnreadMessage,
                        FriendType                = model.FriendType
                    }).ToList()
                });

                ureadMessages = unreadMessages.Select(model => new UnreadFriendMessageModel
                {
                    FriendFacebookId          = model.FriendFacebookId,
                    UnreadMessage             = model.UnreadMessage,
                    LastMessage               = model.LastMessage,
                    CountAllMessages          = model.CountAllMessages,
                    CountUnreadMessages       = model.CountUnreadMessages,
                    LastReadMessageDateTime   = model.LastReadMessageDateTime,
                    LastUnreadMessageDateTime = model.LastUnreadMessageDateTime,
                    FriendGender              = model.Gender,
                    FriendName = model.Name,
                    FriendHref = model.Href
                }).ToList();
            }
            catch (Exception ex)
            {
                _notice.AddNotice(functionName, account.Id, string.Format("Возникла ошибка {0}", ex.Message));
            }
            return(new UnreadFriendMessageList
            {
                UnreadMessages = ureadMessages
            });
        }
Ejemplo n.º 4
0
        public bool RefreshCookies(AccountViewModel account, bool forSpy, IBackgroundJobService backgroundJob)
        {
            if (account.Proxy != null)
            {
                var proxyIsFailed = new CheckProxyEngine().Execute(new CheckProxyModel
                {
                    Driver = _seleniumManager.RegisterNewDriver(account)
                });

                if (proxyIsFailed)
                {
                    if (forSpy)
                    {
                        new UpdateFailSpyAccountInformationCommandHandler(new DataBaseContext()).Handle(
                            new UpdateFailSpyAccountInformationCommand
                        {
                            AccountId         = account.Id,
                            ProxyDataIsFailed = true
                        });
                    }
                    else
                    {
                        new UpdateFailAccountInformationCommandHandler(new DataBaseContext()).Handle(
                            new UpdateFailAccountInformationCommand
                        {
                            AccountId         = account.Id,
                            ProxyDataIsFailed = true
                        });
                    }

                    backgroundJob.RemoveAllBackgroundJobs(new RemoveAccountJobsModel
                    {
                        AccountId = account.Id,
                        IsForSpy  = forSpy,
                        Login     = account.Login
                    });
                    return(false);
                }
            }

            var cookieResponse = new GetNewCookiesEngine().Execute(new GetNewCookiesModel
            {
                Login    = account.Login,
                Password = account.Password,
                Driver   = _seleniumManager.RegisterNewDriver(account),
                Cookie   = account.Cookie
            });

            if (cookieResponse == null || cookieResponse.AuthorizationError)
            {
                if (forSpy)
                {
                    new UpdateFailSpyAccountInformationCommandHandler(new DataBaseContext()).Handle(
                        new UpdateFailSpyAccountInformationCommand
                    {
                        AccountId = account.Id,
                        AuthorizationDataIsFailed = true
                    });
                }
                else
                {
                    new UpdateFailAccountInformationCommandHandler(new DataBaseContext()).Handle(
                        new UpdateFailAccountInformationCommand
                    {
                        AccountId = account.Id,
                        AuthorizationDataIsFailed = true
                    });
                }

                backgroundJob.RemoveAllBackgroundJobs(new RemoveAccountJobsModel
                {
                    AccountId = account.Id,
                    IsForSpy  = forSpy,
                    Login     = account.Login
                });

                return(false);
            }

            if (cookieResponse.ConfirmationError)
            {
                if (forSpy)
                {
                    new UpdateFailSpyAccountInformationCommandHandler(new DataBaseContext()).Handle(
                        new UpdateFailSpyAccountInformationCommand
                    {
                        AccountId            = account.Id,
                        ConformationIsFailed = true
                    });
                }
                else
                {
                    new UpdateFailAccountInformationCommandHandler(new DataBaseContext()).Handle(
                        new UpdateFailAccountInformationCommand
                    {
                        AccountId            = account.Id,
                        ConformationIsFailed = true
                    });
                }

                backgroundJob.RemoveAllBackgroundJobs(new RemoveAccountJobsModel
                {
                    AccountId = account.Id,
                    IsForSpy  = forSpy,
                    Login     = account.Login
                });

                return(false);
            }

            var newCookie = cookieResponse.CookiesString;

            if (newCookie == null)
            {
                return(false);
            }
            if (forSpy)
            {
                new UpdateCookiesForSpyHandler(new DataBaseContext()).Handle(new UpdateCookiesForSpyCommand
                {
                    AccountId       = account.Id,
                    NewCookieString = newCookie
                });
            }
            else
            {
                new UpdateCookiesHandler(new DataBaseContext()).Handle(new UpdateCookiesCommand()
                {
                    AccountId       = account.Id,
                    NewCookieString = newCookie
                });
            }

            new UpdateFailSpyAccountInformationCommandHandler(new DataBaseContext()).Handle(
                new UpdateFailSpyAccountInformationCommand
            {
                AccountId                 = account.Id,
                ProxyDataIsFailed         = false,
                AuthorizationDataIsFailed = false,
                ConformationIsFailed      = false
            });

            return(true);
        }
Ejemplo n.º 5
0
        public FriendViewModel GetFriendToWink(AccountViewModel account)
        {
            const string functionName = "Получение друзей для подмигивания";

            _notice.AddNotice(functionName, account.Id, "Получаем друга для подмигиваний");
            var groupId = account.GroupSettingsId;

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

            var settings = _accountSettingsManager.GetSettings((long)groupId);

            var model = new GetFriendToWinkQueryHandler(new DataBaseContext()).Handle(new GetFriendToWinkQuery
            {
                AccountId       = account.Id,
                GroupSettingsId = (long)groupId
            });

            if (model == null)
            {
                _notice.AddNotice(functionName, account.Id, string.Format("Нет ниодного подходящего друга."));
                return(null);
            }

            _notice.AddNotice(functionName, account.Id, string.Format("Получили {0}({1})", model.FriendName, model.FacebookId));

            var testedFriendsId = new List <long> {
                model.FacebookId
            };

            var considerGeoForWink = settings.ConsiderGeoForWinkFriends;

            if (!considerGeoForWink)
            {
                return(new FriendViewModel
                {
                    AddDateTime = model.AddedDateTime,
                    Id = model.Id,
                    Deleted = model.Deleted,
                    FacebookId = model.FacebookId,
                    MessagesEnded = model.DialogIsCompleted,
                    Name = model.FriendName,
                    Href = model.Href,
                    IsAddedToGroups = model.IsAddedToGroups,
                    IsAddedToPages = model.IsAddedToPages,
                    IsWinked = model.IsWinked,
                    MessageRegime = model.MessageRegime,
                    AddedToRemoveDateTime = model.AddedToRemoveDateTime
                });
            }

            var driver = _seleniumManager.RegisterNewDriver(account); // открываем драйвер

            _notice.AddNotice(functionName, account.Id, string.Format("Проверяем гео данные для {0}({1})", model.FriendName, model.FacebookId));

            var isSuccces = new SpyService(null).AnalizeFriend(account, model.FacebookId, settings, driver);

            while (!isSuccces && settings.ConsiderGeoForWinkFriends)
            {
                _notice.AddNotice(functionName, account.Id, string.Format("Не прошел провергу по Гео {0}({1}), берем другого", model.FriendName, model.FacebookId));
                model = new GetFriendToWinkQueryHandler(new DataBaseContext()).Handle(new GetFriendToWinkQuery
                {
                    AccountId       = account.Id,
                    GroupSettingsId = (long)groupId,
                    TestedFriendsId = testedFriendsId
                });

                testedFriendsId.Add(model.FacebookId);

                _notice.AddNotice(functionName, account.Id, string.Format("Проверяем гео данные для {0}({1})", model.FriendName, model.FacebookId));

                isSuccces = new SpyService(null).AnalizeFriend(account, model.FacebookId, settings, driver);

                settings = _accountSettingsManager.GetSettings((long)groupId); //Обновляем настройки
            }

            driver.Quit(); //Закрываем драйвер

            _notice.AddNotice(functionName, account.Id, string.Format("Получили {0}({1})", model.FriendName, model.FacebookId));
            return(new FriendViewModel
            {
                AddDateTime = model.AddedDateTime,
                Id = model.Id,
                Deleted = model.Deleted,
                FacebookId = model.FacebookId,
                MessagesEnded = model.DialogIsCompleted,
                Name = model.FriendName,
                Href = model.Href,
                IsAddedToGroups = model.IsAddedToGroups,
                IsAddedToPages = model.IsAddedToPages,
                IsWinked = model.IsWinked,
                MessageRegime = model.MessageRegime,
                AddedToRemoveDateTime = model.AddedToRemoveDateTime
            });
        }
Ejemplo n.º 6
0
        public void AnalyzeFriends(AccountViewModel accountViewModel)
        {
            var spyAccount = new AccountModel
            {
                Id            = accountViewModel.Id,
                FacebookId    = accountViewModel.FacebookId,
                Login         = accountViewModel.Login,
                Name          = accountViewModel.Name,
                PageUrl       = accountViewModel.PageUrl,
                Password      = accountViewModel.Password,
                Proxy         = accountViewModel.Proxy,
                ProxyLogin    = accountViewModel.ProxyLogin,
                ProxyPassword = accountViewModel.ProxyPassword,
                Cookie        = new CookieModel
                {
                    CookieString = accountViewModel.Cookie
                }
            };

            var driver = _seleniumManager.RegisterNewDriver(accountViewModel); // открываем драйвер для spy

            try
            {
                var friendList = new GetAnalisysFriendsQueryHandler(new DataBaseContext()).Handle(new GetAnalisysFriendsQuery());

                foreach (var analysisFriendData in friendList)
                {
                    var accountAnalysisFriend = _accountManager.GetAccountById(analysisFriendData.AccountId);

                    if (!new AccountManager().HasAWorkingAccount(accountAnalysisFriend.Id))
                    {
                        continue;
                    }

                    var settingsModel = accountAnalysisFriend.GroupSettingsId != null
                        ? _accountSettingsManager.GetSettings((long)accountAnalysisFriend.GroupSettingsId)
                        : new GroupSettingsViewModel();

                    var settings = new GroupSettingsViewModel
                    {
                        GroupId   = settingsModel.GroupId,
                        Gender    = settingsModel.Gender,
                        Countries = settingsModel.Countries,
                        Cities    = settingsModel.Cities
                    };

                    var friendIsSuccess = AnalizeFriend(new AccountViewModel
                    {
                        FacebookId    = spyAccount.FacebookId,
                        Cookie        = spyAccount.Cookie.CookieString,
                        Proxy         = spyAccount.Proxy,
                        ProxyLogin    = spyAccount.ProxyLogin,
                        ProxyPassword = spyAccount.ProxyPassword
                    }, analysisFriendData.FacebookId,
                                                        settings,
                                                        driver);

                    new AnalizeFriendCore().StartAnalyze(new AnalyzeModel
                    {
                        Settings       = settings,
                        AnalysisFriend = analysisFriendData,
                        InfoIsSuccess  = friendIsSuccess,
                        SpyAccountId   = spyAccount.Id
                    });
                }
            }
            catch (Exception)
            {
                driver.Quit();
            }

            driver.Quit();
        }