예제 #1
0
        /// <summary>
        /// Обновляет свойства ключевых фраз.
        /// </summary>
        /// <param name="projects"></param>
        /// <returns></returns>

        public int UpdateKeywordsProperties(IEnumerable <XmlProject> projects)
        {
            var syncGroupPair = GetSyncGroupsPair(projects);

            int counter = 0;

            foreach (var groupPair in syncGroupPair)
            {
                var pairs = SyncHelper.GetItemsForUpdate(
                    groupPair.Item1.Keywords,
                    groupPair.Item2.Keywords,
                    w => GetPhraseKey(w.Phrase),
                    w => GetPhraseKey(w.Phrase));

                foreach (var wordPair in pairs)
                {
                    var apiKeyword = wordPair.Item2;

                    if (GetSiteKey(wordPair.Item1.TargetUrl) != GetSiteKey(apiKeyword.Target))
                    {
                        var request = _requestBuilder.GetUpdateKeywordTargetRequest(
                            apiKeyword.Id, wordPair.Item1.TargetUrl);

                        var res = _client.GetBoolResult(request);

                        // BUG: При установке таргета результат возвращает всегда false
                        apiKeyword.Target = wordPair.Item1.TargetUrl;
                        ++counter;
                    }
                }
            }

            return(counter);
        }
예제 #2
0
        /// <summary>
        /// Удаляет лишние проекты.
        /// </summary>
        /// <param name="projects"></param>
        /// <returns></returns>
        public int DeleteGroups(IEnumerable <XmlProject> projects)
        {
            var syncProjects = GetSyncProjectGroups(projects);

            int counter = 0;

            foreach (var sync in syncProjects)
            {
                var dropGroups = SyncHelper.GetItemsForDelete(
                    sync.XmlObjects,
                    sync.ApiObjects,
                    g => GetGroupNameKey(g.Name),
                    g => GetGroupNameKey(g.GroupName));

                foreach (var group in dropGroups)
                {
                    var request = _requestBuilder.GetDeleteKeywordGroupRequest(
                        sync.ParentId, group.Id);

                    var res = _client.GetBoolResult(request);

                    if (res)
                    {
                        _apiGroups.Remove(group);
                        ++counter;
                    }
                }
            }

            return(counter);
        }
예제 #3
0
        /// <summary>
        /// Удаляет лишние ключевые фразы.
        /// </summary>
        /// <param name="projects"></param>
        /// <returns></returns>
        public int DeleteKeywords(IEnumerable <XmlProject> projects)
        {
            var syncGroupPair = GetSyncGroupsPair(projects);

            int counter = 0;

            foreach (var groupPair in syncGroupPair)
            {
                var dropWords = SyncHelper.GetItemsForDelete(
                    groupPair.Item1.Keywords,
                    groupPair.Item2.Keywords,
                    w => GetPhraseKey(w.Phrase),
                    w => GetPhraseKey(w.Phrase))
                                .ToList();

                foreach (var word in dropWords)
                {
                    var request = _requestBuilder.GetDeleteKeywordRequest(word.Id);
                    var res     = _client.GetBoolResult(request);

                    if (res)
                    {
                        groupPair.Item2.RemoveKeyword(word);
                        ++counter;
                    }
                }
            }

            return(counter);
        }
예제 #4
0
        /// <summary>
        /// Добавляет недостающие проекты.
        /// </summary>
        /// <param name="projects"></param>
        /// <returns></returns>
        public int AddGroups(IEnumerable <XmlProject> projects)
        {
            var syncProjects = GetSyncProjectGroups(projects);

            int counter = 0;

            foreach (var proj in syncProjects)
            {
                var createGroups = SyncHelper.GetItemsForCreate(
                    proj.XmlObjects,
                    proj.ApiObjects,
                    g => GetGroupNameKey(g.Name),
                    g => GetGroupNameKey(g.GroupName));

                foreach (var group in createGroups)
                {
                    var request = _requestBuilder.GetAddKeywordGroupRequest(
                        proj.ParentId, group.Name, group.Enabled);

                    var groupId = _client.GetMessageResult(request);

                    var newGroup = new SyncKeywordGroup(
                        proj.ParentId, groupId, group.Name, group.Enabled);

                    _apiGroups.Add(newGroup);
                    ++counter;
                }
            }

            return(counter);
        }
예제 #5
0
        /// <summary>
        /// Обновляет свойства проектов.
        /// </summary>
        /// <param name="projects"></param>
        /// <returns></returns>
        public int UpdateProjectsProperties(IEnumerable <XmlProject> projects)
        {
            var updateProjects = SyncHelper.GetItemsForUpdate(
                projects, _apiProjects, p => GetSiteKey(p.Site), p => GetSiteKey(p.Site));

            int counter = 0;

            foreach (var pair in updateProjects)
            {
                // включить или выключить (перенести в архив) проект
                var stateOn = (pair.Item1.Enabled) ? 0 : -1;

                var apiProject = pair.Item2;

                if (stateOn != apiProject.On)
                {
                    var request = _requestBuilder.GetUpdateProjectRequest(
                        apiProject.Id, stateOn);

                    var res = _client.GetBoolResult(request);

                    if (res)
                    {
                        apiProject.On = stateOn;
                        ++counter;
                    }
                }
            }

            return(counter);
        }
예제 #6
0
        /// <summary>
        /// Добавляет недостающие фразы.
        /// </summary>
        /// <param name="projects"></param>
        /// <returns></returns>
        public int AddKeywords(IEnumerable <XmlProject> projects)
        {
            var syncGroupPair = GetSyncGroupsPair(projects);

            int counter = 0;

            foreach (var groupPair in syncGroupPair)
            {
                var apiGroup = groupPair.Item2;

                var createWords = SyncHelper.GetItemsForCreate(
                    groupPair.Item1.Keywords.Where(k => !string.IsNullOrEmpty(k.Phrase)),
                    apiGroup.Keywords,
                    w => GetPhraseKey(w.Phrase),
                    w => GetPhraseKey(w.Phrase));

                // пакетное добавление фраз без таргета
                var addKeywords = createWords
                                  .Where(k => string.IsNullOrEmpty(k.TargetUrl))
                                  .ToList();

                if (addKeywords.Count > 0)
                {
                    var request = _requestBuilder.GetAddKeywordsRequest(
                        apiGroup.ProjectId, addKeywords.Select(p => p.Phrase), apiGroup.Id);

                    var res = _client.GetMessageResult(request);

                    foreach (var word in addKeywords)
                    {
                        apiGroup.AddKeyword(word.Phrase);
                    }

                    counter += addKeywords.Count;
                }

                // добавление по одной фразе с таргетом, чтобы получать их id
                addKeywords = createWords
                              .Where(k => !string.IsNullOrEmpty(k.TargetUrl))
                              .ToList();

                foreach (var word in addKeywords)
                {
                    var request = _requestBuilder.GetAddKeywordRequest(
                        apiGroup.ProjectId, word.Phrase, apiGroup.Id);

                    var phraseId = _client.GetMessageResult(request);
                    var keyword  = apiGroup.AddKeyword(word.Phrase);
                    keyword.Id = phraseId;

                    ++counter;
                }
            }

            return(counter);
        }
예제 #7
0
        private IEnumerable <SyncObject <XmlKeywordGroup, SyncKeywordGroup> > GetSyncProjectGroups(
            IEnumerable <XmlProject> projects)
        {
            var updateProjects = SyncHelper.GetItemsForUpdate(
                projects, _apiProjects, p => GetSiteKey(p.Site), p => GetSiteKey(p.Site));

            foreach (var pair in updateProjects)
            {
                var xmlProject = pair.Item1;
                var apiProject = pair.Item2;

                var projectGroups = _apiGroups
                                    .Where(g => g.ProjectId == apiProject.Id)
                                    .ToList();

                yield return(new SyncObject <XmlKeywordGroup, SyncKeywordGroup>(
                                 pair.Item2.Id, pair.Item1.KeywordGroups, projectGroups));
            }
        }
예제 #8
0
        private IEnumerable <Tuple <XmlKeywordGroup, SyncKeywordGroup> > GetSyncGroupsPair(
            IEnumerable <XmlProject> projects)
        {
            var syncGroups = GetSyncProjectGroups(projects);

            foreach (var sync in syncGroups)
            {
                var updateGroups = SyncHelper.GetItemsForUpdate(
                    sync.XmlObjects,
                    sync.ApiObjects,
                    g => GetGroupNameKey(g.Name),
                    g => GetGroupNameKey(g.GroupName));

                foreach (var pair in updateGroups)
                {
                    yield return(pair);
                }
            }
        }
예제 #9
0
        /// <summary>
        /// Добавляет недостающие проекты.
        /// </summary>
        /// <param name="projects"></param>
        /// <returns></returns>
        public int AddProjects(IEnumerable <XmlProject> projects)
        {
            var createProjects = SyncHelper.GetItemsForCreate(
                projects, _apiProjects, p => GetSiteKey(p.Site), p => GetSiteKey(p.Site))
                                 .ToList();

            foreach (var proj in createProjects)
            {
                var request   = _requestBuilder.GetAddProjectRequest(proj.Site);
                var projectId = _client.GetMessageResult(request);

                var newProject = new ApiProject()
                {
                    Id = projectId, Site = proj.Site, On = 0
                };
                _apiProjects.Add(newProject);
            }

            return(createProjects.Count);
        }
예제 #10
0
        /// <summary>
        /// Удаляет лишние проекты.
        /// </summary>
        /// <param name="projects"></param>
        /// <returns></returns>
        public int DeleteProjects(IEnumerable <XmlProject> projects)
        {
            var dropProjects = SyncHelper.GetItemsForDelete(
                projects, _apiProjects, p => GetSiteKey(p.Site), p => GetSiteKey(p.Site))
                               .ToList();

            int counter = 0;

            foreach (var proj in dropProjects)
            {
                var request = _requestBuilder.GetDeleteProjectRequest(proj.Id);
                var res     = _client.GetBoolResult(request);

                if (res)
                {
                    _apiGroups.RemoveAll(g => g.ProjectId == proj.Id);
                    _apiProjects.Remove(proj);
                    ++counter;
                }
            }

            return(counter);
        }