Пример #1
0
        public async Task <IEnumerable <YoutubeVideo> > GetVideosAsync(string channelId, DateTimeOffset publishedAfter)
        {
            string nextPageToken = null;
            var    results       = new List <YoutubeVideo>();

            do
            {
                var request = Service.Search.List("snippet");
                request.ChannelId      = channelId;
                request.MaxResults     = 50;
                request.Fields         = "nextPageToken,items(id(kind,videoId),snippet(channelId,title,publishedAt,thumbnails(medium,default)))";
                request.Order          = SearchResource.ListRequest.OrderEnum.Date;
                request.PublishedAfter = publishedAfter.UtcDateTime;
                request.SafeSearch     = SearchResource.ListRequest.SafeSearchEnum.None;
                request.Type           = "video";
                if (nextPageToken != null)
                {
                    request.PageToken = nextPageToken;
                }

                var response = await request.ExecuteAsync();

                nextPageToken = response.NextPageToken;
                foreach (var item in response.Items)
                {
                    if (item.Id.Kind != "youtube#video")
                    {
                        continue;
                    }

                    // YouTube API is just ignoring this parameter now.
                    DateTimeOffset publishedAt =
                        new DateTimeOffset(item.Snippet.PublishedAt.Value);
                    if (publishedAt <= publishedAfter)
                    {
                        continue;
                    }

                    var result = new YoutubeVideo
                    {
                        ChannelId   = item.Snippet.ChannelId,
                        Id          = item.Id.VideoId,
                        Title       = item.Snippet.Title,
                        Duration    = TimeSpan.FromMinutes(5),
                        PublishedAt = new DateTimeOffset(item.Snippet.PublishedAt.Value),
                        Thumbnail   = PickThumbnail(item.Snippet.Thumbnails)
                    };
                    results.Add(result);
                }
            } while (nextPageToken != null);

            return(results);
        }
Пример #2
0
        private static SqlDataRecord CreateVideoDataRecord(YoutubeVideo video)
        {
            int index      = 0;
            var dataRecord = new SqlDataRecord(VideoMetaData);

            dataRecord.SetString(index++, video.ChannelId);
            dataRecord.SetString(index++, video.Id);
            dataRecord.SetString(index++, video.Title);
            dataRecord.SetInt64(index++, video.Duration.Ticks);
            dataRecord.SetDateTimeOffset(index++, video.PublishedAt);
            dataRecord.SetString(index++, video.Thumbnail);
            return(dataRecord);
        }