public ActionResult Index() { var apiKey = System.Web.Configuration.WebConfigurationManager.AppSettings["youtubeAPIKey"]; var query = "pokemon"; // Create the request to the API WebRequest request = WebRequest.Create($"https://www.googleapis.com/youtube/v3/search?part=snippet&q={query}&key={apiKey}"); // Send the request. WebResponse response = request.GetResponse(); // Get back the response stream. Stream stream = response.GetResponseStream(); // Make the stream reacheable/accessible. StreamReader reader = new StreamReader(stream); // Human readable response. string responseString = reader.ReadToEnd(); // Parses the response string. JObject parsedString = JObject.Parse(responseString); YoutubeResultModel youtubeResult = parsedString.ToObject <YoutubeResultModel>(); return(View(youtubeResult)); }
public async Task <YoutubeResultModel> Link(string token, [FromBody] YoutubeModel data) { int lang = data.Lang; if (lang < 3 || lang > 4) { lang = 2; } RacLib.RacMsg msgs = RacLib.RacMsg.cache.GetMessage((RacLib.RacMsg.Language)lang); YoutubeResultModel yrm = new YoutubeResultModel(); try { string machine = HttpContext.Features.Get <IHttpConnectionFeature>()?.RemoteIpAddress.ToString(); string userId = SessionCode.ValidateSessionCode(token, machine); if (userId != null) { Profile prf = Profile.LoadProfile(userId); if (prf != null) { string videoCode = ""; if (data.Link.Contains("youtu.be/")) { int idx = data.Link.IndexOf("youtu.be/"); int s = idx + 9; for (int i = s; i < data.Link.Length; i++) { if (data.Link[i] == '/' || data.Link[i] == '&' || data.Link[i] == '?') { break; } videoCode += data.Link[i]; } } else { int idx = data.Link.IndexOf("?"); string pars = data.Link.Substring(idx + 1); string[] p = pars.Split('&'); for (int i = 0; i < p.Length; i++) { if (p[i].StartsWith("v=")) { videoCode = p[i].Substring(2); break; } } } RacLib.BaseLog.log.Log(RacLib.BaseLog.LogType.Informative, "videoCode = '" + videoCode + "'"); // Links do youtube e do bitchute yrm.YoutubeLink = data.Link; yrm.BitchuteLink = ""; // Pega a figura string figlink = "https://i.ytimg.com/vi/" + videoCode + "/maxresdefault.jpg"; HttpClient client_fig = new HttpClient(); HttpResponseMessage response_fig = await client_fig.GetAsync(figlink); byte[] imageBytes = await response_fig.Content.ReadAsByteArrayAsync(); RacLib.BaseLog.log.Log(RacLib.BaseLog.LogType.Informative, "Got bits"); MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length); ms.Write(imageBytes, 0, imageBytes.Length); System.Drawing.Image img = System.Drawing.Image.FromStream(ms, true); int cw = img.Width; int ch = img.Height; int dw = 1280; int dh = 720; float ew = (float)cw / (float)dw; float eh = (float)ch / (float)dh; int px = 0, py = 0; int lw = 0, lh = 0; if (ew == eh) // Nenhum crop necessário, só resize { px = 0; py = 0; lw = cw; lh = ch; } else if (ew > eh) // Tem que cropar na largura { int totw = (int)(cw / eh); px = (totw - dw) / 2; py = 0; lw = (int)(dw * eh); lh = ch; } else if (ew < eh)// Tem que cropar na altura { int toth = (int)(ch / ew); px = 0; py = (toth - dh) / 2; lw = cw; lh = (int)(dh * ew); } ImageAttributes ia = new ImageAttributes(); ia.SetWrapMode(WrapMode.TileFlipXY); using (System.Drawing.Bitmap _bitmap = new System.Drawing.Bitmap(dw, dh, PixelFormat.Format32bppPArgb)) { _bitmap.SetResolution(img.HorizontalResolution, img.VerticalResolution); using (Graphics _graphic = Graphics.FromImage(_bitmap)) { _graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; _graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; _graphic.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; _graphic.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; _graphic.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver; _graphic.Clear(Color.White); _graphic.DrawImage(img, new Rectangle(0, 0, dw, dh), new Rectangle(px, py, lw, lh), GraphicsUnit.Pixel); System.IO.MemoryStream stream = new System.IO.MemoryStream(); _bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg); yrm.Image = Convert.ToBase64String(stream.ToArray()); } } RacLib.BaseLog.log.Log(RacLib.BaseLog.LogType.Informative, "Got image"); string apiCall = "https://www.googleapis.com/youtube/v3/videos?part=snippet&id=" + videoCode + "&key=AIzaSyDZyxrWNvN0MLWexTptTPDHzhnXHwebnCU"; HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"); HttpResponseMessage response = await client.GetAsync(apiCall); string pageContents = await response.Content.ReadAsStringAsync(); RacLib.BaseLog.log.Log(RacLib.BaseLog.LogType.Informative, "Got stuff"); dynamic stuff = JsonConvert.DeserializeObject(pageContents); if (stuff.items.Count > 0) { yrm.Title = stuff.items[0].snippet.title; yrm.Description = stuff.items[0].snippet.description; yrm.Tags = ""; try { for (int i = 0; i < stuff.items[0].snippet.tags.Count; i++) { if (yrm.Tags != "") { yrm.Tags += ", "; } yrm.Tags += stuff.items[0].snippet.tags[i]; } } catch { } RacLib.BaseLog.log.Log(RacLib.BaseLog.LogType.Informative, "Got all"); } else { yrm.Result = (int)RacMsg.Id.InternalError; yrm.ResultComplement = "Não encontrei o vídeo no youtube"; } } } } catch (Exception ex) { RacLib.BaseLog.log.Log(RacLib.BaseLog.LogType.Informative, "Exception"); RacLib.BaseLog.log.Log(ex); yrm.Result = (int)RacMsg.Id.InternalError; yrm.ResultComplement = ex.Message; } return(yrm); }