public SearchResultList Search(SearchContext searchContext)
        {
            SearchResultList resultList = null;
            if (hasInsufficientBalance)
            {
                LogService.Warn(this.GetType(),"Insufficient blance for Bing search request");
            }
            else
            {
                switch (searchContext.SearchType)
                {
                    case SearchTypeEnum.News:
                        resultList = SearchNews(searchContext);
                        break;

                    case SearchTypeEnum.Web:
                        resultList = SearchWeb(searchContext);
                        break;

                    case SearchTypeEnum.Image:
                        resultList = SearchImages(searchContext);
                        break;

                    default:
                        resultList = new SearchResultList();
                        break;
                }
            }
            return resultList;
        }
        public void TwitterSearchWorldRecord()
        {
            string query = "World Record";
            int page = 1;
            int count = 8;
            SearchContext searchContext = new SearchContext(SearchTypeEnum.Tweet, query, page, count);

            SearchResultList searchResultList = new TwitterSearchService().Search(searchContext);
            int result = searchResultList.Count();
            Assert.That(result > 0);
        }
        public void TwitterSearchInvalidCaracters()
        {
            string query = "??37.2";
            int page = 1;
            int count = 8;
            SearchContext searchContext = new SearchContext(SearchTypeEnum.Tweet, query, page, count);

            SearchResultList searchResultList = new TwitterSearchService().Search(searchContext);
            int result = searchResultList.Count();
            System.Console.WriteLine("result : " + result);
            Assert.That(result > 0);
        }
        public void BingSearchNewsTest()
        {
            string query = "Test";
            int page = 1;
            int count = 8;
            SearchContext searchContext = new SearchContext(SearchTypeEnum.News, query, page, count);

            SearchResultList searchResultList = new BingSearchService().Search(searchContext);
            int result = searchResultList.Count();
            Assert.That(result > 0);

            foreach (SearchResultItem item in searchResultList)
            {
                Console.WriteLine(item.Title);
            }

            Console.ReadLine();
        }
        public void FlickrSeachTest()
        {
            string query = "Lady Gaga";
            int page = 1;
            int count = 1;
            SearchContext searchContext = new SearchContext(SearchTypeEnum.Image, query, page, count);

            SearchResultList searchResultList = new FlickrSearchService().Search(searchContext);
            int result = searchResultList.Count();
            Assert.That(result > 0);

            foreach (SearchResultItem item in searchResultList)
            {
                Console.WriteLine(item.ImageURL);
            }

            Console.ReadLine();
        }
        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 accountKey = ConfigService.GetConfig(ConfigKeys.BING_API_KEY, "");

                BingSearchContainer bingContainer = new BingSearchContainer(new Uri("https://api.datamarket.azure.com/Bing/Search/News"));
                bingContainer.Credentials = new NetworkCredential(accountKey, accountKey);

                DataServiceQuery<NewsResult> newsQuery = bingContainer.News(query, "en-us", "strict", null, null, null, null, null);
                IEnumerable<NewsResult> newsResults = newsQuery.Execute();
                foreach (NewsResult result in newsResults)
                {
                    SearchResultItem searchResultItem = new SearchResultItem();
                    searchResultItem.Provider = ProviderEnum.Bing;
                    searchResultItem.Title = result.Title;
                    searchResultItem.URL = result.Url;
                    searchResultItem.Source = result.Source;
                    searchResultItem.PublishedDate = (DateTime) result.Date;
                    string abstractText = result.Description;
                    abstractText = TextCleaner.StripTag(abstractText, "<", ">"); // remove any html tags
                    abstractText = TextCleaner.RemoveHtml(abstractText);
                    searchResultItem.Abstract = abstractText;
                    resultList.Add(searchResultItem);

                }
            }
            catch (Exception exception)
            {
                ErrorService.Log("BingSearchService", "SearchNews", searchContext.ToString(), exception);
                if (exception.ToString().Contains("Insufficient balance"))
                {
                    hasInsufficientBalance = true;
                }
            }

            return resultList;
        }
        public SearchResultList Search(SearchContext searchContext)
        {
            SearchResultList resultList = null;
            switch (searchContext.SearchType)
            {
                case SearchTypeEnum.News:
                    resultList = SearchNews(searchContext);
                    break;

                case SearchTypeEnum.Web:
                    resultList = SearchWeb(searchContext);
                    break;

                default:
                    return resultList;
            }

            return resultList;
        }
        public SearchResultList Search(SearchContext searchContext)
        {
            SearchResultList resultList = new SearchResultList();

            try
            {
                string urlTemplate;
                switch (searchContext.SearchType)
                {
                    case SearchTypeEnum.News:
                        urlTemplate = "http://ajax.googleapis.com/ajax/services/search/news?v={0}&rsz=large&key={1}&q={2}&start={3}";
                        break;

                    case SearchTypeEnum.Web:
                        urlTemplate = "http://ajax.googleapis.com/ajax/services/search/web?v={0}&rsz=large&key={1}&q={2}&start={3}";
                        break;

                    default:
                        return resultList;
                }


                string api = ConfigService.GetConfig(ConfigKeys.GOOGLE_API_KEY, "");
                string query = searchContext.Query;
                int countPerPage = ConfigService.GetConfig(ConfigKeys.THEINTERNETBUZZ_SEARCH_COUNT_PER_PAGE_PER_PROVIDER, 8);
                int page = (searchContext.Page - 1) * countPerPage;

                string url = string.Format(urlTemplate, API_VERSION, api, HttpUtility.UrlEncode(query), page);

                JSONConnector JSONConnector = new JSONConnector();
                JObject searchResultsJSONObject = JSONConnector.GetJSONObject(url);

                new GoogleSearchParser().Parse(searchResultsJSONObject, resultList);
            }
            catch (Exception exception)
            {
                ErrorService.Log("GoogleSearchService", "Search", searchContext.ToString(), exception);
            }

            return resultList;
        }
        public SearchResultList Search(SearchContext searchContext)
        {
            SearchResultList resultList = new SearchResultList();

            try
            {
                string urlTemplate;
                switch (searchContext.SearchType)
                {
                    case SearchTypeEnum.Tweet:
                        urlTemplate = "https://api.twitter.com/1.1/search/tweets.json?q={0}&count={1}&result_type=mixed&include_entities=false";
                        break;

                    default:
                        return resultList;
                }

                TwitterCredentials accessToken = new TwitterAuthService().Authenticate();
                string headerName = "Authorization";
                string headerValue = accessToken.User + " " + accessToken.Token;


                string query = searchContext.Query;
                int countPerPage = ConfigService.GetConfig(ConfigKeys.THEINTERNETBUZZ_SEARCH_COUNT_PER_PAGE_PER_PROVIDER, 8);
                int page = searchContext.Page;

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

                JSONConnector JSONConnector = new JSONConnector();
                JObject searchResultsJSONObject = JSONConnector.GetJSONObjectWithHeader(url, headerName, headerValue);

                new TwitterSearchParser().Parse(searchResultsJSONObject, resultList);
            }
            catch (Exception exception)
            {
                ErrorService.Log("TwitterSearchService", "Search", searchContext.ToString(), exception);
            }

            return resultList;
        }
        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 SearchImages(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 accountKey = ConfigService.GetConfig(ConfigKeys.BING_API_KEY, "");

                BingSearchContainer bingContainer = new BingSearchContainer(new Uri("https://api.datamarket.azure.com/Bing/Search/Images/"));
                bingContainer.Credentials = new NetworkCredential(accountKey, accountKey);

                DataServiceQuery<ImageResult> webQuery = bingContainer.Image(query, "en-us", "strict", null, null, null);
                IEnumerable<ImageResult> imageResults = webQuery.Execute();
                foreach (ImageResult result in imageResults)
                {
                    SearchResultItem searchResultItem = new SearchResultItem();
                    searchResultItem.Provider = ProviderEnum.Bing;
                    searchResultItem.Title = result.Title;
                    searchResultItem.URL = result.MediaUrl;
                    searchResultItem.ImageURL = result.MediaUrl;
                    searchResultItem.Abstract = result.Title;
                    resultList.Add(searchResultItem);
                }
            }
            catch (Exception exception)
            {
                ErrorService.Log("BingSearchService", "SearchImages", searchContext.ToString(), exception);
                if (exception.ToString().Contains("Insufficient balance"))
                {
                    hasInsufficientBalance = true;
                }
            }

            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;
        }
示例#13
0
        public SearchResultList Search(SearchContext searchContext)
        {
            AuditServiceItem auditServiceItem = AuditService.Register("SearchService", "Search", searchContext.ToString());
            AuditService.Start(auditServiceItem);

            SearchTypeEnum searchType = searchContext.SearchType;
            string query = searchContext.Query;
            int page = searchContext.Page;
            int count = searchContext.Count;

            SearchResultList resultList = SearchCacheHelper.ReadSearchResultList(searchType, query, page);

            if (resultList == null)
            {
                resultList = new SearchResultList();

                try
                {
                    if (searchType == SearchTypeEnum.Web || searchType == SearchTypeEnum.News || searchType == SearchTypeEnum.Tweet)
                    {
                        ManualResetEvent[] doneEvents = new ManualResetEvent[1];

                        //doneEvents[0] = new ManualResetEvent(false);
                        //SearchContext bingSearchContext = new SearchContext(ProviderEnum.Bing, searchType, query, page, count);
                        //SearchWorkerThread bingWorkerThread = new SearchWorkerThread(bingSearchContext, doneEvents[0]);
                        //ThreadPool.QueueUserWorkItem(bingWorkerThread.ThreadPoolCallback);

                        doneEvents[0] = new ManualResetEvent(false);
                        SearchContext twitterSearchContext = new SearchContext(ProviderEnum.Twitter, searchType, query, page, count);
                        SearchWorkerThread twitterWorkerThread = new SearchWorkerThread(twitterSearchContext, doneEvents[0]);
                        ThreadPool.QueueUserWorkItem(twitterWorkerThread.ThreadPoolCallback);

                        //doneEvents[1] = new ManualResetEvent(false);
                        //SearchContext googleSearchContext = new SearchContext(ProviderEnum.Google, searchType, query, page, count);
                        //SearchWorkerThread googleWorkerThread = new SearchWorkerThread(googleSearchContext, doneEvents[1]);
                        //ThreadPool.QueueUserWorkItem(googleWorkerThread.ThreadPoolCallback);

                        //doneEvents[2] = new ManualResetEvent(false);
                        //SearchContext yahooSearchContext = new SearchContext(ProviderEnum.Yahoo, searchType, query, page, count);
                        //SearchWorkerThread yahooWorkerThread = new SearchWorkerThread(yahooSearchContext, doneEvents[2]);
                        //ThreadPool.QueueUserWorkItem(yahooWorkerThread.ThreadPoolCallback);

                        WaitHandle.WaitAll(doneEvents);

                        //if (googleWorkerThread.SearchResultList != null) resultList.AddResults(googleWorkerThread.SearchResultList);
                        //if (bingWorkerThread.SearchResultList != null) resultList.AddResults(bingWorkerThread.SearchResultList);
                        //if (yahooWorkerThread.SearchResultList != null) resultList.AddResults(yahooWorkerThread.SearchResultList);
                        if (twitterWorkerThread.SearchResultList != null) resultList.AddResults(twitterWorkerThread.SearchResultList);

                        if (searchType == SearchTypeEnum.News)
                        {
                            FilterNewsByTitle(resultList, query);
                        }
                    }
                    else if (searchType == SearchTypeEnum.Image)
                    {
                        ManualResetEvent[] doneEvents = new ManualResetEvent[1];

                        doneEvents[0] = new ManualResetEvent(false);
                        SearchContext flickrSearchContext = new SearchContext(ProviderEnum.Flickr, searchType, query, page, count);
                        SearchWorkerThread flickrWorkerThread = new SearchWorkerThread(flickrSearchContext, doneEvents[0]);
                        ThreadPool.QueueUserWorkItem(flickrWorkerThread.ThreadPoolCallback);

                        //doneEvents[0] = new ManualResetEvent(false);
                        //SearchContext bingSearchContext = new SearchContext(ProviderEnum.Bing, searchType, query, page, count);
                        //SearchWorkerThread bingWorkerThread = new SearchWorkerThread(bingSearchContext, doneEvents[0]);
                        //ThreadPool.QueueUserWorkItem(bingWorkerThread.ThreadPoolCallback);

                        //doneEvents[0] = new ManualResetEvent(false);
                        //SearchContext twitterSearchContext = new SearchContext(ProviderEnum.Twitter, searchType, query, page, count);
                        //SearchWorkerThread twitterWorkerThread = new SearchWorkerThread(twitterSearchContext, doneEvents[0]);
                        //ThreadPool.QueueUserWorkItem(twitterWorkerThread.ThreadPoolCallback);

                        //doneEvents[1] = new ManualResetEvent(false);
                        //SearchContext googleSearchContext = new SearchContext(ProviderEnum.Google, searchType, query, page, count);
                        //SearchWorkerThread googleWorkerThread = new SearchWorkerThread(googleSearchContext, doneEvents[1]);
                        //ThreadPool.QueueUserWorkItem(googleWorkerThread.ThreadPoolCallback);

                        //doneEvents[2] = new ManualResetEvent(false);
                        //SearchContext yahooSearchContext = new SearchContext(ProviderEnum.Yahoo, searchType, query, page, count);
                        //SearchWorkerThread yahooWorkerThread = new SearchWorkerThread(yahooSearchContext, doneEvents[2]);
                        //ThreadPool.QueueUserWorkItem(yahooWorkerThread.ThreadPoolCallback);

                        WaitHandle.WaitAll(doneEvents);

                        //if (googleWorkerThread.SearchResultList != null) resultList.AddResults(googleWorkerThread.SearchResultList);
                        if (flickrWorkerThread.SearchResultList != null) resultList.AddResults(flickrWorkerThread.SearchResultList);
                        //if (yahooWorkerThread.SearchResultList != null) resultList.AddResults(yahooWorkerThread.SearchResultList);
                        //if (twitterWorkerThread.SearchResultList != null) resultList.AddResults(twitterWorkerThread.SearchResultList);

                    }
                    SearchCacheHelper.CacheSearchResultList(searchType, query, page, resultList);
                }
                catch (Exception exception)
                {
                    ErrorService.Log("SearchService", "Search", searchContext.ToString(), exception);
                    return null;
                }
            }

            AuditService.End(auditServiceItem);

            return resultList;
        }
示例#14
0
        static private void PreLoadTrendsImage(TrendsList trendsList)
        {
            if (trendsList != null)
            {
                SearchService searchService = new SearchService();

                foreach (TrendItem trendItem in trendsList)
                {
                    if (trendItem.ImageURL == null)
                    {
                        String query = trendItem.Title;
                        SearchContext searchContext = new SearchContext(query, 1, 1);
                        searchContext.SearchType = SearchTypeEnum.Image;
                        SearchResultList searchResultList = searchService.Search(searchContext);
                        if (searchResultList.Count() > 0)
                        {
                            SearchResultItem item = (SearchResultItem)searchResultList.Item(0);
                            trendItem.ImageURL = item.ImageURL;
                        }
                    }

                    if (trendItem.TileImageURL == null && trendItem.ImageURL != null)
                    {
                        int imageSize;
                        switch (trendItem.Weight)
                        {
                            case 1: imageSize = 150;
                                break;
                            case 2: imageSize = 150;
                                break;
                            case 3: imageSize = 150;
                                break;
                            case 4: imageSize = 150;
                                break;
                            case 5: imageSize = 150;
                                break;
                            default: imageSize = 150;
                                break;
                        }

                        string url = "http://src.sencha.io/" + imageSize + "/" + trendItem.ImageURL;

                        HttpStatusCode status = new HTTPConnector().GetStatus(url, "image/jpeg,image/gif.image/png");
                        if (status == HttpStatusCode.OK)
                        {
                            trendItem.TileImageURL = url;
                        }
                    }
                }
            }
        }
示例#15
0
        static private void PreLoadTrends(TrendsList trendsList)
        {
            if (trendsList != null)
            {
                TopicService topicService = new TopicService();
                SearchService searchService = new SearchService();
                VideoService videoService = new VideoService();

                foreach (TrendItem trendItem in trendsList)
                {
                    String query = trendItem.Title;

                    topicService.GetTopic(query);

                    int pageMax = ConfigService.GetConfig(ConfigKeys.THEINTERNETBUZZ_SEARCH_COUNT_PER_PAGE_PER_PROVIDER, 8);
                    SearchContext searchContext = new SearchContext(query, 1, pageMax);
                    searchContext.SearchType = SearchTypeEnum.Web;
                    searchService.Search(searchContext);
                    searchContext.SearchType = SearchTypeEnum.News;
                    searchService.Search(searchContext);

                    videoService.Search(query);
                }
            }
        }
 public SearchWorkerThread(SearchContext searchContext, ManualResetEvent doneEvent)
 {
     this.SearchContext = searchContext;
     this.DoneEvent = doneEvent;
 }
示例#17
0
 public SearchCommand(SearchContext searchContext)
 {
     this.searchContext = searchContext;
 }
示例#18
0
 public SearchCommand(SearchTypeEnum searchType, string query, int page, int count)
 {
     this.searchContext = new SearchContext(searchType, query, page, count);
 }