use the youtube api to find the most popular pewdiepie videos in csharp

To find the most popular PewDiePie videos using YouTube API in C#, you need to follow these steps:

  1. Install the YouTube API NuGet package by running the following command in the Package Manager Console:
main.cs
Install-Package Google.Apis.YouTube.v3
39 chars
2 lines
  1. Create a new YouTubeService and authenticate it using your API key:
main.cs
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
    ApiKey = "YOUR_API_KEY_HERE"
});
115 chars
5 lines
  1. Use the YouTube API's search endpoint to search for PewDiePie videos:
main.cs
var searchListRequest = youtubeService.Search.List("id,snippet");
searchListRequest.Q = "PewDiePie";
searchListRequest.Type = "video";
searchListRequest.VideoDefinition = SearchResource.ListRequest.VideoDefinitionEnum.High;
searchListRequest.MaxResults = 50;
searchListRequest.Order = SearchResource.ListRequest.OrderEnum.ViewCount;
var searchListResponse = searchListRequest.Execute();
387 chars
8 lines
  1. For each video in the response, use the YouTube API's videos endpoint to get more details (e.g. view count, likes, etc.):
main.cs
foreach (var searchResult in searchListResponse.Items)
{
    var videoListRequest = youtubeService.Videos.List("snippet,statistics");
    videoListRequest.Id = searchResult.Id.VideoId;
    var videoListResponse = videoListRequest.Execute();
    
    var video = videoListResponse.Items.FirstOrDefault();
    if (video != null)
    {
        Console.WriteLine($"Title: {video.Snippet.Title}");
        Console.WriteLine($"Views: {video.Statistics.ViewCount}");
        Console.WriteLine($"Likes: {video.Statistics.LikeCount}");
        Console.WriteLine($"Dislikes: {video.Statistics.DislikeCount}");
        Console.WriteLine();
    }
}
637 chars
17 lines
  1. Process the data as needed (e.g. sort the videos by view count to find the most popular ones).

Note: This code uses web requests to interact with the YouTube API. Depending on your authentication strategy, you may need to use a different approach (e.g. OAuth).

gistlibby LogSnag