Example #1
0
        private bool GetSerachResults(YouTubeAPI youTubeAPI, string categoryName, YouTubeSearchRequest searchRequest)
        //================================================================================================================
        // Retrieve search results based on the given search request
        //
        // Parameters
        //      youTubeAPI:    YouTube API  options
        //      categoryName:  Category being searched
        //      searchRequest: YouTube search request data
        //
        // Retruns
        //      True/False, has the YouTube data limit been exceeded
        //
        // Outputs
        //      The SearchResults list in the searchRequest object is populated
        //
        // Developer Notes
        //      To avoid exceeding the YouTube data limits, search results are chached locally in the session and also
        //      written to the database. Retrieval priority is as follows...
        //          1) Results are taken from the local cache, if available
        //          2) Results are retrieved from YouTube
        //          3) Results are retrieved from the database, If the data limit has been exceeded
        //
        //          NOTE: A data limnit exceeded flag is also stored in the session. This value is checked first, before
        //                connecting to YouTube to avoid unnecessary data limit exceeded errors.
        //================================================================================================================
        {
            bool dataLimitExceeded = false;

            // Results are loaded from the database...
            //      If the developer flag UseDatabaseFirst is set
            //      -- OR --
            //      If our YouTube API data limit has been exceeded and the local cache is empty
            if ((youTubeAPI.UseDatabaseFirst) || ((youTubeAPI.DataLimitExceeded) && (Session[searchRequest.SessionKey] == null)))
            {
                dataLimitExceeded = (GetSerachResultsFromDatabase(searchRequest) == false);
            }
            else
            {
                // Results are taken from the local cache, if available
                if (Session[searchRequest.SessionKey] != null)
                {
                    searchRequest.SearchResults = (List <YouTubeSearchResult>)Session[searchRequest.SessionKey];
                }
                else
                {
                    // If the YouTube data limit has not been exceeded, Results are retrieved from YouTube
                    // Otherwise, results are retrieved from the database
                    dataLimitExceeded = (Session[YT_LIMIT_EXCEEDED] != null);
                    if (!dataLimitExceeded)
                    {
                        // If the data limit has been exceeded, results are retrieved from the database, if available
                        dataLimitExceeded = SearchYouTube(searchRequest);
                        if (dataLimitExceeded)
                        {
                            dataLimitExceeded          = (GetSerachResultsFromDatabase(searchRequest) == false);
                            Session[YT_LIMIT_EXCEEDED] = true;
                        }
                    }
                }
            }

            // Select the default video
            if (!dataLimitExceeded)
            {
                youTubeAPI.SelectedCategory      = String.Format(Properties.Resources.PokerTopTen, categoryName);
                youTubeAPI.SelectedVideoURL      = searchRequest.SearchResults[0].YouTubeURL;
                youTubeAPI.SelectedVideoResultNo = searchRequest.SearchResults[0].ResultNo;
            }

            return(dataLimitExceeded);
        }