27 Temmuz 2015 Pazartesi

c# youtube video bilgileri alma

YouTube and Vimeo provide a data API that you can use to get information for their videos. YouTube requires the use of an API key and you can get one for free. At the bottom of the article you will see the required steps to obtain a YouTube API key.

Vimeo on the other hand provides a simple API that does not require an API key. For authenticated read/write requests Vimeo provides the advanced API, which requires an API key. In this article we will see an example with the simple API.
In both cases we perform a web request and get back an JSON file with the video’s data. We will use the using System.Web.Helpers; extension to be able to read the data without writing a class to represent the JSON object.
Note: The post has been updated to reflect the recent changes of the YouTube API
using System.Net;
using System.Web.Helpers;
using System.Web.Script.Serialization;
 
class Video
{
    public const string ytKey = "";
 
    public int Width { get; set; }
    public int Height { get; set; }
    public int Duration { get; set; }
    public string Title { get; set; }
    public string ThumbUrl { get; set; }
    public string BigThumbUrl { get; set; }
    public string Description { get; set; }
    public string VideoDuration { get; set; }
    public string Url { get; set; }
    public DateTime UploadDate { get; set; }
 
    public Video()
    {
    }
 
    public bool YouTubeImport(string VideoID)
    {
        try
        {
            WebClient myDownloader = new WebClient();
            myDownloader.Encoding = System.Text.Encoding.UTF8;
 
            string jsonResponse = myDownloader.DownloadString("https://www.googleapis.com/youtube/v3/videos?id=" + VideoID + "&key=" + ytKey + "&part=snippet");
            JavaScriptSerializer jss = new JavaScriptSerializer();
            var dynamicObject = Json.Decode(jsonResponse);
            var item = dynamicObject.items[0].snippet;
 
            Title = item.title;
            ThumbUrl = item.thumbnails.@default.url;
            BigThumbUrl = item.thumbnails.high.url;
            Description = item.description;
            UploadDate = Convert.ToDateTime(item.publishedAt);
 
            jsonResponse = myDownloader.DownloadString("https://www.googleapis.com/youtube/v3/videos?id=" + VideoID + "&key=" + ytKey + "&part=contentDetails");
            dynamicObject = Json.Decode(jsonResponse);
            string tmp = dynamicObject.items[0].contentDetails.duration;
            Duration = Convert.ToInt32(System.Xml.XmlConvert.ToTimeSpan(tmp).TotalSeconds);
 
            Url = "http://www.youtube.com/watch?v=" + VideoID;
 
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
 
    public bool VimeoImport(string VideoID)
    {
        try
        {
            WebClient myDownloader = new WebClient();
            myDownloader.Encoding = System.Text.Encoding.UTF8;
 
            string jsonResponse = myDownloader.DownloadString("http://vimeo.com/api/v2/video/" + VideoID + ".json");
            JavaScriptSerializer jss = new JavaScriptSerializer();
            var dynamicObject = Json.Decode(jsonResponse);
            var item = dynamicObject[0];
 
            Title = item.title;
            Description = item.description;
            Url = item.url;
            ThumbUrl = item.thumbnail_small;
            BigThumbUrl = item.thumbnail_large;
            UploadDate = Convert.ToDateTime(item.upload_date);
            Width = Convert.ToInt32(item.width);
            Height = Convert.ToInt32(item.height);
            Duration = Convert.ToInt32(item.duration);
 
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
}
Note: The Vimeo API can give its data in XML, JSON and PHP format. This can be done easily by setting the URL’s suffix to ‘.xml’ or ‘.json’ or ‘.php’.

How to get a YouTube API key

  1. First, you need to login tohttps://console.developers.google.com/.
  2. Go to APIs and auth and then APIs. You need to search and enable the YouTube Data API as well as theFreebase API.
  3. Set its status to ON and accept it’s Terms of Use.
  4. Go to APIs and auth and then Credentials and create a new key in the Public API access area.
Note that this API key is not only for YouTube, but for many Google services such as Google Maps API, Custom Search API, etc.

Links