Advertisement
H4ckHunt3r

TwitchAPIChannelUpdate Utility Class

May 10th, 2015
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.02 KB | None | 0 0
  1. package me.confu5ed.twitchapi;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.net.URL;
  6. import java.net.URLEncoder;
  7. import javax.net.ssl.HttpsURLConnection;
  8.  
  9.  
  10. // Lib: http://commons.apache.org/proper/commons-io/
  11. import org.apache.commons.io.*;
  12. // Lib: http://mvnrepository.com/artifact/org.json/json/20141113
  13. import org.json.*;
  14.  
  15.  
  16. /**
  17.  * Utility Class for Updating Twitch Channel via TwitchAPI.
  18.  * @version 1.0
  19.  * @author Marcel Naeve <[email protected]>
  20.  */
  21. public final class TwitchChannelUpdate {
  22.    
  23.     /**
  24.      * APIKey (OAuth2.0 Token) for TwitchAPI: channel_edit scale needed,
  25.      * you can get one for this Application by: <code>serious-pro.de/twitchgametracker/token.html</code>.
  26.      */
  27.     public static String APIKey = "";
  28.    
  29.     /**
  30.      * Request URL - TwitchAPI should be constant until the API get updated.
  31.      */
  32.     private static String stringUrl = "https://api.twitch.tv/kraken/channels/";
  33.    
  34.     /**
  35.      * Twitch Channel Name.
  36.      */
  37.     public static String channel = "";
  38.    
  39.     /**
  40.      * Update Channel Information by TwitchAPI.
  41.      * @param key what will be updated? (game/status/delay)
  42.      * @param data newValue
  43.      * @return boolean update success=>true/failed=>false
  44.      */
  45.     public static boolean update(String key, String data) {
  46.        
  47.         URL url;
  48.         String response = "";
  49.         try {      
  50.             // Prepare
  51.             url = new URL(stringUrl + channel);
  52.             String basicAuth = "OAuth " + APIKey;
  53.             String sendData = "channel[" + URLEncoder.encode(key, "UTF-8") + "]=" + URLEncoder.encode(data, "UTF-8");
  54.            
  55.             // Create Request
  56.             HttpsURLConnection apiConnection  = (HttpsURLConnection) url.openConnection();
  57.             apiConnection.setRequestMethod("PUT");
  58.             apiConnection.setDoOutput(true);
  59.            
  60.            
  61.             apiConnection.setRequestProperty("Accept", "application/vnd.twitchtv.v3+json");
  62.             apiConnection.setRequestProperty("X-Requested-With", "TwitchGameTracker");
  63.             apiConnection.setRequestProperty("Authorization", basicAuth);
  64.        
  65.             // Connect to API and send Data
  66.             apiConnection.connect();
  67.             apiConnection.getOutputStream().write(sendData.getBytes("UTF-8"));
  68.            
  69.            
  70.             // Read Response Data
  71.             InputStream inputStream = null;
  72.             System.out.println("ResponseCode: " + apiConnection.getResponseCode());
  73.             if(apiConnection.getResponseCode() == 200)
  74.             {
  75.                 inputStream = apiConnection.getInputStream();
  76.             } else {
  77.                 inputStream = apiConnection.getErrorStream();
  78.             }
  79.             if(inputStream != null) response = IOUtils.toString(inputStream);
  80.            
  81.             // Disconnect from API
  82.             apiConnection.disconnect();
  83.            
  84.         } catch (IOException e) {
  85.             // TODO API Request Failed: Permissions??
  86.             e.printStackTrace();
  87.             return false;
  88.         }
  89.  
  90.         if(response.startsWith("{")) {
  91.             JSONObject json = new JSONObject(response);        
  92.             // TODO check data has been changed and give optical feedback to gui
  93.             System.out.println(json.toString());
  94.         }
  95.         //------------------
  96.        
  97.         return true;
  98.     }
  99.    
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement