public TrendsList GetTrends()
        {
            TrendsList trendsList = new TrendsList();

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

                JSONConnector JSONConnector = new JSONConnector();
                JArray currentTrendsArray = JSONConnector.GetJSONArrayWithHeader(CURRENT_TRENDS_URL, headerName, headerValue);

                new TwitterTrendsParser().Parse(currentTrendsArray, currentTrendsList);
            }
            catch (Exception exception)
            {
                ErrorService.Log("TwitterTrendsService", "GetTrends", null, exception);
            }

            if (twitterTrendsQueue.Count >= QUEUE_SIZE)
            {
                twitterTrendsQueue.Dequeue();
            }
            twitterTrendsQueue.Enqueue(currentTrendsList);
            foreach (TrendsList twitterTrendsList in twitterTrendsQueue)
            {
                trendsList.AddTrends(twitterTrendsList);
            }

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

            if (query.Length > 0)
            {
                try
                {
                    string urlTemplate = "https://www.googleapis.com/freebase/v1/topic/en/{0}?key={1}&{2}";
                    string apiKey = ConfigService.GetConfig(ConfigKeys.GOOGLE_API_KEY, "");
                    string[] domains = new string[] { 
                                         "/common/topic/alias", 
                                         "/common/topic/description", 
                                         "/common/topic/image",
                                         "/common/topic/official_website",
                                         "/common/topic/social_media_presence",
                                         "/influence/influence_node/influenced_by", 
                                         "/people/person/date_of_birth"
                    };
                    string filters = "filter=" + String.Join("&filter=", domains);

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

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

                    new FreebaseTopicParser().Parse(searchResultsJSONObject, topicItem);
                }
                catch (Exception exception)
                {
                    ErrorService.Log("FreebaseTopicService", "FillTopic", topicItem.ToString(), exception);
                }
            }    
        }
        public TwitterCredentials Authenticate()
        {
            if (twitterCredentials == null)
            {
                try
                {
                    string url = "https://api.twitter.com/oauth2/token";

                    string consumerSecret = ConfigService.GetConfig(ConfigKeys.TWITTER_ACCESS_CONSUMER_SECRET, "");
                    string consumerKey = ConfigService.GetConfig(ConfigKeys.TWITTER_ACCESS_CONSUMER_KEY, "");
                    String bearerCredentials = HttpUtility.UrlEncode(consumerKey) + ":" + HttpUtility.UrlEncode(consumerSecret);
                    bearerCredentials = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(bearerCredentials));

                    JSONConnector JSONConnector = new JSONConnector();
                    JObject searchResultsJSONObject = JSONConnector.PostJSONObjectWithHeader(url, "Authorization", "Basic " + bearerCredentials, "grant_type=client_credentials");

                    twitterCredentials = new TwitterAuthParser().Parse(searchResultsJSONObject);
                }
                catch (Exception exception)
                {
                    ErrorService.Log("TwitterSearchService", "Auth", "Authentication Failed", exception);
                }
            }

            return twitterCredentials;
        }
        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 TrendsList GetTrends()
        {
            string urlTemplate;
            switch (trendsType)
            {
                case WhatTheTrendTypeEnum.Current:
                    urlTemplate = "http://api.whatthetrend.com/api/v2/trends.json?api_key={0}";
                    break;

                case WhatTheTrendTypeEnum.MostEdited:
                    urlTemplate = "http://api.whatthetrend.com/api/v2/trends/active.json?api_key={0}";
                    break;

                default:
                    urlTemplate = "http://api.whatthetrend.com/api/v2/trends.json?api_key={0}";
                    break;
            }

            TrendsList trendsList = new TrendsList();

            try
            {
                string api = ConfigService.GetConfig(ConfigKeys.WHATTHETREND_API_KEY, "");
                string url = string.Format(urlTemplate, api);

                JSONConnector JSONConnector = new JSONConnector();
                JObject currentTrendsRootJSONObject = JSONConnector.GetJSONObject(url);
                new WhatTheThrendTrendsParser().Parse(currentTrendsRootJSONObject, trendsList);

            }
            catch (Exception exception)
            {
                ErrorService.Log("WhatTheTrendTrendsService", "GetTrends", urlTemplate, exception);
            }

            return trendsList;
        }