Search twitter - Java Social Media

Java examples for Social Media:Twitter

Description

Search twitter

Demo Code


import twitter4j.conf.*;
import twitter4j.*;
import twitter4j.auth.*;
import twitter4j.api.*;
import java.util.*;
import java.util.regex.*;
import java.io.*;

public class TwitterGet {

  Twitter twitter;// w  w  w. ja v  a  2  s.c  om
  String searchString = "peanut allergy";
  List<Status> tweets;
  int totalTweets;

  void setup() {

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setJSONStoreEnabled(true);
    cb.setOAuthConsumerKey("your key");
    cb.setOAuthConsumerSecret("your key");
    cb.setOAuthAccessToken("your key");
    cb.setOAuthAccessTokenSecret("your key");

    TwitterFactory tf = new TwitterFactory(cb.build());

    twitter = tf.getInstance();
    getNewTweets();
  }

  void getNewTweets() {
    try {
      Query query = new Query(searchString);

      query.setCount(500); // by default its 15 set count
      QueryResult result = twitter.search(query);
      tweets = result.getTweets(); // the return of this function is list

      PrintWriter writer = new PrintWriter("allergy/1.json", "UTF-8"); // name of the file

      PrintWriter writerObj = new PrintWriter("allergy_raw/1.json", "UTF-8"); // name of the file

      for (Status tweet : tweets) {
        // System.out.println(tweet.getFromUser() + ":" + tweet.getText());
        String json = TwitterObjectFactory.getRawJSON(tweet);
        // System.out.println(json);
        writerObj.println(json);

        JSONObject obj = new JSONObject(json);

        JSONObject newObj = new JSONObject();


        String tweetText = obj.getString("text");


        Pattern MY_PATTERN = Pattern.compile("#(\\w+|\\W+)");

        Pattern URL_PATTERN = Pattern.compile(".*http://.*");

        Matcher mat = MY_PATTERN.matcher(tweetText);
        List<String> str = new ArrayList<String>();
        while (mat.find()) {
          str.add(mat.group(1));
        }

        Matcher matchURL = URL_PATTERN.matcher(tweetText);
        List<String> str_url = new ArrayList<String>();

        while (matchURL.find()) {
          str_url.add(matchURL.group(0));
        }

        newObj.put("id", obj.getString("id"));
        newObj.put("text", obj.getString("text"));
        newObj.put("lang", obj.getString("lang"));
        newObj.put("created_at", obj.getString("created_at"));

        newObj.put("twitter_hashtags", str);
        newObj.put("twitter_urls", str_url);
        str.clear();
        str_url.clear();
        writer.println(newObj);
      }
      writer.close();
      writerObj.close();

    } catch (Exception e) {
      System.out.println("Exception " + e);
    }

  }

  void printTweetsToFile() {
    try {
      PrintWriter writer = new PrintWriter("raw/text123.json", "UTF-8");
      writer.println(tweets);
      // writer.println("The second line"+tweets.toString());
      writer.close();
    } catch (Exception e) {
      System.out.println("Exception e");
    }
  }

  public static void main(String[] args) {
    // System.out.println("TwitterGet.main");
    TwitterGet TwitterGetObj = new TwitterGet();
    TwitterGetObj.setup();
  }

}

Related Tutorials