Skip to content

Instantly share code, notes, and snippets.

@leonardopinho
Last active August 22, 2018 14:53
Show Gist options
  • Save leonardopinho/c3f8de6bf5809b13452a115db4add227 to your computer and use it in GitHub Desktop.
Save leonardopinho/c3f8de6bf5809b13452a115db4add227 to your computer and use it in GitHub Desktop.
Useful class for Youtube integration
using System.Net;
using Newtonsoft.Json;
namespace Utils
{
public class GoogleUtils
{
private string _apiKey = "";
private string _shortUrl = "https://www.googleapis.com/urlshortener/v1/url?fields=id%2ClongUrl%2Cstatus&key=";
private string _urlYoutubeLastVideos = "https://www.googleapis.com/youtube/v3/search?key=";
public GoogleUtils(string apiKey)
{
_apiKey = apiKey;
}
/// <summary>
/// ConvertToShortUrl
/// </summary>
/// <param name="longUrl"></param>
/// <returns>short url</returns>
public string ConvertToShortUrl(string longUrl)
{
string result = "";
using (var client = new WebClient())
{
string json = "{ \"longUrl\": \"" + longUrl + "\" }";
client.Headers[HttpRequestHeader.ContentType] = "application/json; charset=utf-8";
result = JsonConvert.DeserializeObject<dynamic>(client.UploadString(_shortUrl + _apiKey, "POST", json)).id.ToString();
}
return result;
}
/// <summary>
/// GetYoutubeList
/// </summary>
/// <param name="channelId"></param>
/// <param name="count"></param>
/// <returns>list to video based on the count</returns>
public object GetYoutubeList(string channelId, int count)
{
object result = null;
using (var client = new WebClient())
{
client.Headers[HttpRequestHeader.ContentType] = "application/json; charset=utf-8";
result = client.DownloadString(_urlYoutubeLastVideos + _apiKey + "&channelId=" + channelId + "&part=snippet,id&order=date&maxResults=" + count);
result = JsonConvert.DeserializeObject<object>(result.ToString());
}
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment