1
Answer

Like,View,Comments to be displayed

Photo of Ramco Ramco

Ramco Ramco

Mar 27
176
1

Hi

  I have below code to list my Videos. I want like,Views,Comments also to be displayed.

protected void MyOwnVideos11()
{
    StringBuilder htmlTable = new StringBuilder();
    string nextPageToken = string.Empty;
    Int32 Sr = 1;
    var data = GetVideosList(nextPageToken);
    var orderByPublishAt = data.items.OrderBy(x => x.snippet.publishedAt);

    htmlTable.Append("<table class='table table-bordered table-hover datatable-highlight' id='tbldata'>");
    htmlTable.Append("<thead><tr><th>Sr.</th><th>Video Title</th><th>Description</th><th>Views</th><th>Like</th><th>Comment</th><th>Published</th></tr></thead>");
    htmlTable.Append("<tbody>");

    nextPageToken = data.nextPageToken;
    while (!string.IsNullOrEmpty(nextPageToken))
    {
        data = GetVideosList(nextPageToken);
        orderByPublishAt = data.items.OrderBy(x => x.snippet.publishedAt);
        nextPageToken = data.nextPageToken;
        foreach (var item in orderByPublishAt)
        {
            htmlTable.Append("<tr>");
            htmlTable.Append("<td>" + Sr + "</td>");
            htmlTable.Append("<td>" + item.snippet.title + "</td>");
            htmlTable.Append("<td>" + item.snippet.description + "</td>");
            htmlTable.Append("<td>" + item.snippet.publishedAt.ToString("dd-MM-yyyy") + "</td>");
            htmlTable.Append("</tr>");
            Sr = Sr + 1;
        }
    }
    htmlTable.Append("</tbody>");
    htmlTable.Append("</table>");
    PlaceHolderTable.Controls.Add(new Literal { Text = htmlTable.ToString() });
}

private static YoutubeSearchListResponse GetVideosList(string nextPageToken)
{
    var client = new RestClient("googleapis.com/youtube/v3");
    var request = new RestRequest("search", Method.GET);
    request.AddParameter("part", "snippet");
    request.AddParameter("type", "video");
    request.AddParameter("channelId", "UoaVw");
    request.AddParameter("key", "AIzaCFG8");
    if (!string.IsNullOrEmpty(nextPageToken))
    {
        request.AddParameter("pageToken", nextPageToken);
    }
    var response = client.Execute<YoutubeSearchListResponse>(request);

    return response.Data;
}
C#

Thanks

Answers (1)

0
Photo of Sangeetha S
258 7.5k 321k Apr 03
  1. Update the API request to include statistics.
  2. Modify the HTML table to display likes, views, and comments.

Here's the updated code:

protected void MyOwnVideos11()
{
    StringBuilder htmlTable = new StringBuilder();
    string nextPageToken = string.Empty;
    Int32 Sr = 1;
    var data = GetVideosList(nextPageToken);
    var orderByPublishAt = data.items.OrderBy(x => x.snippet.publishedAt);

    htmlTable.Append("<table class='table table-bordered table-hover datatable-highlight' id='tbldata'>");
    htmlTable.Append("<thead><tr><th>Sr.</th><th>Video Title</th><th>Description</th><th>Views</th><th>Likes</th><th>Comments</th><th>Published</th></tr></thead>");
    htmlTable.Append("<tbody>");

    nextPageToken = data.nextPageToken;
    while (!string.IsNullOrEmpty(nextPageToken))
    {
        data = GetVideosList(nextPageToken);
        orderByPublishAt = data.items.OrderBy(x => x.snippet.publishedAt);
        nextPageToken = data.nextPageToken;
        foreach (var item in orderByPublishAt)
        {
            htmlTable.Append("<tr>");
            htmlTable.Append("<td>" + Sr + "</td>");
            htmlTable.Append("<td>" + item.snippet.title + "</td>");
            htmlTable.Append("<td>" + item.snippet.description + "</td>");
            htmlTable.Append("<td>" + item.statistics.viewCount + "</td>");
            htmlTable.Append("<td>" + item.statistics.likeCount + "</td>");
            htmlTable.Append("<td>" + item.statistics.commentCount + "</td>");
            htmlTable.Append("<td>" + item.snippet.publishedAt.ToString("dd-MM-yyyy") + "</td>");
            htmlTable.Append("</tr>");
            Sr = Sr + 1;
        }
    }

    htmlTable.Append("</tbody>");
    htmlTable.Append("</table>");
    PlaceHolderTable.Controls.Add(new Literal { Text = htmlTable.ToString() });
}

private static YoutubeSearchListResponse GetVideosList(string nextPageToken)
{
    var client = new RestClient("https://www.googleapis.com/youtube/v3");
    var request = new RestRequest("search", Method.GET);
    request.AddParameter("part", "snippet,statistics");
    request.AddParameter("type", "video");
    request.AddParameter("channelId", "UoaVw");
    request.AddParameter("key", "AIzaCFG8");
    if (!string.IsNullOrEmpty(nextPageToken))
    {
        request.AddParameter("pageToken", nextPageToken);
    }
    var response = client.Execute<YoutubeSearchListResponse>(request);

    return response.Data;
}

This code will now include the view count, like count, and comment count for each video in your HTML table.