取得 Youtube 影片觀看次數。
參考資料:YouTube APIs and Tools – Reference Guide: Data API Protocol
[code language=”csharp”]
/// <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";
}
}
[/code]
使用方法範例:
[code language=”csharp”]
Response.Write(GetYoutubeViewcount("RCsCw9Vz7Nc"));
[/code]
順便提一下取得縮圖的方式:
http://www.reelseo.com/youtube-thumbnail-image/
可用下面網址取得:
http://img.youtube.com/vi/VIDEO_ID/#.jpg
# 可以為 0, 1, 2, 3
說明:
# | 尺寸 | 第幾張縮圖 |
---|---|---|
0 | 480×360 | 1 |
1 | 120×90 | 1 |
2 | 120×90 | 2 |
3 | 120×90 | 3 |