Example #1
0
 /// <summary>
 /// Gets the set of valid time arguments for a standard feed.
 /// </summary>
 /// <param name="feedId">The standard feed ID.</param>
 /// <returns>An array of valid times. The array is always different from null, but it may be empty.</returns>
 public static YouTubeTimeId[] GetValidTime(YouTubeStandardFeed feedId)
 {
     return YouTubeUri.timeFeedValidity[(int)feedId];
 }
Example #2
0
        /// <summary>
        /// Creates a standard feed URI.
        /// </summary>
        /// <param name="feedId">The feed ID.</param>
        /// <param name="regionId">The region ID, it can be null.</param>
        /// <param name="category">The category, it can be null.</param>
        /// <param name="timeId">The time ID, it can be null.</param>
        /// <param name="startIndex">The start index, it can be null.</param>
        /// <param name="maxResults">The max results, it can be null.</param>
        /// <returns>A URI for the selected standard feed.</returns>
        public static Uri GetStandardFeed(
			YouTubeStandardFeed feedId,
			string regionId,
			string category,
			YouTubeTimeId? timeId,
			int? startIndex,
			int? maxResults
			)
        {
            // Feed ID
            string uriFeedId = YouTubeUri.standardFeedIds[(int)feedId];

            // Region ID
            string uriRegionId = null == regionId ? string.Empty : regionId + "/";

            // Category
            string uriCategory = null == category ? string.Empty : "_" + category;

            StringBuilder builder = new StringBuilder(uriStandardVideoFeedPattern.FormatWith(uriRegionId, uriFeedId, uriCategory));

            if (timeId != null)
            {
                builder.Append("&time=" + YouTubeUri.timeIds[(int)timeId]);
            }
            if (startIndex != null)
            {
                builder.Append("&start-index=" + startIndex);
            }
            if (maxResults != null)
            {
                builder.Append("&max-results=" + maxResults);
            }

            return new Uri(builder.ToString());
        }
        // Private methods.
        /// <summary>
        /// Crawls the feed at the specified parameters.
        /// </summary>
        /// <param name="asyncResult">The asynchronous result.</param>
        /// <param name="feedId">The feed.</param>
        /// <param name="timeId">The time.</param>
        /// <param name="category">The category.</param>
        /// <param name="regionId">The region.</param>
        /// <param name="obj">The standard feed object.</param>
        /// <returns>The crawl result.</returns>
        private CrawlResult CrawlFeed(
			SpiderAsyncResult asyncResult,
			YouTubeStandardFeed feedId,
			YouTubeTimeId timeId,
			string category,
			string regionId,
			ref DbObjectStandardFeed obj)
        {
            // If the asynchronousn operation has been canceled, do nothing.
            if (asyncResult.IsCanceled) return CrawlResult.Canceled;

            // Compute the feed key.
            string key = this.EncodeFeedKey(feedId, timeId, category, regionId);

            // Compute the feed URI starting at index 1 and ask for 1 result.
            Uri uri = YouTubeUri.GetStandardFeed(feedId, regionId, category, timeId, 1, 1);

            // Create a new video request.
            YouTubeRequestFeed<Video> request = new YouTubeRequestFeed<Video>(this.crawler.YouTube.Settings);

            // Set the feed URL.
            obj.Url = uri.AbsoluteUri;

            try
            {
                // Begin an asynchronous request for the standard feed.
                AsyncWebResult result = request.Begin(uri, (AsyncWebResult webResult) => { }) as AsyncWebResult;

                // Add the result of the web operation to the collection of web requests.
                AsyncWebOperation operation = asyncResult.AddAsyncWeb(request, result);

                // Wait for the asynchronous operation to complete.
                result.AsyncWaitHandle.WaitOne();

                // Remove the result of the web operation from the collection of web requests.
                asyncResult.RemoveAsyncWeb(operation);

                // Complete the request and get the video feed.
                Feed<Video> feed = request.End(result);

                // If the operation completed successfully, set the browsable to true.
                obj.Browsable = true;
                // Set the response HTTP code.
                obj.HttpCode = (int)(result as AsyncWebResult).Response.StatusCode;

                // Return the result.
                return (feed.FailuresAtom.Count == 0) && (feed.FailuresEntry.Count == 0) ? CrawlResult.Success : CrawlResult.Warning;
            }
            catch (WebException exception)
            {
                if (exception.Status == WebExceptionStatus.RequestCanceled)
                {
                    return CrawlResult.Canceled;
                }
                else
                {
                    // If the operation failed with a web exception, set the browsable to false.
                    obj.Browsable = false;
                    // Set the response HTTP code.
                    obj.HttpCode = (int)(exception.Response as HttpWebResponse).StatusCode;
                    // Return the result.
                    return CrawlResult.Fail;
                }
            }
            catch (Exception)
            {
                // If the operation failed with a web exception, set the browsable to false.
                obj.Browsable = false;
                // Set the response HTTP code to null.
                obj.HttpCode = null;
                // Return the result.
                return CrawlResult.Fail;
            }
        }
 /// <summary>
 /// Sets whether the specified standard feed is selected.
 /// </summary>
 /// <param name="feed">The standard feed.</param>
 /// <param name="selected"><b>True</b> if the standard feed is selected, <b>false</b> otherwise.</param>
 public void SetFeedSelected(YouTubeStandardFeed feed, bool selected)
 {
     DotNetApi.Windows.RegistryExtensions.SetBoolean(this.crawler.Config.SpidersConfigPath + @"\StandardFeeds", feed.ToString(), selected);
 }
 /// <summary>
 /// Gets whether the specified standard feed is selected.
 /// </summary>
 /// <param name="feed">The standard feed.</param>
 /// <returns><b>True</b> if the standard feed is selected, <b>false</b> otherwise.</returns>
 public bool GetFeedSelected(YouTubeStandardFeed feed)
 {
     return DotNetApi.Windows.RegistryExtensions.GetBoolean(this.crawler.Config.SpidersConfigPath + @"\StandardFeeds", feed.ToString(), false);
 }
 /// <summary>
 /// Computes the feed key for a set of feed parameters.
 /// </summary>
 /// <param name="feedId">The feed ID.</param>
 /// <param name="timeId">The time ID.</param>
 /// <param name="category">The category.</param>
 /// <param name="regionId">The region ID.</param>
 /// <returns>The feed key.</returns>
 public string EncodeFeedKey(YouTubeStandardFeed feedId, YouTubeTimeId timeId, string category, string regionId)
 {
     return "{0}.{1}.{2}.{3}".FormatWith(
         (int)feedId,
         (int)timeId,
         category != null ? category : string.Empty,
         regionId != null ? regionId : string.Empty);
 }