List of usage examples for twitter4j Status getUser
User getUser();
From source file:PrintRetweetStream.java
License:Apache License
/** * Main entry of this application.//from w ww . j a v a2s .co m * * @param args arguments doesn't take effect with this example * @throws TwitterException when Twitter service or network is unavailable */ public static void main(String[] args) throws TwitterException { ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true); cb.setOAuthConsumerKey(""); cb.setOAuthConsumerSecret(""); cb.setOAuthAccessToken(""); cb.setOAuthAccessTokenSecret(""); TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance(); StatusListener listener = new StatusListener() { @Override public void onStatus(Status status) { System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText()); } @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.retweet(); }
From source file:TimeLine.java
private void btTwitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btTwitActionPerformed // Tombol update status, show setelah update txtStatus.setText(""); TwitterFactory tf = new TwitterFactory(); Twitter twitter = tf.getInstance();/*from ww w .j a v a2 s . c om*/ try { Status status = twitter.updateStatus(txStatus.getText()); JOptionPane.showMessageDialog(rootPane, "Twit \n [ " + status.getText() + " ]\nTerkirim!"); } catch (TwitterException ex) { JOptionPane.showMessageDialog(rootPane, "Tidak bisa mengirim : " + ex.getMessage()); Logger.getLogger(TimeLine.class.getName()).log(Level.SEVERE, null, ex); } List<Status> statuses = null; try { statuses = twitter.getUserTimeline(); statuses.stream().forEach((status) -> { txtStatus.append(status.getUser().getName() + " : " + status.getText() + " - " + status.getCreatedAt() + " \n Via : " + status.getSource() + "\n\n"); }); txStatus.setText(""); //reload field } catch (TwitterException te) { JOptionPane.showMessageDialog(rootPane, "Failed to Show Status!" + te.getMessage()); } }
From source file:TwitterGateway.java
@Override public void onStatus(Status status) { user = status.getUser().getScreenName(); String mentionStatus = status.getText(); System.out.println("@" + user + " - " + mentionStatus); String paramScreenName = screenName.toLowerCase(); mentionStatus = mentionStatus.toLowerCase().replace(paramScreenName, "").trim(); String locale = new String(); if (mentionStatus.contains(" to ")) { location = mentionStatus.split(" to "); locale = "en"; } else if (mentionStatus.contains(" ke ")) { location = mentionStatus.split(" ke "); locale = "id"; } else {/*from w w w. j a v a 2 s . c o m*/ location = null; } boolean statusLocation1 = false; boolean statusLocation2 = false; if (mentionStatus.equals("help") || mentionStatus.equals(("bantuan"))) { try { if (mentionStatus.equals("help")) { Tweet(user, "For using this Twitter bot for searching public transport route, you can mention..."); Tweet(user, "'First location' to 'second location', example : BIP to PVJ"); } else { Tweet(user, "Format penggunaan Twitter bot untuk mencari jalur transportasi publik adalah..."); Tweet(user, "'Lokasi awal' ke 'lokasi tujuan', contoh : BIP ke PVJ."); } } catch (TwitterException ex) { System.out.println("Tweet help error"); } } else if (location.length == 2 && !location[0].contains("@") && !location[1].contains("@")) { try { if (location[0].equals(location[1])) { if (locale.equals("id")) { Tweet(user, "Pencarian tidak dapat dilakukan karena lokasi awal dan lokasi tujuan sama"); } else if (locale.equals("en")) { Tweet(user, "Route can't be found. Starting location and destination are similar"); } } else { System.out.println("Lokasi 1 : " + location[0].trim()); System.out.println("Lokasi 2 : " + location[1].trim()); //string destination menampung hasil dari JSONObject hasil pencarian apa saja yang ditemukan dari KIRIGateway.getLatLong String destination1 = KIRIGateway.GetLatLong(location[0]); String destination2 = KIRIGateway.GetLatLong(location[1]); //dimasukan ke JSONObject JSONObject objDest1 = new JSONObject(destination1); JSONObject objDest2 = new JSONObject(destination2); //memasukan hasil pencarian pertama dari JSONObject ke abribut routingResponse JSONObject res1 = objDest1.getJSONArray("searchresult").getJSONObject(0); String hasilDest1 = res1.getString("placename"); latlon[0] = res1.getString("location"); if (hasilDest1 != null) { statusLocation1 = true; } JSONObject res2 = objDest2.getJSONArray("searchresult").getJSONObject(0); String hasilDest2 = res2.getString("placename"); latlon[1] = res2.getString("location"); if (hasilDest2 != null) { statusLocation2 = true; } //Mendapatkan hasil pencarian lalu dimasukan ke JSONArray paramSteps untuk dipisah-pisah lalu dimasukan ke RoutingResponse String hasilPencarian = KIRIGateway.GetTrack(latlon[0], latlon[1], locale); JSONObject objTrack = new JSONObject(hasilPencarian); JSONObject routingresults = objTrack.getJSONArray("routingresults").getJSONObject(0); JSONArray paramSteps = routingresults.getJSONArray("steps"); //buat variable step, steps, dan routing response step = new Step[paramSteps.length()]; for (int i = 0; i < step.length; i++) { step[i] = new Step(paramSteps.getJSONArray(i).getString(3) + ""); } steps = new Steps(step); routingResponse = new RoutingResponse(objTrack.getString("status"), steps); if (routingResponse.getStatus().equals("ok")) { for (int i = 0; i < routingResponse.getRoutingResult().getSteps().length; i++) { date = new Date(); Tweet(user, routingResponse.getRoutingResult().getSteps()[i].getHumanDescription()); } if (locale.equals("id")) { Tweet(user, "Untuk lebih lengkapnya dapat dilihat pada http://kiri.travel?start=" + location[0].replace(" ", "%20") + "&finish=" + location[1].replace(" ", "%20") + "®ion=bdo"); } else if (locale.equals("en")) { Tweet(user, "For futher information you can visit http://kiri.travel?start=" + location[0].replace(" ", "%20") + "&finish=" + location[1].replace(" ", "%20") + "®ion=bdo"); } for (int i = 0; i < routingResponse.getRoutingResult().getSteps().length; i++) { System.out.println("@" + user + " " + routingResponse.getRoutingResult().getSteps()[i].getHumanDescription()); } System.out.println( "@" + user + " Untuk lebih lengkap silahkan lihat di http://kiri.travel?start=" + location[0].replace(" ", "%20") + "&finish=" + location[1].replace(" ", "%20") + "®ion=bdo"); } else { System.out.println("status error"); } } } catch (Exception ex) { try { if (!statusLocation1) { date = new Date(); Tweet(user, location[0] + " tidak ditemukan"); System.out.println("@" + user + " " + location[0] + " tidak ditemukan"); } else if (!statusLocation2) { date = new Date(); Tweet(user, location[1] + " tidak ditemukan"); System.out.println("@" + user + " " + location[1] + " tidak ditemukan"); } else { date = new Date(); Tweet(user, "Gangguan Koneksi"); System.out.println("Gangguan Koneksi"); } //java.util.logging.Logger.getLogger(TwitterGateway.class.getName()).log(Level.SEVERE, null, ex); } catch (TwitterException ex1) { System.out.println("Error2"); java.util.logging.Logger.getLogger(TwitterGateway.class.getName()).log(Level.SEVERE, null, ex1); } } } }
From source file:NewMain.java
/** * @param args the command line arguments *//*from w ww. j a v a 2 s . co m*/ public static void main(String[] args) throws TwitterException { ConfigurationBuilder cb = new ConfigurationBuilder(); TwitterFactory tf = new TwitterFactory(cb.build()); Twitter twitter = tf.getInstance(); ResponseList<twitter4j.Status> statuses = twitter.getHomeTimeline(); System.out.println("Mostrando el timeline..."); for (twitter4j.Status status : statuses) { System.out.println("@" + status.getUser().getScreenName() + ": " + status.getText()); } }
From source file:PrintLinksStream.java
License:Apache License
/** * Main entry of this application./*from ww w .j a v a 2 s. co m*/ * * @param args arguments doesn't take effect with this example * @throws TwitterException when Twitter service or network is unavailable */ public static void main(String[] args) throws TwitterException { ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true); cb.setOAuthConsumerKey(""); cb.setOAuthConsumerSecret(""); cb.setOAuthAccessToken(""); cb.setOAuthAccessTokenSecret(""); TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance(); StatusListener listener = new StatusListener() { @Override public void onStatus(Status status) { System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText()); } @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.links(0); }
From source file:PrintSampleStream.java
License:Apache License
/** * Main entry of this application.// ww w . j ava2 s . c om * * @param args arguments doesn't take effect with this example * @throws TwitterException when Twitter service or network is unavailable */ public static void main(String[] args) throws TwitterException { ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true); cb.setOAuthConsumerKey(""); cb.setOAuthConsumerSecret(""); cb.setOAuthAccessToken(""); cb.setOAuthAccessTokenSecret(""); TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance(); StatusListener listener = new StatusListener() { @Override public void onStatus(Status status) { System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText()); } @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:twitterGateway_v2_06.java
License:Creative Commons License
public void SetupTwitter() { //twitterIn = new TwitterConnectStream(); //accessToken = new AccessToken(TwitterAccessToken, TwitterAccessTokenSecret); //TwitterOAuthAuthorization.setOAuthAccessToken(accessToken); //TwitterOAuthAuthorization = new OAuthAuthorization(conf); //TwitterOAuthAuthorization.setOAuthConsumer(TwitterConsumerKey, TwitterConsumerSecret); ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey(TwitterConsumerKey) .setOAuthConsumerSecret(TwitterConsumerSecret).setOAuthAccessToken(TwitterAccessToken) .setOAuthAccessTokenSecret(TwitterAccessTokenSecret); TwitterFactory tf = new TwitterFactory(cb.build()); twitterOut = tf.getInstance();// ww w .j a v a2s .co m // try { // twitterOut.updateStatus("Hello World!"); // } // catch (TwitterException ex) { // println(ex); // } ActivityLogAddLine("twitter connector ready"); output = createWriter("log.txt"); StatusListener twitterIn = new StatusListener() { public void onStatus(Status status) { double Longitude; double Latitude; GeoLocation GeoLoc = status.getGeoLocation(); if (GeoLoc != null) { //println("YES got a location"); Longitude = GeoLoc.getLongitude(); Latitude = GeoLoc.getLatitude(); } else { Longitude = 0; Latitude = 0; } println(TimeStamp() + "\t" + Latitude + "\t" + Longitude + "\t" + status.getUser().getScreenName() + "\t" + status.getText()); output.println(TimeStamp() + "\t" + Latitude + "\t" + Longitude + "\t" + status.getUser().getScreenName() + "\t" + status.getText()); output.flush(); TwitterToOsc(status.getUser().getScreenName(), status.getText()); } public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId()); } public void onTrackLimitationNotice(int numberOfLimitedStatuses) { System.out.println("Got track limitation notice:" + numberOfLimitedStatuses); } public void onScrubGeo(long userId, long upToStatusId) { System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId); } public void onException(Exception ex) { println("CAUGHT in the ACT: " + ex); } }; ConfigurationBuilder cbIn = new ConfigurationBuilder(); cbIn.setDebugEnabled(true).setOAuthConsumerKey(TwitterConsumerKey) .setOAuthConsumerSecret(TwitterConsumerSecret).setOAuthAccessToken(TwitterAccessToken) .setOAuthAccessTokenSecret(TwitterAccessTokenSecret); TwitterStreamFactory ts = new TwitterStreamFactory(cbIn.build()); TwitterStream twitterStream = ts.getInstance(); twitterStream.addListener(twitterIn); // filter() method internally creates a thread which manipulates TwitterStream and calls these adequate listener methods continuously. FilterQuery twitterFilter = new FilterQuery(0, TwitterFollowIDs, TwitterTrackWords); twitterStream.filter(twitterFilter); }
From source file:Streamer.java
@Override public void onStatus(Status status) { if ((status.getPlace() != null) && !users.contains((status.getUser().getName())) && trending.validate(status)) { data.add(status);// w ww .ja va 2 s. c o m users.add(status.getUser().getName()); } }
From source file:Demo.java
/** * Main method.//from w ww . j ava2s. c om * * @param args * @throws TwitterException */ public static void main(String[] args) throws TwitterException, IOException { // The TwitterFactory object provides an instance of a Twitter object // via the getInstance() method. The Twitter object is the API consumer. // It has the methods for interacting with the Twitter API. TwitterFactory tf = new TwitterFactory(); Twitter twitter = tf.getInstance(); boolean keepItGoinFullSteam = true; do { // Main menu Scanner input = new Scanner(System.in); System.out.print("\n--------------------" + "\nH. Home Timeline\nS. Search\nT. Tweet" + "\n--------------------" + "\nA. Get Access Token\nQ. Quit" + "\n--------------------\n> "); String choice = input.nextLine(); try { // Home Timeline if (choice.equalsIgnoreCase("H")) { // Display the user's screen name. User user = twitter.verifyCredentials(); System.out.println("\n@" + user.getScreenName() + "'s timeline:"); // Display recent tweets from the Home Timeline. for (Status status : twitter.getHomeTimeline()) { System.out.println("\n@" + status.getUser().getScreenName() + ": " + status.getText()); } } // Search else if (choice.equalsIgnoreCase("S")) { // Ask the user for a search string. System.out.print("\nSearch: "); String searchStr = input.nextLine(); // Create a Query object. Query query = new Query(searchStr); // Send API request to execute a search with the given query. QueryResult result = twitter.search(query); // Display search results. result.getTweets().stream().forEach((Status status) -> { System.out.println("\n@" + status.getUser().getName() + ": " + status.getText()); }); } // Tweet else if (choice.equalsIgnoreCase("T")) { boolean isOkayLength = true; String tweet; do { // Ask the user for a tweet. System.out.print("\nTweet: "); tweet = input.nextLine(); // Ensure the tweet length is okay. if (tweet.length() > 140) { System.out.println("Too long! Keep it under 140."); isOkayLength = false; } } while (isOkayLength == false); // Send API request to create a new tweet. Status status = twitter.updateStatus(tweet); System.out.println("Just tweeted: \"" + status.getText() + "\""); } // Get Access Token else if (choice.equalsIgnoreCase("A")) { // First, we ask Twitter for a request token. RequestToken reqToken = twitter.getOAuthRequestToken(); System.out.println("\nRequest token: " + reqToken.getToken() + "\nRequest token secret: " + reqToken.getTokenSecret()); AccessToken accessToken = null; while (accessToken == null) { // The authorization URL sends the request token to Twitter in order // to request an access token. At this point, Twitter asks the user // to authorize the request. If the user authorizes, then Twitter // provides a PIN. System.out .print("\nOpen this URL in a browser: " + "\n " + reqToken.getAuthorizationURL() + "\n" + "\nAuthorize the app, then enter the PIN here: "); String pin = input.nextLine(); try { // We use the provided PIN to get the access token. The access // token allows this app to access the user's account without // knowing his/her password. accessToken = twitter.getOAuthAccessToken(reqToken, pin); } catch (TwitterException te) { System.out.println(te.getMessage()); } } System.out.println("\nAccess token: " + accessToken.getToken() + "\nAccess token secret: " + accessToken.getTokenSecret() + "\nSuccess!"); } // Quit else if (choice.equalsIgnoreCase("Q")) { keepItGoinFullSteam = false; GeoApiContext context = new GeoApiContext() .setApiKey("AIzaSyArz1NljiDpuOriFWalOerYEdHOyi8ow8Y"); System.out.println(GeocodingApi.geocode(context, "Sigma Phi Epsilon, Lincoln, Nebraska, USA") .await()[0].geometry.location.toString()); System.out.println(GeocodingApi.geocode(context, "16th and R, Lincoln, Nebraska, USA") .await()[0].geometry.location.toString()); File htmlFile = new File("map.html"); Desktop.getDesktop().browse(htmlFile.toURI()); } // Bad choice else { System.out.println("Invalid option."); } } catch (IllegalStateException ex) { System.out.println(ex.getMessage()); } catch (Exception ex) { System.out.println(ex.getMessage()); } } while (keepItGoinFullSteam == true); }
From source file:PrintFilterStream.java
License:Apache License
/** * Main entry of this application./*w w w .j a va 2 s.co m*/ * * @param args follow(comma separated user ids) track(comma separated filter terms) * @throws TwitterException when Twitter service or network is unavailable */ public static void main(String[] args) throws TwitterException { if (args.length < 1) { System.out.println( "Usage: java twitter4j.examples.PrintFilterStream [follow(comma separated numerical user ids)] [track(comma separated filter terms)]"); System.exit(-1); } StatusListener listener = new StatusListener() { @Override public void onStatus(Status status) { System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText()); } @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(); } }; ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true); cb.setOAuthConsumerKey(""); cb.setOAuthConsumerSecret(""); cb.setOAuthAccessToken(""); cb.setOAuthAccessTokenSecret(""); TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance(); twitterStream.addListener(listener); ArrayList<Long> follow = new ArrayList<Long>(); ArrayList<String> track = new ArrayList<String>(); for (String arg : args) { if (isNumericalArgument(arg)) { for (String id : arg.split(",")) { follow.add(Long.parseLong(id)); } } else { track.addAll(Arrays.asList(arg.split(","))); } } long[] followArray = new long[follow.size()]; for (int i = 0; i < follow.size(); i++) { followArray[i] = follow.get(i); } String[] trackArray = track.toArray(new String[track.size()]); // filter() method internally creates a thread which manipulates TwitterStream and calls these adequate listener methods continuously. twitterStream.filter(new FilterQuery(0, followArray, trackArray)); }