This post is in continuation to the YouTube API with .NET series of articles. In the last post we learn about pulling region or country specific video feed. In this post we will learn to read single video metadata from YouTube.
Each of the video on YouTube has their own statistics such as Video Title, Video descriptions, uploaded by, number of views, comments etc. We will try to fetch these statistics from YouTubeServices.
For demonstration purpose, we have designed following interface in .NET. We have a textbox where you can write or paste the video URL as shown in the web browser. The “Show Statistics” button has code logic to connect with YouTubeServices and request for video metadata that has been entered in the text box. The list view control will show the metadata that YouTubeService has returned in the response.
In the form code we have added Google Data API references and used our Developer Key that we created in the Google API Console. The entire code snippet is following:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Google.GData.YouTube; using Google.YouTube; using Google.GData.Client; using Google.GData.Extensions.MediaRss; namespace YouTubeAPI { public partial class Form1 : Form { string myDeveloperKey; public Form1() { myDeveloperKey = "my Long string Developer key"; InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { listView1.Columns.Add("Video Title"); listView1.Columns.Add("Uploaded By"); listView1.Columns.Add("Video Desc."); listView1.FullRowSelect = true; string VideoWebLink; int startPos; int endPos; VideoWebLink = textBox1.Text; startPos = VideoWebLink.LastIndexOf("="); endPos = VideoWebLink.Length - startPos; VideoWebLink = VideoWebLink.Substring(startPos+1, endPos-1); YouTubeService myservice = new YouTubeService("aa",myDeveloperKey); Uri videoEntryUrl = new Uri("http://gdata.youtube.com/feeds/api/videos/" + VideoWebLink); FeedQuery fq=new FeedQuery(); fq.Uri=videoEntryUrl; Feed<Video> videoFeed = new Feed<Video>(myservice,fq); foreach (Video entry in videoFeed.Entries) { listView1.Items.Add((new ListViewItem(new string[] { entry.Title.ToString(), entry.ViewCount.ToString(), entry.Description.ToString() }))); } } } }
After design and code setup, I connected with the internet and clicked on one of the video. I copied the URL as shown in the web browser and pasted it into form text box. Next, I clicked on the button “Show Statistics” and application code logic connected with the YouTubeService and pulled the vide statistics such as Video Title, Video Descriptions, Number of views etc.