List of usage examples for twitter4j TwitterStreamFactory TwitterStreamFactory
public TwitterStreamFactory(String configTreePath)
From source file:edu.csupomona.nlp.tool.crawler.Twitter.java
/** * Construct Twitter for crawling with Stream API * @throws IOException//from w w w. j a va2s. c om */ public Twitter() throws IOException { // init default parameters lang_ = "en"; includeRetweet_ = false; // init default restriction sizeLimit_ = 3000; hourLimit_ = 24; // 24 hours // read and construct property Properties key = new Properties(); key.load(getClass().getResourceAsStream("/etc/twitter.properties")); // set authentication key&token ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey(key.getProperty("ConsumerKey")) .setOAuthConsumerSecret(key.getProperty("ConsumerSecret")) .setOAuthAccessToken(key.getProperty("AccessToken")) .setOAuthAccessTokenSecret(key.getProperty("AccessTokenSecret")); // create twitter stream ts_ = new TwitterStreamFactory(cb.build()).getInstance(); // add listener ts_.addListener(new StatusListener() { @Override public void onStatus(Status status) { // only record tweet matches requirement if (isRetweetMatch(status) && !isIdRedundant(status)) { String text = status.getText(); Long id = status.getId(); text = text.replaceAll("\\n", ""); // to the same line tweet_.add(id.toString() + ":" + text); idSet_.add(id); } System.out.println( "[" + idSet_.size() + "/" + sizeLimit_ + "]" + status.getId() + ": " + status.getText()); // when limit is reached if (isLimitReached()) { try { // write tweet to file finishup(); } catch (IOException ex) { Logger.getLogger(Twitter.class.getName()).log(Level.SEVERE, null, ex); } } } @Override public void onDeletionNotice(StatusDeletionNotice sdn) { throw new UnsupportedOperationException("Not supported yet."); } @Override public void onTrackLimitationNotice(int i) { throw new UnsupportedOperationException("Not supported yet."); } @Override public void onScrubGeo(long l, long l1) { throw new UnsupportedOperationException("Not supported yet."); } @Override public void onStallWarning(StallWarning sw) { throw new UnsupportedOperationException("Not supported yet."); } @Override public void onException(Exception excptn) { excptn.printStackTrace(); // anything wrong, just save everything we have and stop try { finishup(); } catch (IOException ex) { Logger.getLogger(Twitter.class.getName()).log(Level.SEVERE, null, ex); } } }); }
From source file:edu.lander.twitter.TwitStreaming.java
public void prepareDownloadTweets(int newType, long limit) { setType(newType);/*w w w .j a v a 2 s . com*/ if (getType() == TIME_LIMIT) { timeTweetDownloadLimit = limit; } else if (getType() == NUMBER_LIMIT) { nTweetDownloadLimit = limit; } ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey(getConsumerKey()).setOAuthConsumerSecret(getConsumerSecret()) .setOAuthAccessToken(getAccessToken()).setOAuthAccessTokenSecret(getAccessTokenSecret()) .setJSONStoreEnabled(true); connectMongoDB(); listener = new TweetDownloadListener(this); twitterStream = new TwitterStreamFactory(cb.build()).getInstance(); twitterStream.addListener(listener); }
From source file:edu.uci.ics.asterix.external.util.TwitterUtil.java
License:Apache License
public static TwitterStream getTwitterStream(Map<String, String> configuration) { ConfigurationBuilder cb = getAuthConfiguration(configuration); TwitterStreamFactory factory = new TwitterStreamFactory(cb.build()); return factory.getInstance(); }
From source file:flight_ranker.Flight_colllector.java
public static void main(String[] args) throws FileNotFoundException, IOException { ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true);/* w ww .j ava 2s. c o m*/ cb.setOAuthConsumerKey("Oa6WAzH0j3sgVrP0CNGvxnWA2"); cb.setOAuthConsumerSecret("sLdoFybvJvVFz7Lxbbv9KWQDFeKcVeZAkWDC4QMHnx5lV2OmGE"); cb.setOAuthAccessToken("2691889945-5NOBWKUgT9FiAoyOQSCFg8CLlPRlDMbWcUrJBdK"); cb.setOAuthAccessTokenSecret("J6tA8Sxrtz2JNSFdQwAonbGxNfLNuD9I54Zfvomku3p5t"); TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance(); StatusListener listener = new StatusListener() { @Override public void onException(Exception arg0) { // TODO Auto-generated method stub } @Override public void onDeletionNotice(StatusDeletionNotice arg0) { // TODO Auto-generated method stub } @Override public void onScrubGeo(long arg0, long arg1) { // TODO Auto-generated method stub } @Override public void onStatus(Status status) { User user = status.getUser(); // gets Username String username = status.getUser().getScreenName(); long followers = user.getFollowersCount(); long retweets = status.getRetweetCount(); long favs = status.getFavoriteCount(); System.out.println("USERNAME--> " + username); System.out.println("FOLLOWERS--> " + followers); String profileLocation = user.getLocation(); // System.out.println("RETWEETS--> "+retweets); // System.out.println("FAVOURITES--> "+favs); System.out.println("LOCATION--> " + profileLocation); long tweetId = status.getId(); System.out.println("TWEET ID--> " + tweetId); String content = status.getText(); System.out.println("TWEET--> " + content + "\n"); BufferedWriter b1 = null, b2, b3, b4, b5, b6, b7 = null; try { //output_file = new BufferedReader(new FileReader("G:\\Sentiwords.txt")); FileWriter f1 = new FileWriter("G:\\flights_data.txt", true); b1 = new BufferedWriter(f1); b1.write("#USERNAME- " + username); b1.newLine(); b1.write("#Followers- " + followers); b1.newLine(); b1.write("#Location- " + profileLocation); b1.newLine(); b1.write("#ID- " + tweetId); b1.newLine(); b1.write("#Tweet- " + content); b1.newLine(); b1.newLine(); tweet_editor modified_tweet = new tweet_editor(content); //tweet_tagger tagged_tweet = new tweet_tagger(modified_tweet.edited_tweet); //System.out.println(tagged_tweet.tagged); sentiment_calculator senti_value = new sentiment_calculator(modified_tweet.edited_tweet); if (content.contains("Indigo")) { System.out.println("indigo"); FileWriter f2 = new FileWriter("G:\\Indigo.txt", true); b2 = new BufferedWriter(f2); b2.write(Double.toString(senti_value.senti_rate)); b2.newLine(); } if (content.contains("Jet")) { System.out.println("jet"); FileWriter f3 = new FileWriter("G:\\jet.txt", true); b3 = new BufferedWriter(f3); b3.write(Double.toString(senti_value.senti_rate)); b3.newLine(); } if (content.contains("Indian")) { System.out.println("indian"); FileWriter f4 = new FileWriter("G:\\Indian.txt", true); b4 = new BufferedWriter(f4); b4.write(Double.toString(senti_value.senti_rate)); b4.newLine(); } if (content.contains("Spicejet")) { System.out.println("spicejet"); FileWriter f5 = new FileWriter("G:\\spicejet.txt", true); b5 = new BufferedWriter(f5); b5.write(Double.toString(senti_value.senti_rate)); b5.newLine(); } if (content.contains("AirAsia")) { System.out.println("airasia"); FileWriter f6 = new FileWriter("G:\\airasia.txt", true); b6 = new BufferedWriter(f6); b6.write(Double.toString(senti_value.senti_rate)); b6.newLine(); } try { //output_file = new BufferedReader(new FileReader("G:\\Sentiwords.txt")); FileWriter f7 = new FileWriter("G:\\flight_senti.txt", true); b7 = new BufferedWriter(f7); b7.write(String.valueOf(senti_value.senti_rate)); b7.newLine(); } catch (IOException e) { e.printStackTrace(); } finally { try { b7.close(); } catch (IOException ex) { Logger.getLogger(Flight_colllector.class.getName()).log(Level.SEVERE, null, ex); } } } catch (IOException e) { e.printStackTrace(); } finally { try { b1.close(); } catch (IOException ex) { Logger.getLogger(Flight_colllector.class.getName()).log(Level.SEVERE, null, ex); } } } @Override public void onTrackLimitationNotice(int arg0) { // TODO Auto-generated method stub } @Override public void onStallWarning(StallWarning sw) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } }; FilterQuery fq = new FilterQuery(); String keywords[] = { "Indian Airlines, Indigo Airlines, Indigo Airline , Indian Airline , Spicejet , jetAirways , Jet Airways, Jet Airlines , airasia" }; //we will pass stock related keyword here fq.track(keywords); twitterStream.addListener(listener); twitterStream.filter(fq); }
From source file:kr.debop4j.search.twitter.Twitters.java
License:Apache License
public synchronized static TwitterStream getTwitterStream() { if (twitterStream == null) twitterStream = new TwitterStreamFactory(getConfiguration()).getInstance(); return twitterStream; }
From source file:my.twittergui.TwitterUI.java
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed String text;/* ww w. j a v a 2s . co m*/ text = jTextField1.getText(); //jTextArea1.append(text+"\n"); jTextArea1.append("Searching for: "); String[] strarray = text.split(" "); for (int i = 0; i < strarray.length; i++) { jTextArea1.append(strarray[i] + "\n"); } File filed = null; filed = new File("C:\\Results"); if (!filed.exists()) { filed.mkdir(); } ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey("").setOAuthConsumerSecret("").setOAuthAccessToken("") .setOAuthAccessTokenSecret(""); StatusListener listener = new StatusListener() { public void onStatus(Status status) { try { int fileno = 0; String strI = Integer.toString(fileno); String fname = "C:\\results\\DataCol" + strI + ".csv"; File file = new File(fname); FileWriter bw = new FileWriter(file, true); if (file.length() == 0) { bw.write("\"Screen Name\", text, \"created at\", geolocation, Retweet? \r\n"); bw.flush(); } while (file.length() > 10485760) { fileno += 1; strI = Integer.toString(fileno); fname = "DataCol" + strI + ".txt"; file = new File(fname); bw = new FileWriter(file, true); bw.write("\"Screen Name\", \"text\", \"created_at\", \"geolocation\" \r\n"); bw.flush(); } // if(!status.isRetweet()){ bw.write("\r\n"); bw.write("\"" + status.getUser().getScreenName() + "\","); String tweettxt = status.getText(); tweettxt = tweettxt.replace("\n", ""); tweettxt = tweettxt.replace(",", ""); tweettxt = tweettxt.replace("\"", ""); bw.write("\"" + tweettxt + "\","); bw.write("\"" + status.getCreatedAt() + "\","); if (status.getGeoLocation() != null) { bw.write("\"" + status.getGeoLocation() + "\""); } else bw.write("N/A,"); if (status.isRetweet()) bw.write("Yes"); else bw.write("No"); bw.flush(); bw.close(); //System.out.print("\n"); String str = "@" + status.getUser().getScreenName() + " " + status.getText() + " " + status.getCreatedAt() + " "; if (status.getGeoLocation() != null) str += status.getGeoLocation(); //System.out.print(str); jTextArea1.append(str + "\n"); // } bw.close(); } catch (IOException e) { //System.out.print("EXCEPTION"); e.printStackTrace(); } } public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { } public void onTrackLimitationNotice(int numberOfLimitedStatuses) { } public void onStallWarning(StallWarning stall) { } public void onScrubGeo(long a, long b) { } public void onException(Exception ex) { ex.printStackTrace(); } }; TwitterStreamFactory tf = new TwitterStreamFactory(cb.build()); TwitterStream twitterStream = tf.getInstance(); twitterStream.addListener(listener); // Filter FilterQuery filtre = new FilterQuery(); Scanner in = new Scanner(System.in); filtre.track(strarray); twitterStream.filter(filtre); }
From source file:net.nokok.twitduke.core.factory.TTwitterStreamFactory.java
License:Open Source License
/** * ??????//from w ww .j av a2 s . c om * * @param accessToken * * @return ??? */ public static TwitterStream newInstance(AccessToken accessToken) { final TwitterStream twitterStream = new TwitterStreamFactory(ConfigurationProvider.getConfiguration()) .getInstance(accessToken); ShutdownHook.INSTANCE.addHook("Twitter Stream Shutdown Thread", twitterStream::shutdown); return twitterStream; }
From source file:nyu.twitter.lg.FentchTwitter.java
License:Open Source License
public static void invoke() throws Exception { init();// w w w.j a v a2s . co m // Create table if it does not exist yet if (Tables.doesTableExist(dynamoDB, tableName)) { System.out.println("Table " + tableName + " is already ACTIVE"); } else { // Create a table with a primary hash key named 'name', which holds // a string CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName) .withKeySchema(new KeySchemaElement().withAttributeName("id").withKeyType(KeyType.HASH)) .withAttributeDefinitions(new AttributeDefinition().withAttributeName("id") .withAttributeType(ScalarAttributeType.S)) .withProvisionedThroughput( new ProvisionedThroughput().withReadCapacityUnits(1L).withWriteCapacityUnits(1L)); TableDescription createdTableDescription = dynamoDB.createTable(createTableRequest) .getTableDescription(); System.out.println("Created Table: " + createdTableDescription); // Wait for it to become active System.out.println("Waiting for " + tableName + " to become ACTIVE..."); Tables.waitForTableToBecomeActive(dynamoDB, tableName); } ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey("Emwo2pG").setOAuthConsumerSecret("RM9B7fske5T") .setOAuthAccessToken("19ubQOirq").setOAuthAccessTokenSecret("Lbg3C"); final TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance(); StatusListener listener = new StatusListener() { @Override public void onStatus(Status status) { if (status.getGeoLocation() != null && status.getPlace() != null) { // if (count == 0) { // count++; // } // latitude = status.getGeoLocation().getLatitude(); longtitude = status.getGeoLocation().getLongitude(); place = status.getPlace().getCountry() + "," + status.getPlace().getFullName(); date = status.getCreatedAt().toString(); id = Integer.toString(count); name = status.getUser().getScreenName(); message = status.getText(); System.out.println("---------------------------"); System.out.println("ID:" + count); System.out.println("latitude:" + latitude); System.out.println("longtitude:" + longtitude); System.out.println("place:" + place); System.out.println("name:" + name); System.out.println("message:" + message); System.out.println("data:" + date); System.out.println("-------------8-------------"); insertDB(id, count, name, longtitude, latitude, place, message, date); if (++count > 100) { twitterStream.shutdown(); System.out.println("Information Collection Completed"); } // count = (count+1) % 101; } } @Override public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { // System.out.println("Got a status deletion notice id:" // + statusDeletionNotice.getStatusId()); } @Override public void onTrackLimitationNotice(int numberOfLimitedStatuses) { // System.out.println("Got track limitation notice:" // + numberOfLimitedStatuses); } @Override public void onScrubGeo(long userId, long upToStatusId) { // System.out.println("Got scrub_geo event userId:" + userId // + " upToStatusId:" + upToStatusId); } @Override public void onStallWarning(StallWarning warning) { // System.out.println("Got stall warning:" + warning); } @Override public void onException(Exception ex) { ex.printStackTrace(); } }; twitterStream.addListener(listener); twitterStream.sample(); }
From source file:OAuth.MyOwnTwitterFactory.java
License:Open Source License
public TwitterStream createOneTwitterStreamInstance(AccessToken accessToken) { TwitterStream twitterStream;/*from w w w. j a va 2 s .c o m*/ ConfigurationBuilder builder = new ConfigurationBuilder(); builder.setOAuthConsumerKey(APIkeys.getTwitterAPIKey()); builder.setOAuthConsumerSecret(APIkeys.getTwitterAPISecret()); System.out.println("AT: " + accessToken.getToken()); System.out.println("ATS: " + accessToken.getTokenSecret()); builder.setOAuthAccessToken(accessToken.getToken()); builder.setOAuthAccessTokenSecret(accessToken.getTokenSecret()); builder.setJSONStoreEnabled(true); // builder.setOAuthAccessToken("31805620-QQy8TFFDKRxWyOUVnY08UcxT5bzrFhRWUa0A3lEW3"); // builder.setOAuthAccessTokenSecret("iJuCkdgrfIpGn5odyF2evMSvAsovreeEV6cZU5ihVVI7j"); Configuration configuration = builder.build(); TwitterStreamFactory factory = new TwitterStreamFactory(configuration); twitterStream = factory.getInstance(); return twitterStream; }
From source file:OAuth.MyOwnTwitterFactory.java
License:Open Source License
public TwitterStream createOneTwitterStreamInstanceFromApp(String APIKey, String APIKeySecret, String accessToken, String accessTokenSecret) { TwitterStream twitterStream;//from w w w . ja va 2 s . co m ConfigurationBuilder builder = new ConfigurationBuilder(); if (!Admin.isTest()) { builder.setOAuthConsumerKey(APIKey); builder.setOAuthConsumerSecret(APIKeySecret); builder.setOAuthAccessToken(accessToken); builder.setOAuthAccessTokenSecret(accessTokenSecret); builder.setJSONStoreEnabled(true); } else { builder.setOAuthConsumerKey("KNjw1QTK1hJKx8LpK6X6rg"); builder.setOAuthConsumerSecret("ikX9blowuh3FqFAkIg5LQi5voLOV413EWzPsDl77uU"); builder.setOAuthAccessToken("31805620-QQy8TFFDKRxWyOUVnY08UcxT5bzrFhRWUa0A3lEW3"); builder.setOAuthAccessTokenSecret("iJuCkdgrfIpGn5odyF2evMSvAsovreeEV6cZU5ihVVI7j"); builder.setJSONStoreEnabled(true); } Configuration configuration = builder.build(); TwitterStreamFactory factory = new TwitterStreamFactory(configuration); twitterStream = factory.getInstance(); return twitterStream; }