예제 #1
0
        public void StartSearch(int interval)
        {
            SetTimer(interval);
            _timer.Start();

            foreach (var prefs in _usersContext.Preferences)
            {
                if (prefs.GroupId > 0 && !string.IsNullOrEmpty(prefs.Keyword))
                {
                    var keywords     = PrepareKeywords(prefs.Keyword);
                    var wallGeParams = new WallGetParams
                    {
                        Count   = 50,
                        OwnerId = -prefs.GroupId
                    };

                    var getResult = _api.Wall.Get(wallGeParams);
                    var posts     = getResult.WallPosts;

                    foreach (var post in posts)
                    {
                        var searchResult = _keywordSearcher.LookIntoPost(post, keywords);
                        if (!searchResult.Contains)
                        {
                            continue;
                        }
                    }
                }
            }
        }
예제 #2
0
        public void DoSearch()
        {
            using (var usersContext = _serviceProvider.GetRequiredService <IUsersContext>())
            {
                var allPrefs = usersContext.Preferences.Include(u => u.User);
                foreach (var prefs in allPrefs)
                {
                    try
                    {
                        if (!ValidForSearch(prefs))
                        {
                            continue;
                        }

                        var keywords     = PrepareKeywords(prefs.Keyword);
                        var wallGeParams = new WallGetParams
                        {
                            Count   = 50,
                            OwnerId = prefs.TargetType == PreferenceType.VkGroup ? -prefs.TargetId : prefs.TargetId
                        };

                        Thread.Sleep(1000);

                        var getResult = _api.Wall.Get(wallGeParams);
                        var posts     = getResult.WallPosts;

                        foreach (var post in posts.Reverse())
                        {
                            var searchResult = _keywordSearcher.LookIntoPost(post, keywords);
                            if (!searchResult.Contains)
                            {
                                continue;
                            }

                            if (post.Date <= prefs.LastNotifiedPostTime)
                            {
                                continue;
                            }

                            prefs.LastNotifiedPostTime = post.Date ?? DateTime.Now;
                            _userNotifier.NotifyUser(prefs, post.Id.Value, searchResult.Word);
                        }
                    }
                    catch (UserAuthorizationFailException ex)
                    {
                        _logger.Error($"UserAuthorizationException. {ex}");
                        //if (!_api.IsAuthorized)
                        {
                            _logger.Info($"VkApi wasn't authorized. Authorizing..");
                            _api.SimpleAuthorize(_settings.Vkontakte);
                            _logger.Info($"SimpleAuthorize passed. New vk auth status = {_api.IsAuthorized}");
                        }
                    }
                    catch (Exception ex)
                    {
                        _logger.Error(ex, $"Error while fetching VK groups. Pref: {prefs.ToShortString()}. Exception message: {ex}");
                    }
                }
                usersContext.SaveChanges();
            }
        }