summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Wu <peter@lekensteyn.nl>2014-04-24 01:58:20 +0200
committerPeter Wu <peter@lekensteyn.nl>2014-04-24 01:58:20 +0200
commit7315b2e9340dc8d3912e062b903710fdb7f82ec1 (patch)
treed0ed6951b0724334c1cf1da097ff2e821112dd19
parentc79e6a27e9e0617171e9d4a78364ed71f9153c80 (diff)
downloadTwitterDataAnalytics-7315b2e9340dc8d3912e062b903710fdb7f82ec1.tar.gz
TwitterAPI: helper for accessing the TwitterAPI
-rw-r--r--src/mining/TwitterApi.java93
1 files changed, 93 insertions, 0 deletions
diff --git a/src/mining/TwitterApi.java b/src/mining/TwitterApi.java
new file mode 100644
index 0000000..39db3ad
--- /dev/null
+++ b/src/mining/TwitterApi.java
@@ -0,0 +1,93 @@
+package mining;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.logging.Logger;
+import org.apache.commons.codec.Charsets;
+import org.apache.http.NameValuePair;
+import org.apache.http.client.utils.URLEncodedUtils;
+import org.apache.http.message.BasicNameValuePair;
+import org.json.JSONObject;
+import support.ConsumerKeySecret;
+import utils.Configuration;
+
+/**
+ * Accessors to the Twitter API.
+ */
+public class TwitterApi {
+
+ private static final String CFG_BEARER_TOKEN = "bearer-token";
+ private final Requester requester;
+
+ public TwitterApi(Requester requester) {
+ this.requester = requester;
+ }
+
+ /**
+ * Establishes an instance using application-only authentication using a
+ * file as cache.
+ *
+ * @return An API context usable for requests using app-only auth.
+ * @throws IOException
+ */
+ public static TwitterApi getAppOnly() throws IOException {
+ Configuration cfg = Configuration.getConfig();
+ String token;
+ BearerRequester breq = null;
+ token = cfg.getProperty(CFG_BEARER_TOKEN);
+ if (token != null) {
+ breq = new BearerRequester(token);
+ if (!breq.isValid()) {
+ Logger.getLogger(TwitterApi.class.getName())
+ .info("Bearer token invalid, trying a new one...");
+ breq = null;
+ }
+ }
+ if (breq == null) {
+ ConsumerKeySecret cks = getConsumerKeySecret();
+ breq = new BearerRequester(cks);
+ cfg.setProperty(CFG_BEARER_TOKEN, breq.getAccessToken());
+ }
+ return new TwitterApi(breq);
+ }
+
+ private static ConsumerKeySecret getConsumerKeySecret() {
+ // static consumer keys retrieved from dev.twitter.com
+ return new ConsumerKeySecret(Configuration.CONSUMER_KEY,
+ Configuration.CONSUMER_SECRET);
+ }
+
+ /**
+ * @param resource The resource to be requested.
+ * @return A builder for an API request.
+ */
+ public Builder build(String resource) {
+ return new Builder(resource);
+ }
+
+ public class Builder {
+
+ private final String resource;
+ private final List<NameValuePair> params;
+
+ public Builder(String resource) {
+ this.resource = resource;
+ this.params = new ArrayList<>();
+ }
+
+ public Builder param(String key, String val) {
+ params.add(new BasicNameValuePair(key, val));
+ return this;
+ }
+
+ @Override
+ public String toString() {
+ return URLEncodedUtils.format(params, Charsets.UTF_8);
+ }
+
+ public JSONObject request() throws IOException {
+ return TwitterApi.this.requester.getJSON(toString());
+ }
+ }
+}