2014年3月30日 星期日

C# Youtube 影片資訊 JSON (回傳影片瀏覽次數)



/// <summary>
/// 取得 Youtube 影片觀看次數
/// </summary>
/// <param name="youtubecode">Youtube 影片碼</param>
/// <returns></returns>
public static string GetYoutubeViewcount(string youtubecode)
{
    string url = string.Format("http://gdata.youtube.com/feeds/api/videos/{0}?alt=json", youtubecode);
    System.Net.HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
    try
    {
        using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
        {
            StreamReader reader = new StreamReader(response.GetResponseStream());
            string retVal = reader.ReadToEnd();
            JObject googleSearch = JObject.Parse(retVal);
            return googleSearch["entry"]["yt$statistics"]["viewCount"].ToString();
        }
    }
    catch
    {
        return "0";
    }
}

參考網站:http://patw.idv.tw/blog/archives/521/asp-net-c-access-to-youtube-videos-watched/

標籤:

0 個意見:

張貼留言

訂閱 張貼留言 [Atom]

<< 首頁