Hi
In below code few videos are repeating again
protected void MyOwnVideos()
{
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 Published ");
htmlTable.Append("");
nextPageToken = data0.nextPageToken;
while (!string.IsNullOrEmpty(nextPageToken))
{
data0 = GetVideosList(nextPageToken);
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("" + data.snippet.title + " ");
htmlTable.Append("" + item.snippet.title + " ");
htmlTable.Append("" + item.snippet.description + " ");
htmlTable.Append("" + data.snippet.publishedAt.ToString("dd-MM-yyyy") + " ");
htmlTable.Append("");
Sr = Sr + 1;
InsertVideoDetails(data.id.videoId, item.snippet.title, item.snippet.description, item.statistics.likeCount, item.statistics.viewCount, item.statistics.commentCount, data.snippet.publishedAt);
}
}
}
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", "NaVw");
request.AddParameter("key", "AG8");
if (!string.IsNullOrEmpty(nextPageToken))
{
request.AddParameter("pageToken", nextPageToken);
}
var response = client.Execute(request);
return response.Data;
}
Thanks
Daniel WrightPosted Apr 27, 2025, 5:38 AM
It seems that in the provided code snippet, there is a possibility of videos being repeated due to how the iteration and data handling is implemented. Let's break it down:
1. The code starts by fetching a list of videos using the `GetVideosList` method. The data fetched is then ordered by the `publishedAt` property.
2. Inside the `while` loop that checks for the presence of a `nextPageToken`, the same list of data is fetched again using the same `GetVideosList` method. This implies that the data is being retrieved multiple times unnecessarily.
3. Within the iteration of `orderByPublishAt`, for each video item, an API request is made to get additional details about the video using `RestClient` and `RestRequest`. This could potentially lead to the same video details being fetched more than once, resulting in repeating videos in the output.
4. The HTML table generation and insertion logic seems to be correct, but the handling of video details and insertion into the table should be reviewed to ensure that each video is processed only once.
To address the issue of videos repeating, you may consider optimizing the code by fetching and processing each video's details only once. One possible approach could be to fetch all the necessary video details initially and then iterate through them to populate the table without repeating requests unnecessarily.
By optimizing the data retrieval and processing workflow, you can ensure that each video is displayed only once in the table, eliminating the issue of videos repeating.