Beispiel #1
0
        public static YandexXmlResponse Parse(string response)
        {
            var xml             = XDocument.Parse(response);
            var responseElement = xml.Element("yandexsearch")?.Element("response");

            var result = new YandexXmlResponse();

            if (responseElement != null)
            {
                var error = responseElement.Element("error");
                if (error != null)
                {
                    result.ErrorCode    = error.Attribute("code")?.Value;
                    result.ErrorMessage = error.Value;
                }

                foreach (var node in responseElement.Elements("found"))
                {
                    if (result.Found == 0 && node.Name == "found" && node.Attribute("priority")?.Value == "all")
                    {
                        result.Found = long.Parse(node.Value);
                    }
                }

                var grouping = responseElement.Element("results")?.Element("grouping");
                if (grouping == null)
                {
                    return(result);
                }

                foreach (var group in grouping.Elements("group"))
                {
                    var doc = group.Element("doc");
                    if (doc == null)
                    {
                        continue;
                    }

                    var url      = (doc.Element("url")?.Value ?? string.Empty).Trim();
                    var title    = (doc.Element("title")?.Value ?? string.Empty).Trim();
                    var headline = (doc.Element("headline")?.Value ?? string.Empty).Trim();
                    var domain   = (doc.Element("domain")?.Value ?? string.Empty).Trim();

                    result.Items.Add(new YandexXmlItem {
                        Url      = url,
                        Title    = title,
                        Domain   = domain,
                        Headline = headline
                    });
                }
            }

            return(result);
        }
        /// <summary>
        /// Получение данных из Yandex.Xml
        /// </summary>
        /// <param name="query">Запрос</param>
        /// <param name="needResult">Нужное количество результатов (максимум 250. Ограничений яндекса)</param>
        /// <returns></returns>
        public async Task <YandexXmlResponse> Get(string query, int needResult)
        {
            var result    = new YandexXmlResponse();
            var page      = 0;
            var docOnPage = 100;

            var config = _config.GetSection("YandexXml").Get <YandexXmlConfig>();

            do
            {
                var url      = string.Format(REQUEST_PATTERN, config.User, config.Key, HttpUtility.UrlEncode(query), docOnPage, page++);
                var response = await GetStringContent(url);

                if (response == null)
                {
                    return(result);
                }

                var xmlResponse = Parse(response);
                if (!string.IsNullOrWhiteSpace(xmlResponse.ErrorCode) || !string.IsNullOrWhiteSpace(xmlResponse.ErrorMessage))
                {
                    _logger.Warn($"При обращении к YandexXml получена ошибка code: {xmlResponse.ErrorCode} message: {xmlResponse.ErrorMessage}");
                }

                if (xmlResponse.Items.Count == 0)
                {
                    return(result);
                }

                if (result.Found == 0)
                {
                    result.Found = xmlResponse.Found;
                }

                result.Items.AddRange(xmlResponse.Items);

                if (page == 2)
                {
                    docOnPage = 50;
                    page      = 4;
                }
            } while (result.Items.Count < needResult);

            return(result);
        }