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;
        }
Esempio n. 2
0
        static private TrendsList BuildTrends()
        {
            TrendsList trendsList = null;

            try
            {
                ManualResetEvent[] doneEvents = new ManualResetEvent[8];

                doneEvents[0] = new ManualResetEvent(false);
                TrendsWorkerThread googleWorkerThread = new TrendsWorkerThread(new GoogleTrendsService(), doneEvents[0]);
                ThreadPool.QueueUserWorkItem(googleWorkerThread.ThreadPoolCallback);

                doneEvents[1] = new ManualResetEvent(false);
                TrendsWorkerThread twitterWorkerThread = new TrendsWorkerThread(new TwitterTrendsService(), doneEvents[1]);
                ThreadPool.QueueUserWorkItem(twitterWorkerThread.ThreadPoolCallback);

                doneEvents[2] = new ManualResetEvent(false);
                TrendsWorkerThread yahooWorkerThread = new TrendsWorkerThread(new YahooTrendsService(), doneEvents[2]);
                ThreadPool.QueueUserWorkItem(yahooWorkerThread.ThreadPoolCallback);

                doneEvents[3] = new ManualResetEvent(false);
                TrendsWorkerThread whatTheTrendCurrentWorkerThread = new TrendsWorkerThread(new WhatTheTrendTrendsService(WhatTheTrendTypeEnum.Current), doneEvents[3]);
                ThreadPool.QueueUserWorkItem(whatTheTrendCurrentWorkerThread.ThreadPoolCallback);

                doneEvents[4] = new ManualResetEvent(false);
                TrendsWorkerThread whatTheTrendMostEditedWorkerThread = new TrendsWorkerThread(new WhatTheTrendTrendsService(WhatTheTrendTypeEnum.MostEdited), doneEvents[4]);
                ThreadPool.QueueUserWorkItem(whatTheTrendMostEditedWorkerThread.ThreadPoolCallback);

                doneEvents[5] = new ManualResetEvent(false);
                TrendsWorkerThread alexaWorkerThread = new TrendsWorkerThread(new AlexaTrendsService(), doneEvents[5]);
                ThreadPool.QueueUserWorkItem(alexaWorkerThread.ThreadPoolCallback);

                doneEvents[6] = new ManualResetEvent(false);
                TrendsWorkerThread iTunesSongsWorkerThread = new TrendsWorkerThread(new ITunesTrendsService(ITunesTypeEnum.Top10Songs), doneEvents[6]);
                ThreadPool.QueueUserWorkItem(iTunesSongsWorkerThread.ThreadPoolCallback);

                doneEvents[7] = new ManualResetEvent(false);
                TrendsWorkerThread iTunesMoviesWorkerThread = new TrendsWorkerThread(new ITunesTrendsService(ITunesTypeEnum.TopMovies), doneEvents[7]);
                ThreadPool.QueueUserWorkItem(iTunesMoviesWorkerThread.ThreadPoolCallback);

                WaitHandle.WaitAll(doneEvents);

                trendsList = new TrendsList();
                trendsList.AddTrends(googleWorkerThread.TrendsList);
                trendsList.AddTrends(yahooWorkerThread.TrendsList);
                trendsList.AddTrends(twitterWorkerThread.TrendsList);
                trendsList.AddTrends(whatTheTrendCurrentWorkerThread.TrendsList);
                trendsList.AddTrends(whatTheTrendMostEditedWorkerThread.TrendsList);
                trendsList.AddTrends(alexaWorkerThread.TrendsList);
                trendsList.AddTrends(iTunesSongsWorkerThread.TrendsList);
                trendsList.AddTrends(iTunesMoviesWorkerThread.TrendsList);

                if (trendsList != null && trendsList.Count() > 0)
                {
                    trendsList.Sort(new TrendItemComparer(TrendItemComparerEnum.Title));
                }
            }
            catch (Exception exception)
            {
                ErrorService.Log("TrendsBuilder", "BuildTrends", "", exception);
            }

            return trendsList;
        }