Hi
Few Videos are repeating again & there are 428 Short Videos it is displaying 403
protected void MyOwnVideos11()
{
StringBuilder htmlTable = new StringBuilder();
string nextPageToken = string.Empty;
Int32 Sr = 1;
var data0 = GetVideosList(nextPageToken);
var orderByPublishAt = data0.items.OrderBy(x => x.snippet.publishedAt);
htmlTable.Append("");
htmlTable.Append("Sr. Video Id Video Title Description ");
htmlTable.Append("");
nextPageToken = data0.nextPageToken;
while (!string.IsNullOrEmpty(nextPageToken))
{
data0 = GetVideosList(nextPageToken);
orderByPublishAt = data0.items.OrderBy(x => x.snippet.publishedAt);
nextPageToken = data0.nextPageToken;
foreach (var data in orderByPublishAt)
{
var clientTag = new RestClient("googleapis.com/youtube/v3/");
var tagRequest = new RestRequest("videos", Method.GET);
tagRequest.AddParameter("key", "AG8");
tagRequest.AddParameter("part", "snippet,statistics");
tagRequest.AddParameter("id", data.id.videoId);
var tagResponse = clientTag.Execute(tagRequest);
foreach (var item in tagResponse.Data.items)
{
htmlTable.Append("" + Sr + " ");
htmlTable.Append(" ");
htmlTable.Append("" + item.snippet.title + " ");
htmlTable.Append("" + item.snippet.description + " ");
htmlTable.Append("");
Sr = Sr + 1;
}
}
}
htmlTable.Append(" ");
htmlTable.Append("
");
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", "UVw");
request.AddParameter("key", "AG8");
if (!string.IsNullOrEmpty(nextPageToken))
{
request.AddParameter("pageToken", nextPageToken);
}
var response = client.Execute(request);
return response.Data;
}
Thanks
Tuhin PaulPosted Mar 28, 2025, 5:11 PM
After applying the fixes, the output should:
Sr.,Video Id,Video Title,Description).Tuhin PaulPosted Mar 28, 2025, 5:09 PM
Part 3:
Explanation of Changes
Duplicate Filtering :
HashSetnameduniqueVideoIdsis used to store unique video IDs. Before processing a video, its ID is checked against theHashSet. If the ID already exists, the video is skipped.Pagination Logic :
do-whileloop ensures that all pages are processed untilnextPageTokenbecomesnull.nextPageTokenis updated after each iteration, and the loop fetches the next page of videos.HTML Table Structure :
Video Idcolumn to display the unique video ID.Tuhin PaulPosted Mar 28, 2025, 5:09 PM
Part 2:
Below is the corrected version of your code with the necessary fixes:
Tuhin PaulPosted Mar 28, 2025, 5:08 PM
Part 1:
Root Causes and Solutions
1. Repeating Videos
The YouTube API's
search.listendpoint may return duplicate video entries across different pages if the query parameters are not properly constrained. To address this:HashSetto track unique video IDs and ensure no duplicates are added to the table.data0.itemscollection does not already contain duplicates before processing.2. Incorrect Video Count
The pagination logic might not be iterating through all pages correctly. This could happen if:
nextPageTokenis not updated properly.nextPageToken.To fix this:
whileloop continues untilnextPageTokenbecomesnull.