Hi
Few Videos are repeating again & there are 428 Short Videos it is displaying 403
C#
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 IdVideo TitleDescription");
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 PaulRank #7 · 71.9K reputationPosted Mar 28, 2025, 1:08 PM EDT
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.Tuhin PaulRank #7 · 71.9K reputationPosted Mar 28, 2025, 1:09 PM EDT
Part 2:
Below is the corrected version of your code with the necessary fixes:
protected void MyOwnVideos11() { StringBuilder htmlTable = new StringBuilder(); string nextPageToken = string.Empty; Int32 Sr = 1; // HashSet to track unique video IDs HashSet uniqueVideoIds = new HashSet(); // Fetch the first page of videos var data0 = GetVideosList(nextPageToken); var orderByPublishAt = data0.items.OrderBy(x => x.snippet.publishedAt); htmlTable.Append(""); htmlTable.Append("Sr.Video IdVideo TitleDescription"); htmlTable.Append(""); do { foreach (var data in orderByPublishAt) { // Skip duplicate video IDs if (!uniqueVideoIds.Add(data.id.videoId)) continue; var clientTag = new RestClient("https://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(""); htmlTable.Append("" + Sr + ""); htmlTable.Append("" + item.id + ""); htmlTable.Append("" + item.snippet.title + ""); htmlTable.Append("" + item.snippet.description + ""); htmlTable.Append(""); Sr++; } } // Update nextPageToken and fetch the next page nextPageToken = data0.nextPageToken; if (!string.IsNullOrEmpty(nextPageToken)) { data0 = GetVideosList(nextPageToken); orderByPublishAt = data0.items.OrderBy(x => x.snippet.publishedAt); } } while (!string.IsNullOrEmpty(nextPageToken)); htmlTable.Append(""); htmlTable.Append(""); PlaceHolderTable.Controls.Add(new Literal { Text = htmlTable.ToString() }); }Tuhin PaulRank #7 · 71.9K reputationPosted Mar 28, 2025, 1:09 PM EDT
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 PaulRank #7 · 71.9K reputationPosted Mar 28, 2025, 1:11 PM EDT
After applying the fixes, the output should:
Sr.,Video Id,Video Title,Description).Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts