From 5e5bfb04ec9ebc88206039e8adba51589adf5b66 Mon Sep 17 00:00:00 2001 From: DjBushido Date: Mon, 7 Apr 2014 14:12:45 -0400 Subject: [PATCH] Add code for interfacing with the Twitter API --- .../edu/uncc/itcs4180/hw5/TwitterClient.java | 13 -- .../edu/uncc/itcs4180/hw5/twitter/Tweet.java | 93 +++++++++++ .../uncc/itcs4180/hw5/twitter/TweetList.java | 12 ++ .../hw5/twitter/TwitterAuthentication.java | 6 + .../itcs4180/hw5/twitter/TwitterClient.java | 145 ++++++++++++++++++ .../itcs4180/hw5/twitter/TwitterUser.java | 42 +++++ 6 files changed, 298 insertions(+), 13 deletions(-) delete mode 100644 HW5/src/edu/uncc/itcs4180/hw5/TwitterClient.java create mode 100644 HW5/src/edu/uncc/itcs4180/hw5/twitter/Tweet.java create mode 100644 HW5/src/edu/uncc/itcs4180/hw5/twitter/TweetList.java create mode 100644 HW5/src/edu/uncc/itcs4180/hw5/twitter/TwitterAuthentication.java create mode 100644 HW5/src/edu/uncc/itcs4180/hw5/twitter/TwitterClient.java create mode 100644 HW5/src/edu/uncc/itcs4180/hw5/twitter/TwitterUser.java diff --git a/HW5/src/edu/uncc/itcs4180/hw5/TwitterClient.java b/HW5/src/edu/uncc/itcs4180/hw5/TwitterClient.java deleted file mode 100644 index 1519c6e..0000000 --- a/HW5/src/edu/uncc/itcs4180/hw5/TwitterClient.java +++ /dev/null @@ -1,13 +0,0 @@ -package edu.uncc.itcs4180.hw5; - -import java.net.URL; - -public class TwitterClient { - - private final String API_KEY = "HuEbvsXOxQsKVmTneyilVtsV6"; - private final String API_SECRET = "ZIJqmKEGoQUKLCIit15SKz6XIl4PP1xgDm1jVCIBSDhmImzFqk"; - - private final String URL_TOKEN = "https://api.twitter.com/oauth2/token"; - private final String URL_API = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name="; - -} diff --git a/HW5/src/edu/uncc/itcs4180/hw5/twitter/Tweet.java b/HW5/src/edu/uncc/itcs4180/hw5/twitter/Tweet.java new file mode 100644 index 0000000..b7f66e3 --- /dev/null +++ b/HW5/src/edu/uncc/itcs4180/hw5/twitter/Tweet.java @@ -0,0 +1,93 @@ +package edu.uncc.itcs4180.hw5.twitter; + +// Code adapted from: +// https://github.com/Rockncoder/TwitterTutorial/blob/master/src/com/example/TwitterTutorial/Tweet.java +// +// We changed the field names to conform to Java standards + +import com.google.gson.annotations.SerializedName; + +public class Tweet { + + @SerializedName("created_at") + private String dateCreated; + + @SerializedName("id") + private String id; + + @SerializedName("text") + private String text; + + @SerializedName("in_reply_to_status_id") + private String inReplyToStatusId; + + @SerializedName("in_reply_to_user_id") + private String inReplyToUserId; + + @SerializedName("in_reply_to_screen_name") + private String inReplyToScreenName; + + @SerializedName("user") + private TwitterUser user; + + public String getDateCreated() { + return dateCreated; + } + + public String getId() { + return id; + } + + public String getInReplyToScreenName() { + return inReplyToScreenName; + } + + public String getInReplyToStatusId() { + return inReplyToStatusId; + } + + public String getInReplyToUserId() { + return inReplyToUserId; + } + + public String getText() { + return text; + } + + public void setDateCreated(String dateCreated) { + this.dateCreated = dateCreated; + } + + public void setId(String id) { + this.id = id; + } + + public void setInReplyToScreenName(String inReplyToScreenName) { + this.inReplyToScreenName = inReplyToScreenName; + } + + public void setInReplyToStatusId(String inReplyToStatusId) { + this.inReplyToStatusId = inReplyToStatusId; + } + + public void setInReplyToUserId(String inReplyToUserId) { + this.inReplyToUserId = inReplyToUserId; + } + + public void setText(String text) { + this.text = text; + } + + public void setUser(TwitterUser user) { + this.user = user; + } + + public TwitterUser getUser() { + return user; + } + + @Override + public String toString() { + return getText(); + } +} \ No newline at end of file diff --git a/HW5/src/edu/uncc/itcs4180/hw5/twitter/TweetList.java b/HW5/src/edu/uncc/itcs4180/hw5/twitter/TweetList.java new file mode 100644 index 0000000..8ecd101 --- /dev/null +++ b/HW5/src/edu/uncc/itcs4180/hw5/twitter/TweetList.java @@ -0,0 +1,12 @@ +package edu.uncc.itcs4180.hw5.twitter; + +import java.util.ArrayList; + +public class TweetList extends ArrayList { + + /** + * + */ + private static final long serialVersionUID = 1L; + +} diff --git a/HW5/src/edu/uncc/itcs4180/hw5/twitter/TwitterAuthentication.java b/HW5/src/edu/uncc/itcs4180/hw5/twitter/TwitterAuthentication.java new file mode 100644 index 0000000..29aa4fa --- /dev/null +++ b/HW5/src/edu/uncc/itcs4180/hw5/twitter/TwitterAuthentication.java @@ -0,0 +1,6 @@ +package edu.uncc.itcs4180.hw5.twitter; + +public class TwitterAuthentication { + String tokenType; + String accessToken; +} diff --git a/HW5/src/edu/uncc/itcs4180/hw5/twitter/TwitterClient.java b/HW5/src/edu/uncc/itcs4180/hw5/twitter/TwitterClient.java new file mode 100644 index 0000000..ee03451 --- /dev/null +++ b/HW5/src/edu/uncc/itcs4180/hw5/twitter/TwitterClient.java @@ -0,0 +1,145 @@ +package edu.uncc.itcs4180.hw5.twitter; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.UnsupportedEncodingException; +import java.net.URL; +import java.net.URLEncoder; +import java.text.BreakIterator; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ExecutionException; + +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpRequestBase; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.params.BasicHttpParams; + +import com.google.gson.Gson; + +import android.os.AsyncTask; +import android.util.Base64; + +public class TwitterClient { + + private final String API_KEY = "HuEbvsXOxQsKVmTneyilVtsV6"; + private final String API_SECRET = "ZIJqmKEGoQUKLCIit15SKz6XIl4PP1xgDm1jVCIBSDhmImzFqk"; + + private final String URL_TOKEN = "https://api.twitter.com/oauth2/token"; + private final String URL_API = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name="; + + // Code mimicked in large part from: + // https://github.com/Rockncoder/TwitterTutorial/blob/master/src/com/example/TwitterTutorial/MainActivity.java + + public static TweetList getTweetList(String handle) { + TwitterClient tc = new TwitterClient(); + try { + return tc.new TweetListDownloader().execute(handle).get(); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (ExecutionException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return null; + } + + private class TweetListDownloader extends AsyncTask { + + @Override + protected TweetList doInBackground(String... params) { + if (params.length > 0) + return getTweetList(params[0]); + else + return null; + } + + private String getResponse(HttpRequestBase request) { + // Does all our HTTP work for us + StringBuilder sb = new StringBuilder(); + + try { + DefaultHttpClient client = new DefaultHttpClient(new BasicHttpParams()); + HttpResponse response; + response = client.execute(request); + int statusCode = response.getStatusLine().getStatusCode(); + String reason = response.getStatusLine().getReasonPhrase(); + + if (statusCode == 200) { + + HttpEntity entity = response.getEntity(); + InputStream input = entity.getContent(); + BufferedReader br = new BufferedReader(new InputStreamReader(input)); + String line = null; + while ((line = br.readLine()) != null) + sb.append(line); + + } else + sb.append(reason); + + } catch (ClientProtocolException e) { + } catch (IOException e) { + } + + + return sb.toString(); + } + + private TwitterAuthentication responseToAuth(String response) { + TwitterAuthentication auth = null; + if (response != null && response.length() > 0) { + Gson gson = new Gson(); + auth = gson.fromJson(response, TwitterAuthentication.class); + } + + return auth; + } + + private TweetList responseToTweets(String response) { + TweetList tweets = null; + if (response != null && response.length() > 0) { + Gson gson = new Gson(); + // We have to use TweetList because ArrayList.class doesn't exist + tweets = gson.fromJson(response, TweetList.class); + } + return tweets; + } + + private TweetList getTweetList(String handle) { + try { + // Set up our keys + String urlApiKey = URLEncoder.encode(API_KEY, "UTF-8"); + String urlApiSecret = URLEncoder.encode(API_SECRET, "UTF-8"); + String combined = urlApiKey + ":" + urlApiSecret; + String b64Encoded = Base64.encodeToString(combined.getBytes(), Base64.NO_WRAP); + + // Get our token + HttpPost post = new HttpPost(URL_TOKEN); + post.setHeader("Authorization", "Basic " + b64Encoded); + post.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"); + post.setEntity(new StringEntity("grant_type=client_credentials")); + TwitterAuthentication auth = responseToAuth(getResponse(post)); + + // And now get our actual tweets list + HttpGet get = new HttpGet(URL_API + handle); + get.setHeader("Authorization", "Bearer " + auth.accessToken); + get.setHeader("Content-Type", "application/json"); + return responseToTweets(getResponse(get)); + } catch (UnsupportedEncodingException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return null; + } + + } +} diff --git a/HW5/src/edu/uncc/itcs4180/hw5/twitter/TwitterUser.java b/HW5/src/edu/uncc/itcs4180/hw5/twitter/TwitterUser.java new file mode 100644 index 0000000..bbae277 --- /dev/null +++ b/HW5/src/edu/uncc/itcs4180/hw5/twitter/TwitterUser.java @@ -0,0 +1,42 @@ +package edu.uncc.itcs4180.hw5.twitter; + +// Code copied from: +// https://github.com/Rockncoder/TwitterTutorial/blob/master/src/com/example/TwitterTutorial/TwitterUser.java + +import com.google.gson.annotations.SerializedName; + +public class TwitterUser { + + @SerializedName("screen_name") + private String screenName; + + @SerializedName("name") + private String name; + + @SerializedName("profile_image_url") + private String profileImageUrl; + + public String getProfileImageUrl() { + return profileImageUrl; + } + + public String getScreenName() { + return screenName; + } + + public void setProfileImageUrl(String profileImageUrl) { + this.profileImageUrl = profileImageUrl; + } + + public void setScreenName(String screenName) { + this.screenName = screenName; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} \ No newline at end of file