private CategoriesList BuildCategories()
        {
            CategoriesList categoriesList = new CategoriesList();

            try
            {
                string filepath =  System.Web.Hosting.HostingEnvironment.MapPath("~/data/Categories.xml"); 
                XmlDocument xmlDocument = new XMLConnector().GetXMLDocument(filepath);

                XmlNodeList categoryNodes = xmlDocument.SelectNodes("categories/category");
                if (categoryNodes != null)
                {
                    foreach (XmlNode categoryNode in categoryNodes)
                    {
                        string id = categoryNode.Attributes["id"].Value;                        
                        CategoryItem categoryItem = new CategoryItem(id);
                        categoryItem.Title = categoryNode.Attributes["title"].Value;

                        XmlNodeList providerMappingsNodes = categoryNode.SelectNodes("provider-mappings/provider-mapping");
                        if (providerMappingsNodes != null)
                        {
                            ProviderMappingList providerMappingList = categoryItem.ProviderMappingList;
                            foreach (XmlNode providerMappingNode in providerMappingsNodes)
                            {
                                ProviderMapping providerMapping = new ProviderMapping();
                                providerMapping.Provider = (ProviderEnum) Enum.Parse(typeof(ProviderEnum), providerMappingNode.Attributes["id"].Value);
                                providerMapping.Category = providerMappingNode.Attributes["category"].Value;

                                providerMappingList.Add(providerMapping);
                            }
                        }

                        XmlNodeList topicsNodes = categoryNode.SelectNodes("topics/topic");
                        if (topicsNodes != null)
                        {
                            TopicList topicList = categoryItem.TopicList;
                            foreach (XmlNode topicNode in topicsNodes)
                            {
                                string title = topicNode.Attributes["title"].Value;
                                TopicItem topicItem = new TopicItem(title, title);
                                topicList.Add(topicItem);
                            }
                        }

                        categoriesList.Add(categoryItem);
                    }
                }
            }
            catch (Exception exception)
            {
                ErrorService.Log("CategorizationService", "BuildCategories", null, exception);
            }

            return categoriesList;
        }
        public void FillTopic(TopicItem topicItem)
        {
            String query = topicItem.Query.Replace(" ", "_");

            try
            {
                XMLConnector XMLConnector = new XMLConnector();

                bool isRedirect = true;

                while (isRedirect) 
                {

                    isRedirect = false;

                    string url = "http://en.wikipedia.org/wiki/Special:Export/" + query;
                    XmlDocument xmlDocument = XMLConnector.GetXMLDocument(url);

                    XmlNodeList nodeList = xmlDocument.GetElementsByTagName("text");
                    string content = nodeList[0].InnerText;
                    if (content != null && content.Length > 0)
                    {
                        // "#REDIRECT [[Dan Quayle]] {{R from other capitalisation}}"
                        if (content.IndexOf("#REDIRECT") >= 0) 
                        {
                            string newQuery = ParseRedirect(content);
                            if (newQuery != null && newQuery.Length > 0)
                            {
                                if (!newQuery.Equals(query))
                                {
                                    query = newQuery;
                                    isRedirect = true;
                                }
                            }
                        }
                        else 
                        {
                            topicItem.WikipediaSummary = BuildSummary(content);
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                ErrorService.Log("WikipediaTopicService", "FillTopic", topicItem.ToString(), exception);
            }
        }
Пример #3
0
        public Channel GetChannel(string url)
        {
            string path = "rss/channel/item";

            XmlDocument rssDoc = new XMLConnector().GetXMLDocument(url);

            Channel channel = new Channel();
            XmlNodeList rssItems = rssDoc.SelectNodes(path);

            if (rssItems != null) 
            {
                foreach (XmlNode rssItem in rssItems)
                {
                    Item item = new Item();

                    foreach (XmlNode childNode in rssItem.ChildNodes)

                    if (childNode.Name == "title")
                    {
                        item.Title = childNode.InnerText;
                    }
                    else if (childNode.Name == "link")
                    {
                        item.URL = childNode.InnerText;
                    }
                    else if (childNode.Name == "description")
                    {
                        item.Description = childNode.InnerText;
                    }
                    else
                    {
                        item.Set(childNode.Name, childNode.InnerText);
                    }

                    channel.Add(item);
                }
            }
            return channel;
        }
        public SearchResultList Search(SearchContext searchContext)
        {
            SearchResultList resultList = new SearchResultList();

            try
            {
                string urlTemplate;
                switch (searchContext.SearchType)
                {
                    case SearchTypeEnum.Image:
                        urlTemplate = "http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key={0}&safe_search=1&per_page={1}&page={2}&format=rest&nojsoncallback=1&privacy_filter=1&content_type=1&sort=relevance&text={3}";
                        break;

                    default:
                        return resultList;
                }


                string query = searchContext.Query;
                int countPerPage = searchContext.Count;
                int page = searchContext.Page;
                string key = ConfigService.GetConfig(ConfigKeys.FLICKR_API_KEY, "");

                string url = string.Format(urlTemplate, key, countPerPage, page, HttpUtility.UrlEncode(query));

                Console.WriteLine(url);

                XMLConnector XMLConnector = new XMLConnector();
                XmlDocument xmlDocument = XMLConnector.GetXMLDocument(url);

                new FlickrSearchParser().Parse(xmlDocument, resultList);
            }
            catch (Exception exception)
            {
                ErrorService.Log("FlickrSearchService", "Search", searchContext.ToString(), exception);
            }

            return resultList;
        }
        private SearchResultList SearchNews(SearchContext searchContext)
        {
            SearchResultList resultList = new SearchResultList();

            try
            {
                string query = searchContext.Query;
                int page = searchContext.Page;
                int count = searchContext.Count;
                int start = (page - 1) * count;
                string urlTemplate = "http://boss.yahooapis.com/ysearch/news/v1/{0}?appid={1}&start={2}&count={3}&orderby=date&age=7d&style=raw&format=xml";
                string api = ConfigService.GetConfig(ConfigKeys.YAHOO_API_KEY, "");

                string url = string.Format(urlTemplate, query, api, start, count);

                XmlDocument xmlDocument = new XMLConnector().GetXMLDocument(url);
                XmlNamespaceManager nsmanager = new XmlNamespaceManager(xmlDocument.NameTable);
                nsmanager.AddNamespace("default", "http://www.inktomi.com/");

                XmlNodeList resultNodes = xmlDocument.SelectNodes("default:ysearchresponse/default:resultset_news/default:result", nsmanager);
                if (resultNodes != null)
                {
                    foreach (XmlNode resultNode in resultNodes)
                    {
                        SearchResultItem resultItem = new SearchResultItem();
                        resultItem.Provider = ProviderEnum.Yahoo;

                        XmlNode titleNode = resultNode.SelectSingleNode("default:title", nsmanager);
                        if (titleNode != null)
                        {
                            resultItem.Title = titleNode.InnerText;
                        }

                        XmlNode abstractNode = resultNode.SelectSingleNode("default:abstract", nsmanager);
                        if (abstractNode != null)
                        {
                            string abstractText = abstractNode.InnerText;
                            abstractText = TextCleaner.StripTag(abstractText, "<", ">"); // remove any html tags
                            abstractText = TextCleaner.RemoveHtml(abstractText);
                            resultItem.Abstract = abstractText;
                        }

                        XmlNode urlNode = resultNode.SelectSingleNode("default:url", nsmanager);
                        if (urlNode != null)
                        {
                            resultItem.URL = urlNode.InnerText;
                        }

                        XmlNode sourceNode = resultNode.SelectSingleNode("default:source", nsmanager);
                        if (sourceNode != null)
                        {
                            resultItem.Source = sourceNode.InnerText;
                        }

                        XmlNode dateNode = resultNode.SelectSingleNode("default:date", nsmanager);
                        XmlNode timeNode = resultNode.SelectSingleNode("default:time", nsmanager);
                        if (dateNode != null && timeNode != null)
                        {
                            //format <date>2009/11/20</date><time>01:03:57</time> 
                            string dateString = dateNode.InnerText + " " + timeNode.InnerText + " +0";
                            if (dateString != null)
                            {
                                DateTime publishedDate = DateParser.Parse(dateString, "yyyy'/'MM'/'dd HH':'mm':'ss z");
                                resultItem.PublishedDate = publishedDate;
                            }
                        }

                        resultList.Add(resultItem);
                    }
                }
            }
            catch (Exception exception)
            {
                ErrorService.Log("YahooSearchService", "SearchNews", searchContext.ToString(), exception);
            }

            return resultList;
        }
        public SuggestionsList GetSuggestions(SuggestionsContext suggestionsContext)
        {
            SuggestionsList suggestionsList = new SuggestionsList();
            try
            {
                string query = suggestionsContext.Query;

                string urlTemplate = "http://google.com/complete/search?output=toolbar&q={0}";
                string url = string.Format(urlTemplate, query);

                XmlDocument xmlDocument = new XMLConnector().GetXMLDocument(url); ;

                XmlNodeList resultNodes = xmlDocument.SelectNodes("toplevel/CompleteSuggestion");
                if (resultNodes != null)
                {
                    foreach (XmlNode resultNode in resultNodes)
                    {
                        string name = null;
                        double numberQueries = 0;

                        XmlNode suggestionNode = resultNode.SelectSingleNode("suggestion");
                        if (suggestionNode != null)
                        {
                            XmlAttribute dataAttribute = suggestionNode.Attributes["data"];
                            if (dataAttribute != null)
                            {
                                name = dataAttribute.Value;
                            }
                        }

                        XmlNode numQueriesNode = resultNode.SelectSingleNode("num_queries");
                        if (numQueriesNode != null)
                        {
                            XmlAttribute intAttribute = numQueriesNode.Attributes["int"];
                            if (intAttribute != null)
                            {
                                string numQuery = intAttribute.Value;
                                try {
                                    numberQueries = double.Parse(numQuery);
                                } catch (Exception) {}
                            }
                        }

                        if (name != null && numberQueries > 0)
                        {
                            SuggestionItem suggestionItem = new SuggestionItem(name);
                            suggestionItem.Provider = ProviderEnum.Google;
                            suggestionItem.NumberQueries = numberQueries;
                            suggestionItem.Weight = CalculateWeight(numberQueries);
                            suggestionsList.Add(suggestionItem);
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                ErrorService.Log("GoogleSuggestionsService", "GetSuggestions", suggestionsContext.ToString(), exception);
            }

            return suggestionsList;
        }