public async Task <ActionResult> Twitch() { var EmpInfo = new TwitchResponse(); using (var client = new HttpClient()) { //Passing service base url client.BaseAddress = new Uri(Baseurl); client.DefaultRequestHeaders.Clear(); //Define request data format client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.twitchtv.v5+json")); client.DefaultRequestHeaders.Add("Client-ID", "dyw2v1kn7zglk676f4pldux07apn4b"); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("OAuth", "ly3mc8y9bsjnsu0avaj5odzfkdjb46"); //Sending request to find web api REST service resource GetAllEmployees using HttpClient HttpResponseMessage Res = await client.GetAsync("https://api.twitch.tv/helix/streams?user_login=gamesdonequick" + "&user_login=legendarymihawk" + "&user_login=superakouma"); //Checking the response is successful or not which is sent using HttpClient if (Res.IsSuccessStatusCode) { //Storing the response details recieved from web api var EmpResponse = Res.Content.ReadAsStringAsync().Result; //Deserializing the response recieved from web api and storing into the Employee list EmpInfo = JsonConvert.DeserializeObject <TwitchResponse>(EmpResponse); } //returning the employee list to view return(View(EmpInfo.data)); } }
public async void RunManualCommercial(int length) { int activationDelay; // Get given delay (0 up to and including 60) from textbox. // On error, default to 20 and show warning of the actual start. try { activationDelay = Convert.ToInt32(tbManComDelay.Text); if (activationDelay < 0 || activationDelay > 60) { throw new ArgumentOutOfRangeException(); } } catch (Exception) { activationDelay = 20; MessageBox.Show("Invalid delay given of 0 up to and including 60.\nA commercial will be requested in 20 seconds after pressing OK.", "OakBot Manual Commercial", MessageBoxButton.OK, MessageBoxImage.Warning); } // Create a new task with a sleep of the given delay // Await the task to retrieve exceptions try { Task activateCommercial = new Task(() => { Thread.Sleep(activationDelay * 1000); TwitchResponse r = Utils.GetClient().TriggerCommercial(length); if (r.Status == 422) { MessageBox.Show("Failed to start the commercial:\n" + r.Message, "OakBot Manual Commercial", MessageBoxButton.OK, MessageBoxImage.Error); } else { MessageBox.Show(string.Format("The {0} second commercial has started.", length), "OakBot Manual Commercial", MessageBoxButton.OK, MessageBoxImage.Asterisk); } }); activateCommercial.Start(); await activateCommercial; } catch (Exception ex) { MessageBox.Show("Something went wrong starting the commercial:\n" + ex.ToString(), "OakBot Manual Commercial", MessageBoxButton.OK, MessageBoxImage.Error); } }
public IActionResult TwitchLive()//[FromBody]TwitterTwitchViewModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var mails = new Dictionary <string, List <string> >(); var userIds = ( from souscription in _db.Souscriptions where souscription.AppletId == 5 select souscription.UserId ).ToList(); foreach (var id in userIds) { var list = new List <string>(); var userName = ( from users in _db.Users where users.Id == id select users.UserName ).FirstOrDefault(); var streamer = ( from souscription in _db.Souscriptions where souscription.UserId == id && souscription.AppletId == 5 select souscription.Streamer ).FirstOrDefault(); var token = ( from souscription in _db.Souscriptions where souscription.UserId == id && souscription.AppletId == 5 select souscription.TwitterToken ).FirstOrDefault(); var secret = ( from souscription in _db.Souscriptions where souscription.UserId == id && souscription.AppletId == 5 select souscription.TwitterSecret ).FirstOrDefault(); list.Add(streamer); list.Add(token); list.Add(secret); mails.Add(userName, list); } foreach (var pair in mails) { /*Calling API http://twitch/api */ string apiKey = "wi4v1bglz74mehi3fo02mlx2cdu70y"; HttpWebRequest apiRequest = WebRequest.Create("https://api.twitch.tv/kraken/streams/" + pair.Value[0] + "?client_id=" + apiKey) as HttpWebRequest; string apiResponse = ""; using (HttpWebResponse response = apiRequest.GetResponse() as HttpWebResponse) { StreamReader reader = new StreamReader(response.GetResponseStream()); apiResponse = reader.ReadToEnd(); } /*End*/ /*http://json2csharp.com*/ TwitchResponse rootObject = JsonConvert.DeserializeObject <TwitchResponse>(apiResponse); if (rootObject.stream != null) { Auth.SetUserCredentials("GchL7ZiVPOxn4S1ahVqr72tP3", "m4X3R69C5nHdSSKdAyg2TetLpTvypYwPrEYAmMd8pL8ShUYwBr", pair.Value[1], pair.Value[2]); var user = Tweetinvi.User.GetAuthenticatedUser(); var tweet = Tweet.PublishTweet("The streamer " + rootObject.stream.channel.display_name + " is on live on the game " + rootObject.stream.game + " for " + rootObject.stream.viewers + " viewers\n" + "clic on this link to follow " + rootObject.stream.channel.display_name + ": " + rootObject.stream.channel.url); var timelineTweets = Timeline.GetUserTimeline(user, 5); foreach (var timelineTweet in timelineTweets) { Console.WriteLine(timelineTweet); } Console.ReadKey(); return(Ok(new { message = "Tweet sent successfully", status = 200 })); } else { return(BadRequest(new { message = pair.Value[0] + " is not on live", status = 400 })); } } return(Ok(new { message = "Tweet sent successfully", status = 200 })); }
public IActionResult Live()//[FromBody] TwitchViewModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var mails = new Dictionary <string, string>(); var userIds = ( from souscription in _db.Souscriptions where souscription.AppletId == 3 select souscription.UserId ).ToList(); foreach (var id in userIds) { var userName = ( from user in _db.Users where user.Id == id select user.UserName ).FirstOrDefault(); var streamer = ( from souscription in _db.Souscriptions where souscription.UserId == id && souscription.AppletId == 3 select souscription.Streamer ).FirstOrDefault(); mails.Add(userName, streamer); } foreach (var pair in mails) { /*Calling API http://twitch/api */ string apiKey = "wi4v1bglz74mehi3fo02mlx2cdu70y"; HttpWebRequest apiRequest = WebRequest.Create("https://api.twitch.tv/kraken/streams/" + pair.Value + "?client_id=" + apiKey) as HttpWebRequest; string apiResponse = ""; using (HttpWebResponse response = apiRequest.GetResponse() as HttpWebResponse) { StreamReader reader = new StreamReader(response.GetResponseStream()); apiResponse = reader.ReadToEnd(); } /*End*/ /*http://json2csharp.com*/ TwitchResponse rootObject = JsonConvert.DeserializeObject <TwitchResponse>(apiResponse); try { if (rootObject.stream != null) { SmtpClient client = new SmtpClient("smtp.gmail.com"); client.EnableSsl = true; client.UseDefaultCredentials = false; client.Credentials = new NetworkCredential("*****@*****.**", "Starkiller75"); MailMessage mailMessage = new MailMessage(); mailMessage.From = new MailAddress("*****@*****.**"); mailMessage.To.Add(pair.Key); mailMessage.Body = ( $"<h1 style=\"text-align: center;\">{rootObject.stream.channel.display_name} est en live sur {rootObject.stream.game}:</h1>" + $"<h2 style=\"text-align: center;\">{rootObject.stream.channel.display_name} stream pour {rootObject.stream.viewers} viewers:</h2><br>" + $"<h3 style=\"text-align: center;\">{rootObject.stream.channel.status}</h3><br>" + $"<img style=\"text-align: center;\" src=\"{rootObject.stream.channel.logo}\" /><br>" + $"<img style=\"text-align: center;\" src=\"{rootObject.stream.preview.medium}\" /><br>" + $"<a href=\"{rootObject.stream.channel.url}\" style=\"text-align: center;\">Suivez {rootObject.stream.channel.display_name} en cliquant sur le lien suivant</a>" ); mailMessage.IsBodyHtml = true; mailMessage.Subject = $"{rootObject.stream.channel.display_name} est en Live!"; client.Send(mailMessage); } //else //{ //return BadRequest(new { message = model.streamer + " is not in live", status = 400 }); //} } catch (SmtpFailedRecipientException ex) { return(BadRequest(ex)); } } return(Ok(new { message = "Mail sent successfully", status = 200 })); }
public IActionResult TwitchLive()//[FromBody]TwitterTwitchViewModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var mails = new Dictionary <string, List <string> >(); var userIds = ( from souscription in _db.Souscriptions where souscription.AppletId == 7 select souscription.UserId ).ToList(); foreach (var id in userIds) { var list = new List <string>(); var userName = ( from users in _db.Users where users.Id == id select users.UserName ).FirstOrDefault(); var streamer = ( from souscription in _db.Souscriptions where souscription.UserId == id && souscription.AppletId == 7 select souscription.Streamer ).FirstOrDefault(); var token = ( from souscription in _db.Souscriptions where souscription.UserId == id && souscription.AppletId == 7 select souscription.FbToken ).FirstOrDefault(); list.Add(streamer); list.Add(token); mails.Add(userName, list); } foreach (var pair in mails) { /*Calling API http://twitch/api */ string apiKey = "wi4v1bglz74mehi3fo02mlx2cdu70y"; HttpWebRequest apiRequest = WebRequest.Create("https://api.twitch.tv/kraken/streams/" + pair.Value[0] + "?client_id=" + apiKey) as HttpWebRequest; string apiResponse = ""; using (HttpWebResponse response = apiRequest.GetResponse() as HttpWebResponse) { StreamReader reader = new StreamReader(response.GetResponseStream()); apiResponse = reader.ReadToEnd(); } /*End*/ /*http://json2csharp.com*/ TwitchResponse rootObject = JsonConvert.DeserializeObject <TwitchResponse>(apiResponse); if (rootObject.stream != null) { var access_token = pair.Value[1]; var client = new FacebookClient(access_token); client.Post("/me/feed", new { message = "The streamer " + rootObject.stream.channel.display_name + " is on live on the game " + rootObject.stream.game + " for " + rootObject.stream.viewers + " viewers\n" + "clic on this link to follow " + rootObject.stream.channel.display_name + ": " + rootObject.stream.channel.url }); return(Ok(new { message = "Facebook post posted successfully", status = 200 })); } else { return(BadRequest(new { message = pair.Value[1] + " is not on live", status = 400 })); } } return(BadRequest(new { message = "Facebook post not sent", status = 400 })); }