mirror of
				https://github.com/bspeice/itcs4180
				synced 2025-11-04 02:10:32 -05:00 
			
		
		
		
	Add code for interfacing with the Twitter API
This commit is contained in:
		@ -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=";
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
							
								
								
									
										93
									
								
								HW5/src/edu/uncc/itcs4180/hw5/twitter/Tweet.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										93
									
								
								HW5/src/edu/uncc/itcs4180/hw5/twitter/Tweet.java
									
									
									
									
									
										Normal file
									
								
							@ -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();
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										12
									
								
								HW5/src/edu/uncc/itcs4180/hw5/twitter/TweetList.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								HW5/src/edu/uncc/itcs4180/hw5/twitter/TweetList.java
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,12 @@
 | 
				
			|||||||
 | 
					package edu.uncc.itcs4180.hw5.twitter;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.util.ArrayList;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					public class TweetList extends ArrayList<Tweet> {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * 
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						private static final long serialVersionUID = 1L;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -0,0 +1,6 @@
 | 
				
			|||||||
 | 
					package edu.uncc.itcs4180.hw5.twitter;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					public class TwitterAuthentication {
 | 
				
			||||||
 | 
						String tokenType;
 | 
				
			||||||
 | 
						String accessToken;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										145
									
								
								HW5/src/edu/uncc/itcs4180/hw5/twitter/TwitterClient.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										145
									
								
								HW5/src/edu/uncc/itcs4180/hw5/twitter/TwitterClient.java
									
									
									
									
									
										Normal file
									
								
							@ -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<String, Void, TweetList> {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							@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<Tweet>.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;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										42
									
								
								HW5/src/edu/uncc/itcs4180/hw5/twitter/TwitterUser.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								HW5/src/edu/uncc/itcs4180/hw5/twitter/TwitterUser.java
									
									
									
									
									
										Normal file
									
								
							@ -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;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user