List of usage examples for twitter4j.conf ConfigurationBuilder setDebugEnabled
public ConfigurationBuilder setDebugEnabled(boolean debugEnabled)
From source file:crawling.PrintSampleStream.java
License:Apache License
private static TwitterStream getOAuthTwitterStream() { File file = new File(fileName); InputStream is = null;/*from w ww . ja v a 2 s.c o m*/ try { if (file.exists()) { is = new FileInputStream(file); prop.load(is); } else { System.out.println(fileName + " doesn't exist!"); System.exit(-1); } } catch (IOException ioe) { ioe.printStackTrace(); System.exit(-1); } ConfigurationBuilder cb = new ConfigurationBuilder(); /*cb.setDebugEnabled(true) .setOAuthConsumerKey("Oo1Np62vR41MJTeVViKMmA") .setOAuthConsumerSecret("9e2ad6usWLJikjoI4yNuDQ85rrf9eRvfLmUDRBPtZ8") .setOAuthAccessToken("700944906-6zdtjgw0Tskn90eDTB36sx1RCOaTff9C2zt1QYa8") .setOAuthAccessTokenSecret("x56tcXGBdQ2TZOs4Ci69Rm2QiUJMgjKVQTn1W7uM");*/ cb.setDebugEnabled(true).setOAuthConsumerKey(prop.getProperty("oauth.consumerKey")) .setOAuthConsumerSecret(prop.getProperty("oauth.consumerSecret")) .setOAuthAccessToken(prop.getProperty("oauth.accessToken")) .setOAuthAccessTokenSecret(prop.getProperty("oauth.accessTokenSecret")); return new TwitterStreamFactory(cb.build()).getInstance(); }
From source file:crawling.SearchTweetsHashtag.java
License:Apache License
private static Twitter getOAuthTwitter() { ConfigurationBuilder cb = new ConfigurationBuilder(); /*cb.setDebugEnabled(true) .setOAuthConsumerKey("Oo1Np62vR41MJTeVViKMmA") .setOAuthConsumerSecret("9e2ad6usWLJikjoI4yNuDQ85rrf9eRvfLmUDRBPtZ8") .setOAuthAccessToken("701461304-mbDoQtYqtSG9Q2RHLlSBUFEgRWbKE0CpViYowNXs") .setOAuthAccessTokenSecret("aWeao9VabuGfphGcH83SozAwI0hgmlmg3ptS5KGGVs"); *//*from w w w. j av a2 s . co m*/ cb.setDebugEnabled(true).setOAuthConsumerKey(prop.getProperty("oauth.consumerKey")) .setOAuthConsumerSecret(prop.getProperty("oauth.consumerSecret")) .setOAuthAccessToken(prop.getProperty("oauth.accessToken")) .setOAuthAccessTokenSecret(prop.getProperty("oauth.accessTokenSecret")); return new TwitterFactory(cb.build()).getInstance(); }
From source file:crawltweets2mongo.CrawlTweets2Mongo.java
public static void getTweetByQuery(boolean loadRecords, String keyword) { if (collection != null) { final String api_key = "your api key in here"; final String api_secret = "applied it from twitter website"; final String access_token = "applied token from twitter website"; final String access_token_secret = "applied it from twitter website"; // The factory instance is re-useable and thread safe. if (twitter == null) { ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(false).setOAuthConsumerKey(api_key).setOAuthConsumerSecret(api_secret) .setOAuthAccessToken(access_token).setOAuthAccessTokenSecret(access_token_secret); TwitterFactory tf = new TwitterFactory(cb.build()); twitter = tf.getInstance();/*from ww w. j a v a 2s. c o m*/ } threadMessage("Starting MessageLoop thread"); long startTime = System.currentTimeMillis(); Thread t = new Thread(new MonoThread(collection, twitter)); t.start(); threadMessage("Waiting for MessageLoop thread to finish"); // loop until MessageLoop // thread exits while (t.isAlive()) { try { t.join(28000); t.join(28000); t.join(28000); if (((System.currentTimeMillis() - startTime) > 1000 * 60 * 60) // long patience = 1000 * 60 * 60; && t.isAlive()) { threadMessage("Tired of waiting!"); t.interrupt(); t.join(); } } catch (InterruptedException ex) { Logger.getLogger(CrawlTweets2Mongo.class.getName()).log(Level.SEVERE, null, ex); } } threadMessage("Finally!"); } else { System.out.println("MongoDB is not Connected! Please check mongoDB intance running.."); } }
From source file:crosstreams.twitter.TwitterStreamFileWriter.java
License:Mozilla Public License
/** * Start crawling tweets/*from ww w . ja v a 2s . c o m*/ * @param args * @throws TwitterException */ public static void main(String[] args) throws TwitterException { System.err.println("### Twitter Stream Writer ###"); System.err.println("Saves tweets from the Spritzer/Gardenhose Stream to a series of files"); System.err.println( "Command: crosstreams.twitter.TwitterStreamFileWriter <saveFolder> <twitterusername> <twitterpassword> <numberoftweetstostoreperfile>(optional)"); System.err.println(" saveFolder: Where the tweets will be downloaded to"); System.err.println(" twitterusername: The username of the twitter account to use for downloading tweets"); System.err.println(" twitterpassword: The password of the twitter account to use for downloading tweets"); System.err.println( " numberoftweetstostoreperfile: The total number of tweets to write to a file before closing that file and opening a new one (Integer) (defaults=1000000)"); System.err.println("Optional System Properties (-D):"); System.err.println(" http.proxyhost: The proxy host to use if needed"); System.err.println(" http.proxyport: The proxy port to use if needed"); System.err.println(" email: An email address to send alerts to if an error is encountered"); System.err.println(" emailconf: An file containing the javax.mail configuration"); System.err.println( " emailonvalidate: true/false - should I send an email when a file is correctly validated rather than only when it fails? (default=false)"); if (args.length <= 1 || args.length >= 5) { System.err.println("Example:"); System.err.println( "java -Demail=\"MYEMAIL@HOST.COM\" -Demailconf=\"./javamail.conf\" -Demailonvalidate=\"true\" -jar TwitterStreamFileCrawler.jar ./ MYUSERNAME MYPASSWORD 100000"); System.err.println("Don't forget to modify ./javamail.conf to contain your email server host"); System.exit(0); } // user inputs String saveFolder = args[0]; String username = args[1]; String password = args[2]; final int numberOfTweetsToStorePerFile; if (args.length > 2) numberOfTweetsToStorePerFile = Integer.parseInt(args[3]); else numberOfTweetsToStorePerFile = 1000000; String proxyhost = System.getProperty("http.proxyhost"); String proxyport = System.getProperty("http.proxyport"); final String email = System.getProperty("email"); final String emailconf = System.getProperty("emailconf"); // define the user account in use and proxy settings if needed ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true); if (proxyhost != null && proxyport != null) { cb.setHttpProxyHost(proxyhost); cb.setHttpProxyPort(Integer.parseInt(proxyport)); } cb.setUser(username); cb.setPassword(password); if (!saveFolder.endsWith("/") && !saveFolder.endsWith("\\")) { saveFolder = saveFolder + System.getProperty("file.separator"); } final String finalSaveFolder = saveFolder; // Twitter4J Stream - the type of stream is set automatically, i.e. Gardenhose if you have it, Spritzer otherwise. TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance(); // The status listener is the important bit, this fires when a new tweet arrives. StatusListener listener = new StatusListener() { /** The status listener holds a writer to save content to **/ BufferedWriter statusWriter = null; // the tweets go here BufferedWriter logWriter = null; // we write any delete requests or error messages here /** We store a fixed number of Tweets in each file **/ int numberInThisFile = numberOfTweetsToStorePerFile; int numberPerFile = numberOfTweetsToStorePerFile; String currentFilename; int numerrors = 0; /** * A new tweet has arrived */ public void onStatus(Status status) { if (numberInThisFile >= numberPerFile) { // closing and opening of new files try { if (statusWriter != null) { statusWriter.close(); logWriter.close(); validateJSONFile(currentFilename, numberPerFile); } Long currentTime = System.currentTimeMillis(); currentFilename = finalSaveFolder + currentTime.toString() + ".json.gz"; statusWriter = new BufferedWriter(new OutputStreamWriter( new GZIPOutputStream(new FileOutputStream(currentFilename)), "UTF-8")); logWriter = new BufferedWriter(new OutputStreamWriter( new GZIPOutputStream( new FileOutputStream(finalSaveFolder + currentTime.toString() + ".log.gz")), "UTF-8")); numberInThisFile = 0; numerrors = 0; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } numberInThisFile++; // write the JSON - note that I added the getJSON() method to the Twitter4J status object // this is why the Twitter4j sources are included rather than importing the jar. try { Object s = status.getJSON(); statusWriter.write(status.getJSON().toString() + '\n'); statusWriter.flush(); } catch (Exception e) { e.printStackTrace(); numerrors++; if (emailconf != null && email != null && numerrors < 5) Mail.mail(emailconf, email, email, "Twitter Stream Writer Alert - Write Failed", "An IOException was thrown when calling statusWriter.write()." + '\n' + e.getMessage() + '\n' + "The current file will be closed and a new file will be created."); } } public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { try { logWriter.write("DEL: " + statusDeletionNotice.getStatusId() + " " + statusDeletionNotice.getUserId() + '\n'); } catch (IOException e) { e.printStackTrace(); } } public void onTrackLimitationNotice(int numberOfLimitedStatuses) { try { logWriter.write("LIMIT: " + numberOfLimitedStatuses + '\n'); } catch (IOException e) { e.printStackTrace(); } } public void onScrubGeo(long userId, long upToStatusId) { try { logWriter.write("SCRUBGEO: " + userId + " " + upToStatusId + '\n'); } catch (IOException e) { e.printStackTrace(); } } public void onException(Exception ex) { if (logWriter == null) return; try { logWriter.write("ERR: " + ex.getLocalizedMessage() + '\n'); logWriter.flush(); if (statusWriter != null) { statusWriter.close(); statusWriter = null; logWriter.close(); validateJSONFile(currentFilename, numberPerFile); } } catch (IOException e) { e.printStackTrace(); } //ex.printStackTrace(); } }; if (emailconf != null && email != null) Mail.mail(emailconf, email, email, "Twitter Stream Writer Info - Writer has started", "The Gardenhose Writer has begun crawling the stream (this email indicates that you will recieve alerts if something goes wrong."); twitterStream.addListener(listener); twitterStream.sample(); }
From source file:cz.mgn.collabdesktop.utils.apis.twitter.CollabTwitter.java
License:Open Source License
public static void tweet(String roomName, String text, BufferedImage roomImage, String accessToken, String accessTokenSecret) { try {//w w w . j ava 2s .c om ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true) // .setOAuthConsumerKey("PLFCUaLARG3rW66MbME1Kg") // twitter app: MG Collab desktop .setOAuthConsumerSecret("Rf5G5htymWOVnC4KGT5lSzaAzlgW6mUwXNIPnywI8") // .setOAuthAccessToken(accessToken) // .setOAuthAccessTokenSecret(accessTokenSecret); Twitter twitter = new TwitterFactory(cb.build()).getInstance(); StatusUpdate su = new StatusUpdate(text); su.media(roomName, makeInputStreamFromImage(roomImage)); twitter.updateStatus(su); } catch (IOException ex) { Logger.getLogger(CollabTwitter.class.getName()).log(Level.SEVERE, null, ex); } catch (TwitterException ex) { Logger.getLogger(CollabTwitter.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:d3V.GetFriendsIDs.java
License:Apache License
/** * Usage: java twitter4j.examples.friendsandfollowers.GetFollowersIDs [screen name] * * @param args message/*from ww w.j av a 2s . c om*/ */ public static void main(String[] args) { ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey("Em3WTI7jc90HcvKzPkTLQ") .setOAuthConsumerSecret("vg4p6rOF32bmffqRR8m0jAUClrxvtGiMB5PrSr3Zsw") .setOAuthAccessToken("1681973072-1q0zI0VPjHD3ttNuaBOL94frzCI9sXInxAcDK0w") .setOAuthAccessTokenSecret("ZRLkOyjmhHBkU1iNyEVNyIgIBsKrl0DUDKOcOMneYFYEM"); cb.setJSONStoreEnabled(true); TwitterFactory tf = new TwitterFactory(cb.build()); try { String core_user = "nytimes"; FileWriter writer = new FileWriter(core_user + "_friends.txt", true); Twitter twitter = tf.getInstance(); long cursor = -1; ArrayList<String> directFriends = new ArrayList<String>(); PagableResponseList<User> tempFriends = null; System.out.println("Listing " + core_user + " friends's ScreenNames."); int count = 0; //boolean first = true; //getFriends(writer, twitter, cursor, count, core_user); do { tempFriends = twitter.getFriendsList(core_user, cursor); for (User user : tempFriends) { directFriends.add(user.getScreenName()); writer.write(core_user + "\t" + user.getScreenName() + "\n"); } count += tempFriends.size(); System.out.println(core_user + ": " + count); if (count >= 100) break; Thread.sleep(300 * MILLISECOND); } while ((cursor = tempFriends.getNextCursor()) != 0); System.out.println("Size of DFs: " + directFriends.size()); // for (String user : directFriends){ for (int i = 0; i < directFriends.size(); i++) { // PagableResponseList<User> idrfriends = null; long _cursor = -1; int _count = 0; String _username = directFriends.get(i); System.out.println("\nFriends of Direct friend: " + _username + "(" + (i + 1) + ")"); try { getFriends(writer, twitter, _cursor, _count, _username); } catch (TwitterException e) { e.printStackTrace(); System.out.println("Failed to get friends' ids: " + e.getMessage()); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } writer.close(); System.out.println(count); System.exit(0); } catch (TwitterException e) { e.printStackTrace(); System.out.println("Failed to get friends' ids: " + e.getMessage()); System.exit(-1); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } }
From source file:datasite.DataSite.java
public static void main(String[] args) { ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true); cb.setOAuthConsumerKey("ConsumerKey"); cb.setOAuthConsumerSecret("ConsumerSecret"); cb.setOAuthAccessToken("AccessToken"); cb.setOAuthAccessTokenSecret("AccessTokenSecret"); TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance(); StatusListener listener;//from w w w . j a v a 2s. co m 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(); try (PrintWriter out = new PrintWriter( new BufferedWriter(new FileWriter("tweet_input\\tweets.txt", true)))) { //out.println(username); String content = status.getText(); out.println(content + "\n"); //System.out.append( content +"\n"); } catch (IOException ex) { Logger.getLogger(DataSite.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[] = { "africa" }; fq.track(keywords); twitterStream.addListener(listener); twitterStream.filter(fq); //twitterStream.(); }
From source file:datasite.SortedUnique.java
public static void main(String[] args) { ConfigurationBuilder confb = new ConfigurationBuilder(); confb.setDebugEnabled(true); confb.setOAuthConsumerKey("Consumer Key"); confb.setOAuthConsumerSecret("Consumer Secret"); confb.setOAuthAccessToken("Access Token"); confb.setOAuthAccessTokenSecret("Access Token Secret"); TwitterStream twitterStream = new TwitterStreamFactory(confb.build()).getInstance(); StatusListener listener;//from w w w .j a va 2 s.co m 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) { PrintStream out = null; try { User user = status.getUser(); // gets Username String username = status.getUser().getScreenName(); String content = status.getText(); Set<String> userWrds = new HashSet<String>(); // HashSet implements Set Scanner sc = new Scanner(content + user); while (sc.hasNext()) { String word = sc.next(); userWrds.add(word); } out = new PrintStream(new FileOutputStream("tweet_output\\unique.txt", true)); Iterator<String> iter = userWrds.iterator(); for (int i = 1; i <= 1 && iter.hasNext(); i++) //System.out.println(iter.next()); out.println(userWrds.size()); } catch (FileNotFoundException ex) { Logger.getLogger(SortedUnique.class.getName()).log(Level.SEVERE, null, ex); } finally { out.close(); } } @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[] = { "africa" }; fq.track(keywords); twitterStream.addListener(listener); twitterStream.filter(fq); //twitterStream.(); }
From source file:de.ecw.ant.twitter.AntTwitterTask.java
License:Apache License
/** * Executes Ant task://ww w . j a v a 2 s.c o m * <ul> * <li>establish a connection to Twitter via twitter4j</li> * <li>if enabled, execute URL shortening via bitly</li> * <li>check the input message and split it into chunks if message is larger * than 140 characters</li> * <li>update status of every message</li> * </ul> */ public void execute() throws BuildException { String useMessage = getMessage(); // validate Twitter parameters if ((getConsumerKey() == null) || ((getConsumerKey() != null) && (getConsumerKey().length() == 0)) || (getConsumerSecret() == null) || ((getConsumerSecret() != null) && (getConsumerSecret().length() == 0))) { log("You have to enter a valid Twitter username/password combination, missing arguments!", Project.MSG_ERR); return; } // should URLs be shortened? if ((getEnableBitly() != null) && (getEnableBitly().toLowerCase().equals(TRUE))) { // validate bit.ly parameters if ((getBitlyUsername() == null) || ((getBitlyUsername() != null) && (getBitlyUsername().length() == 0)) || (getBitlyApiKey() == null) || ((getBitlyApiKey() != null) && (getBitlyApiKey().length() == 0))) { log("You have enabled bit.ly support, but bit.ly username or API key is missing - bit.ly support is disabled", Project.MSG_WARN); } else { useMessage = shortenUrls(message); } } // new Twitter client ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey(getConsumerKey()).setOAuthConsumerSecret(getConsumerSecret()) .setOAuthAccessToken(getAccessToken()).setOAuthAccessTokenSecret(getAccessTokenSecret()); TwitterFactory tf = new TwitterFactory(cb.build()); Twitter twitter = tf.getInstance(); // assume that all messages are longer than 140 chars List<String> messages = AntTwitterTask.splitMessage(useMessage); log("Twittering update (" + messages.size() + " tweets)", Project.MSG_INFO); int iTotalPosts = messages.size(); int iSuccededUpdates = 0; try { // post every tweet for (int i = 0; i < iTotalPosts; i++) { String msg = messages.get(i); log(msg.length() + " chars: " + msg, Project.MSG_INFO); twitter.updateStatus(msg); iSuccededUpdates++; } } catch (TwitterException e) { log("Failed to update Twitter status", e, Project.MSG_ERR); } log("Tweets posted: " + iSuccededUpdates + "/" + iTotalPosts, Project.MSG_INFO); }
From source file:de.jetsli.twitter.TwitterSearch.java
License:Apache License
/** * Connect with twitter to get a new personalized twitter4j instance. * * @throws RuntimeException if verification or connecting failed */// ww w.ja v a2s. c o m public TwitterSearch initTwitter4JInstance(String token, String tokenSecret, boolean verify) { if (consumerKey == null) throw new NullPointerException("Please use init consumer settings!"); setupProperties(); ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey(consumerKey).setOAuthConsumerSecret(consumerSecret) .setOAuthAccessToken(token).setOAuthAccessTokenSecret(tokenSecret); TwitterFactory tf = new TwitterFactory(cb.build()); twitter = tf.getInstance(); try { // RequestToken requestToken = t.getOAuthRequestToken(); // System.out.println("TW-URL:" + requestToken.getAuthorizationURL()); if (verify) twitter.verifyCredentials(); String str = twitter.getScreenName(); logger.info("create new TwitterSearch for " + str + " with verifification:" + verify); } catch (TwitterException ex) { // if (checkAndWaitIfRateLimited("initTwitter", ex)) // return this; throw new RuntimeException(ex); } return this; }