Hi ramco1917,
For inserting and downloading video, you need to write code inside the foreach loop.
Download the libvideo library from Nuget using the following command.
Install-Package VideoLibrary
Then inherit the following namespaces.
using System.Configuration;
using System.Data.SqlClient;
using System.IO;
using VideoLibrary;
Code
protected void MyOwnVideos()
{
StringBuilder htmlTable = new StringBuilder();
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", "UC_nNoaVw"); // Replace with your actual channelId
request.AddParameter("key", "AIz8..."); // Replace with your actual API key
IRestResponse<YoutubeSearchListResponse> response = client.Execute<YoutubeSearchListResponse>(request);
// Check if the response contains data
if (response.Data == null || response.Data.items == null || response.Data.items.Count == 0)
{
Console.WriteLine("No Data Found. Please check your channelId and API key.");
return;
}
// Build the HTML table
htmlTable.Append("<table class='table table-bordered table-hover datatable-highlight' id='tbldata'>");
htmlTable.Append("<thead><tr><th>Video Id</th><th>Video Title</th><th>Description</th><th>Published</th></tr></thead>");
htmlTable.Append("<tbody>");
foreach (var data in response.Data.items.OrderBy(x => x.snippet.publishedAt))
{
htmlTable.Append("<tr>");
htmlTable.Append($"<td>{data.id}</td>");
htmlTable.Append($"<td>{data.snippet.title}</td>");
htmlTable.Append($"<td>{data.snippet.description}</td>");
htmlTable.Append($"<td>{data.snippet.publishedAt}</td>");
// Insert into database.
htmlTable.Append("</tr>");
this.InsertVideoDetails(data.id, data.name, data.title, data.description, data.publishedAt);
// Download the Video.
this.DownloadVideo(data.id);
}
htmlTable.Append("</tbody>");
htmlTable.Append("</table>");
// Output the HTML table
Console.WriteLine(htmlTable.ToString());
}
private void InsertVideoDetails(string id, string name, string title, string description, string createdDate)
{
string sql = "INSERT INTO TableName (Id, Name, Title, Description, CreatedDate) VALUES (@Id, @Name, @Title, @Description, @CreatedDate)";
string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand(sql))
{
cmd.Parameters.AddWithValue("@Id", id);
cmd.Parameters.AddWithValue("@Name", name);
cmd.Parameters.AddWithValue("@Title", title);
cmd.Parameters.AddWithValue("@Description", description);
cmd.Parameters.AddWithValue("@CreatedDate", createdDate);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
private void DownloadVideo(string videoId)
{
string vedioUrl = string.Format("https://www.youtube.com/embed/{0}.mp4", videoId);
var youTube = YouTube.Default;
var video = youTube.GetVideo(vedioUrl);
File.WriteAllBytes(Server.MapPath(string.Format("~/Videos/{0}.mp4"), video.FullName), video.GetBytes());
}
public class YoutubeSearchListResponse
{
public string kind { get; set; }
public string etag { get; set; }
public string nextPageToken { get; set; }
public string regionCode { get; set; }
public PageInfo pageInfo { get; set; }
public List<Item> items { get; set; }
}
public class Snippet
{
public DateTime publishedAt { get; set; }
public string channelId { get; set; }
public string title { get; set; }
public string description { get; set; }
public Thumbnail thumbnails { get; set; }
public string channelTitle { get; set; }
public string liveBroadcastContent { get; set; }
public DateTime publishTime { get; set; }
}
public class Id
{
public string kind { get; set; }
public string videoId { get; set; }
}
public class Item
{
public string kind { get; set; }
public string etag { get; set; }
public Id id { get; set; }
public Snippet snippet { get; set; }
}