//DoWork with arguments is called from the app.xaml //DoWork with no arguments is called from NotifyIconViewModel.cs //Overloaded for ease of use and lazy coding public static void DoWork(string[] args) { const int pollingInterval = 5 * 60 * 1000; bool continuePolling = true; TwitchApi myApi = new TwitchApi(); Utilities myUtils = new Utilities(); if (args.Length == 0) { //There were no arguments the only way we have to alert the user is via a log myUtils.WriteFile("No User name specified please refer to READ.MD", "error.log"); } else { //Let's assume that the first argument is the channel name //We really need the _Id for the channel as this is required per v5 TwitchChannelJson myChannel = myApi.TwitchChannelsQueryApi(args[0].ToLower()); //We should have a channel or list of channels if (myChannel.channels.Count == 1) { //We only got one channel back from the API call, extract the _id change the global twitchId = (myChannel.channels.ElementAt(0)._id).ToString(); } else { foreach (TwitchChannel channel in myChannel.channels) { //We need an exact match for the channel name we've been supplied if (channel.name.ToLower() == args[0].ToLower()) { twitchId = (channel._id).ToString(); break; } } } } //Let's set up our polling interval do { //Make a call to overloaded DoWork to avoid code duplication DoWork(); System.Threading.Thread.Sleep(pollingInterval); } while (continuePolling); return; }
//Wrapper to make the relevant Twitch API calls public static void CallTwitchApi(string ApiName) { TwitchApi myApi = new TwitchApi(); Utilities myUtils = new Utilities(); //Using a switch to pick out the relevant API calls switch (ApiName) { case "channels": TwitchChannel myChannel = myApi.TwitchChannelsApi(twitchId); //We have a List if (myChannel != null) { //We only got one response in our list //Console.WriteLine("Current game is {0}", myChannel.channels.ElementAt(0).game); myUtils.DebugLog(string.Format("Current game is {0}", myChannel.game)); myUtils.WriteFile(myChannel.game, "Game.txt"); myUtils.WriteFile(myChannel.followers.ToString(), "NumberofFollowers.txt"); myUtils.WriteFile(myChannel.status, "Status.txt"); } break; case "channelsquery": TwitchChannelJson myTwitchChannelJson = myApi.TwitchChannelsQueryApi(twitchId); break; case "channelcommunity": TwitchCommunity myTwitchCommunity = myApi.TwitchChannelCommunityApi(twitchId); myUtils.WriteFile(myTwitchCommunity.name, "Community.txt"); break; case "channelfollows": TwitchFollowsJson myTwitchFollowsJson = myApi.TwitchChannelFollows(twitchId); string myFollowers = null; foreach (TwitchFollow follow in myTwitchFollowsJson.follows) { myFollowers += follow.user.display_name + ", "; } myUtils.WriteFile(myFollowers, "Followers.txt"); break; default: break; } return; }
public TwitchChannelJson TwitchChannelsQueryApi(string user) { //TODO remove the query parameter and separate out into a new call //because the query and the normal return different types //Build an HttpRequest for the relevant Twitch endpoint HttpWebRequest myWebRequest = WebRequest.CreateHttp("https://api.twitch.tv/kraken/search/channels?query=" + user); //Add Headers and Accept to force v5 myWebRequest = AddHeaders(myWebRequest); //Create the Json object outside the using so we can access it later TwitchChannelJson myChannel = null; //Make the http request and handle the response using (System.IO.Stream s = myWebRequest.GetResponse().GetResponseStream()) { using (System.IO.StreamReader sr = new System.IO.StreamReader(s)) { var jsonResponse = sr.ReadToEnd(); //Console.WriteLine(String.Format("Response: {0}", jsonResponse)); myTwitchUtils.DebugLog(String.Format("Response: {0}", jsonResponse)); //Deserialize the response using Newtonsoft and manage exceptions try { myChannel = JsonConvert.DeserializeObject <TwitchChannelJson>(jsonResponse); } catch (Exception ex) { //Console.WriteLine("Failed deserialzing with {0}", ex.Message); myTwitchUtils.DebugLog(string.Format("Failed deserialzing with {0}", ex.Message)); } } } return(myChannel); }