Created
September 26, 2012 14:16
-
-
Save komiya-atsushi/3788310 to your computer and use it in GitHub Desktop.
Twitter4J 2.2.6 を使って Twitter v1.1 API の search/tweets を叩くサンプルコード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package twitter4j.internal.http; | |
public class HttpResponseHelper { | |
public static HttpClientConfiguration getHttpClientConfiguration( | |
HttpResponse res) { | |
return res.CONF; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package twitter4j.internal.json; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.Reader; | |
import java.util.List; | |
import java.util.Map; | |
import twitter4j.ResponseList; | |
import twitter4j.Status; | |
import twitter4j.TwitterException; | |
import twitter4j.conf.Configuration; | |
import twitter4j.internal.http.HttpResponse; | |
import twitter4j.internal.http.HttpResponseHelper; | |
import twitter4j.internal.org.json.JSONArray; | |
import twitter4j.internal.org.json.JSONException; | |
import twitter4j.internal.org.json.JSONObject; | |
public class StatusJSONHelper { | |
public static ResponseList<Status> create(HttpResponse res, | |
Configuration conf) throws TwitterException { | |
try { | |
JSONObject rootJson = res.asJSONObject(); | |
JSONArray list = rootJson.getJSONArray("statuses"); | |
int size = list.length(); | |
ResponseList<Status> result = new ResponseListImpl<Status>(size, | |
new HttpResponseDecorator(res)); | |
for (int i = 0; i < size; i++) { | |
JSONObject json = list.getJSONObject(i); | |
Status status = new StatusJSONImpl(json); | |
if (conf.isJSONStoreEnabled()) { | |
DataObjectFactoryUtil.registerJSONObject(status, json); | |
} | |
result.add(status); | |
} | |
if (conf.isJSONStoreEnabled()) { | |
DataObjectFactoryUtil.registerJSONObject(result, list); | |
} | |
return result; | |
} catch (JSONException e) { | |
throw new TwitterException(e); | |
} | |
} | |
private static class HttpResponseDecorator extends HttpResponse { | |
private HttpResponse original; | |
public HttpResponseDecorator(HttpResponse original) { | |
super(HttpResponseHelper.getHttpClientConfiguration(original)); | |
this.original = original; | |
} | |
@Override | |
public String getResponseHeader(String name) { | |
if (name.toLowerCase().startsWith("x-ratelimit")) { | |
String newHeaderName = "X-Rate-" + name.substring(6); | |
return original.getResponseHeader(newHeaderName); | |
} | |
return original.getResponseHeader(name); | |
} | |
@Override | |
public Map<String, List<String>> getResponseHeaderFields() { | |
return original.getResponseHeaderFields(); | |
} | |
@Override | |
public void disconnect() throws IOException { | |
original.disconnect(); | |
} | |
@Override | |
public JSONArray asJSONArray() throws TwitterException { | |
return original.asJSONArray(); | |
} | |
@Override | |
public JSONObject asJSONObject() throws TwitterException { | |
return original.asJSONObject(); | |
} | |
@Override | |
public Reader asReader() { | |
return original.asReader(); | |
} | |
@Override | |
public InputStream asStream() { | |
return original.asStream(); | |
} | |
@Override | |
public String asString() throws TwitterException { | |
return original.asString(); | |
} | |
@Override | |
public int getStatusCode() { | |
return original.getStatusCode(); | |
} | |
@Override | |
public String toString() { | |
return original.toString(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package twitter4j; | |
import twitter4j.internal.http.HttpParameter; | |
import twitter4j.internal.http.HttpResponse; | |
import twitter4j.internal.json.StatusJSONHelper; | |
public class TwitterV1_1 extends TwitterBaseImpl { | |
private static final long serialVersionUID = -5860538294316425972L; | |
private static final String V1_1_BASE_URL = "https://api.twitter.com/1.1/"; | |
private TwitterImpl twitter; | |
public TwitterV1_1(Twitter _twitter) { | |
this((TwitterImpl) _twitter); | |
} | |
private TwitterV1_1(TwitterImpl twitter) { | |
super(twitter.conf, twitter.auth); | |
this.twitter = twitter; | |
} | |
private HttpResponse get(String url, HttpParameter[] parameters) | |
throws TwitterException { | |
if (!twitter.conf.isMBeanEnabled()) { | |
return twitter.http.get(url, parameters, twitter.auth); | |
} else { | |
// intercept HTTP call for monitoring purposes | |
HttpResponse response = null; | |
long start = System.currentTimeMillis(); | |
try { | |
response = twitter.http.get(url, parameters, twitter.auth); | |
} finally { | |
long elapsedTime = System.currentTimeMillis() - start; | |
TwitterAPIMonitor.getInstance().methodCalled(url, elapsedTime, | |
isOk(response)); | |
} | |
return response; | |
} | |
} | |
private boolean isOk(HttpResponse response) { | |
return response != null && response.getStatusCode() < 300; | |
} | |
/** | |
* v1.1 API の search/tweets を呼び出します。 | |
* | |
* @param query | |
* @return | |
* @throws TwitterException | |
*/ | |
public ResponseList<Status> searchTweets(String query) | |
throws TwitterException { | |
ensureAuthorizationEnabled(); | |
HttpResponse res = get( | |
String.format("%s/search/tweets.json", V1_1_BASE_URL), | |
new HttpParameter[] { new HttpParameter("q", query) }); | |
return StatusJSONHelper.create(res, twitter.conf); | |
} | |
public static void main(String[] args) throws TwitterException { | |
TwitterV1_1 twitter = new TwitterV1_1(TwitterFactory.getSingleton()); | |
ResponseList<Status> searchResult = twitter.searchTweets("komiya_atsushi"); | |
System.out.println(searchResult.toString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment