SHARE
TWEET
Untitled
a guest
May 12th, 2016
52
Never
- package twitterdownloader;
- import java.io.FileNotFoundException;
- import java.io.PrintWriter;
- import java.io.UnsupportedEncodingException;
- import java.util.HashSet;
- import java.util.Map;
- import java.util.Set;
- import twitter4j.Query;
- import twitter4j.QueryResult;
- import twitter4j.RateLimitStatus;
- import twitter4j.Status;
- import twitter4j.Twitter;
- import twitter4j.TwitterException;
- import twitter4j.TwitterFactory;
- import twitter4j.auth.AccessToken;
- public class TwitterManag {
- private final String consumerKey = "XXXX"; //working credentials
- private final String consumerKeySecret = "XXXXX"; //working credentials
- private final String accessToken = "XXXX"; //working credentials
- private final String accessTokenSecret = "XXX"; //working credentials
- private final Twitter twitter; //Twitter4J Instance
- private final Set<Long> idSet; //set od tweet's ids in order to filter result ro receive only unique tweets
- private long maxID = -1; //holding maxId to filter tweets
- private int alreadyDownloaded = 0; //amount of already downloaded unique tweets
- private boolean isBlocked = false; //is API Rate Limit hit?
- private int waitTimeLeftSeconds = 0; //how much to wait till API is available again
- public TwitterManag() {
- twitter = TwitterFactory.getSingleton(); //create Twitter4J Instance
- twitter.setOAuthConsumer(consumerKey, consumerKeySecret);
- AccessToken accessTokenAPI = new AccessToken(accessToken, accessTokenSecret);
- twitter.setOAuthAccessToken(accessTokenAPI);
- idSet = new HashSet();
- }
- public void downloadTweets(String query, int ammount, String filePathToSave) throws FileNotFoundException, UnsupportedEncodingException, InterruptedException {
- PrintWriter writer = new PrintWriter(filePathToSave, "UTF-8"); //writting tweets to file
- Query twitterQuery = new Query(query);
- twitterQuery.setCount(100);
- twitterQuery.setLang("en");
- downloadData(writer, twitterQuery);
- while (alreadyDownloaded != ammount) {
- if (!isBlocked) { //if we are not blocked by API
- twitterQuery.setMaxId(maxID); //set maxID
- if (ammount - alreadyDownloaded < 100) { //set count to appopiate ammount
- twitterQuery.setCount(ammount - alreadyDownloaded);
- } else {
- twitterQuery.setCount(100);
- }
- downloadData(writer, twitterQuery);
- } else { //if we are blocked
- System.out.println("BLOCKED, WAITING FOR " + waitTimeLeftSeconds / 60 + " minutess.");
- if (waitTimeLeftSeconds <= 0)
- waitTimeLeftSeconds = 1;
- Thread.sleep(waitTimeLeftSeconds * 1000);
- isBlocked = false;
- }
- }
- writer.close();
- }
- private void downloadData(PrintWriter writer, Query twitterQuery) {
- try {
- QueryResult result = twitter.search(twitterQuery);
- for (Status singleTweet : result.getTweets()) {
- if (!idSet.contains(singleTweet.getId()) && !singleTweet.isRetweet()) { //if tweet is not yet in our small database and it is not retweet
- if (maxID == -1 || singleTweet.getId() < maxID) //get new maxId
- maxID = singleTweet.getId() - 1;
- idSet.add(singleTweet.getId()); //add tweet ID to "database"
- writer.println(singleTweet.getText());
- alreadyDownloaded++;
- }
- }
- System.out.println("Already downloaded: " + alreadyDownloaded);
- } catch (TwitterException ex) {
- if (ex.exceededRateLimitation()) {
- try {
- Map<String, RateLimitStatus> rateLimitStatus = twitter.getRateLimitStatus("search");
- RateLimitStatus searchTweetsRateLimit = rateLimitStatus.get("/search/tweets");
- isBlocked = true;
- waitTimeLeftSeconds = searchTweetsRateLimit.getSecondsUntilReset();
- } catch (TwitterException ex1) {
- waitTimeLeftSeconds = 15 * 60;
- }
- }
- }
- }
- }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

