-
-
Save LSTANCZYK/567b40521a65150ab153803e9e4efcc9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Web.Mvc; | |
using System.Web.Mvc.Ajax; | |
using RestSharp; | |
using Newtonsoft.Json; | |
using RestSharp.Deserializers; | |
namespace Helpers | |
{ | |
public class DynamicJsonDeserializer : IDeserializer | |
{ | |
public string RootElement { get; set; } | |
public string Namespace { get; set; } | |
public string DateFormat { get; set; } | |
#region IDeserializer implementation | |
public T Deserialize<T> (IRestResponse response) | |
{ | |
return JsonConvert.DeserializeObject<dynamic>(response.Content); | |
} | |
#endregion | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Criar uma classe que herde de restclient e criar métodos para facilitar o uso dos verbos http e encapsular a criação do request e a definição do custom serializer |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Web.Mvc; | |
using System.Web.Mvc.Ajax; | |
using RestSharp; | |
using Newtonsoft.Json; | |
using RestSharp.Deserializers; | |
using System.Text.RegularExpressions; | |
using VideoApi; | |
using Helpers; | |
namespace Controllers | |
{ | |
public class HomeController : Controller | |
{ | |
public ActionResult Index (string q) | |
{ | |
if (string.IsNullOrWhiteSpace (q)) | |
return View (); | |
var information = Regex.IsMatch(q, @"youtube\.com")? GetYoutubeDetail(q) : GetVimeoDetail(q); | |
var model = new VideoViewModel { | |
Title = information.title, | |
Duration = information.duration, | |
Thumbnail = information.thumbnail | |
}; | |
return View (model); | |
} | |
private dynamic GetVimeoDetail(string url) | |
{ | |
var code = Regex.Match (url, @"vimeo.com/\(\d+)$"); | |
if (!code.Success) | |
throw new ArgumentException("invalid url"); | |
var client = new RestClient("http://vimeo.com/api/v2/video/{videoid}.output"); | |
client.AddHandler("application/json", new DynamicJsonDeserializer()); | |
var request = new RestRequest("videos/{videoid}", Method.GET); | |
request.AddUrlSegment("videoid", code.Groups[1].Value); | |
var model = client.Execute<dynamic>(request).Data; | |
var title = model["title"]; | |
var thumbnail = model["thumbnail_medium"]; | |
var duration = model["duration"]; | |
return new { | |
title, thumbnail, duration | |
}; | |
} | |
private dynamic GetYoutubeDetail(string url) | |
{ | |
var code = Regex.Match (url, @"youtube.com/\watch\?v=(\S+)$"); | |
if (!code.Success) | |
throw new ArgumentException("invalid url"); | |
var client = new RestClient("https://gdata.youtube.com/feeds/api"); | |
client.AddHandler("application/json", new DynamicJsonDeserializer()); | |
var request = new RestRequest("videos/{videoid}?v=2&alt=json", Method.GET); | |
request.AddUrlSegment("videoid", code.Groups[1].Value); | |
var model = client.Execute<dynamic>(request).Data; | |
var title = model["entry"]["media$group"]["media$title"]["$t"]; | |
var thumbnail = model["entry"]["media$group"]["media$thumbnail"][1]["url"]; | |
var duration = model["entry"]["media$group"]["yt$duration"]["seconds"]; | |
return new { | |
title, thumbnail, duration | |
}; | |
} | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Models | |
{ | |
public class VideoViewModel | |
{ | |
public string Title { | |
get; | |
set; | |
} | |
public string Duration { | |
get; | |
set; | |
} | |
public string Thumbnail { | |
get; | |
set; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment