//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 TwitchFollowsJson TwitchChannelFollows(string id) { HttpWebRequest myWebRequest = null; myWebRequest = WebRequest.CreateHttp("https://api.twitch.tv/kraken/channels/" + id + "/follows?limit=100"); //Add Headers and Accept to force v5 myWebRequest = AddHeaders(myWebRequest); //Create the Json object outside the using so we can access it later TwitchFollowsJson myFollows = 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 = new TwitchChannelJson(); //myChannel.channels = new List<TwitchChannel>(); //myChannel.channels.Add(JsonConvert.DeserializeObject<TwitchChannel>(jsonResponse)); myFollows = JsonConvert.DeserializeObject <TwitchFollowsJson>(jsonResponse); } catch (Exception ex) { //Console.WriteLine("Failed deserialzing with {0}", ex.Message); myTwitchUtils.DebugLog(string.Format("Failed deserialzing with {0}", ex.Message)); } } } return(myFollows); }