List of usage examples for twitter4j StatusListener StatusListener
StatusListener
From source file:de.jetwick.tw.TwitterSearch.java
License:Apache License
public TwitterStream streamingTwitter(Collection<String> track, final Queue<JTweet> queue) throws TwitterException { String[] trackArray = track.toArray(new String[track.size()]); TwitterStream stream = new TwitterStreamFactory().getInstance(twitter.getAuthorization()); stream.addListener(new StatusListener() { @Override//from w w w. j a v a2s.c o m public void onStatus(Status status) { // ugly twitter ... if (Helper.isEmpty(status.getUser().getScreenName())) return; if (!queue.offer(new JTweet(toTweet(status), new JUser(status.getUser())))) logger.error("Cannot add tweet as input queue for streaming is full:" + queue.size()); } @Override public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { logger.error("We do not support onDeletionNotice at the moment! Tweet id: " + statusDeletionNotice.getStatusId()); } @Override public void onTrackLimitationNotice(int numberOfLimitedStatuses) { logger.warn("onTrackLimitationNotice:" + numberOfLimitedStatuses); } @Override public void onException(Exception ex) { logger.error("onException", ex); } @Override public void onScrubGeo(long userId, long upToStatusId) { } }); stream.filter(new FilterQuery(0, new long[0], trackArray)); return stream; }
From source file:druid.examples.twitter.TwitterSpritzerFirehoseFactory.java
License:Open Source License
@Override public Firehose connect() throws IOException { final ConnectionLifeCycleListener connectionLifeCycleListener = new ConnectionLifeCycleListener() { @Override/*from w w w . j ava 2 s . com*/ public void onConnect() { log.info("Connected_to_Twitter"); } @Override public void onDisconnect() { log.info("Disconnect_from_Twitter"); } /** * called before thread gets cleaned up */ @Override public void onCleanUp() { log.info("Cleanup_twitter_stream"); } }; // ConnectionLifeCycleListener final TwitterStream twitterStream; final StatusListener statusListener; final int QUEUE_SIZE = 2000; /** This queue is used to move twitter events from the twitter4j thread to the druid ingest thread. */ final BlockingQueue<Status> queue = new ArrayBlockingQueue<Status>(QUEUE_SIZE); final LinkedList<String> dimensions = new LinkedList<String>(); final long startMsec = System.currentTimeMillis(); dimensions.add("htags"); dimensions.add("lang"); dimensions.add("utc_offset"); // // set up Twitter Spritzer // twitterStream = new TwitterStreamFactory().getInstance(); twitterStream.addConnectionLifeCycleListener(connectionLifeCycleListener); statusListener = new StatusListener() { // This is what really gets called to deliver stuff from twitter4j @Override public void onStatus(Status status) { // time to stop? if (Thread.currentThread().isInterrupted()) { throw new RuntimeException("Interrupted, time to stop"); } try { boolean success = queue.offer(status, 15L, TimeUnit.SECONDS); if (!success) { log.warn("queue too slow!"); } } catch (InterruptedException e) { throw new RuntimeException("InterruptedException", e); } } @Override public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { //log.info("Got a status deletion notice id:" + statusDeletionNotice.getStatusId()); } @Override public void onTrackLimitationNotice(int numberOfLimitedStatuses) { // This notice will be sent each time a limited stream becomes unlimited. // If this number is high and or rapidly increasing, it is an indication that your predicate is too broad, and you should consider a predicate with higher selectivity. log.warn("Got track limitation notice:" + numberOfLimitedStatuses); } @Override public void onScrubGeo(long userId, long upToStatusId) { //log.info("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId); } @Override public void onException(Exception ex) { ex.printStackTrace(); } @Override public void onStallWarning(StallWarning warning) { System.out.println("Got stall warning:" + warning); } }; twitterStream.addListener(statusListener); twitterStream.sample(); // creates a generic StatusStream log.info("returned from sample()"); return new Firehose() { private final Runnable doNothingRunnable = new Runnable() { public void run() { } }; private long rowCount = 0L; private boolean waitIfmax = (maxEventCount < 0L); private final Map<String, Object> theMap = new HashMap<String, Object>(2); // DIY json parsing // private final ObjectMapper omapper = new ObjectMapper(); private boolean maxTimeReached() { if (maxRunMinutes <= 0) { return false; } else { return (System.currentTimeMillis() - startMsec) / 60000L >= maxRunMinutes; } } private boolean maxCountReached() { return maxEventCount >= 0 && rowCount >= maxEventCount; } @Override public boolean hasMore() { if (maxCountReached() || maxTimeReached()) { return waitIfmax; } else { return true; } } @Override public InputRow nextRow() { // Interrupted to stop? if (Thread.currentThread().isInterrupted()) { throw new RuntimeException("Interrupted, time to stop"); } // all done? if (maxCountReached() || maxTimeReached()) { if (waitIfmax) { // sleep a long time instead of terminating try { log.info("reached limit, sleeping a long time..."); sleep(2000000000L); } catch (InterruptedException e) { throw new RuntimeException("InterruptedException", e); } } else { // allow this event through, and the next hasMore() call will be false } } if (++rowCount % 1000 == 0) { log.info("nextRow() has returned %,d InputRows", rowCount); } Status status; try { status = queue.take(); } catch (InterruptedException e) { throw new RuntimeException("InterruptedException", e); } HashtagEntity[] hts = status.getHashtagEntities(); if (hts != null && hts.length > 0) { List<String> hashTags = Lists.newArrayListWithExpectedSize(hts.length); for (HashtagEntity ht : hts) { hashTags.add(ht.getText()); } theMap.put("htags", Arrays.asList(hashTags.get(0))); } long retweetCount = status.getRetweetCount(); theMap.put("retweet_count", retweetCount); User user = status.getUser(); if (user != null) { theMap.put("follower_count", user.getFollowersCount()); theMap.put("friends_count", user.getFriendsCount()); theMap.put("lang", user.getLang()); theMap.put("utc_offset", user.getUtcOffset()); // resolution in seconds, -1 if not available? theMap.put("statuses_count", user.getStatusesCount()); } return new MapBasedInputRow(status.getCreatedAt().getTime(), dimensions, theMap); } @Override public Runnable commit() { // ephemera in, ephemera out. return doNothingRunnable; // reuse the same object each time } @Override public void close() throws IOException { log.info("CLOSE twitterstream"); twitterStream.shutdown(); // invokes twitterStream.cleanUp() } }; }
From source file:edu.csupomona.nlp.tool.crawler.Twitter.java
/** * Construct Twitter for crawling with Stream API * @throws IOException// w ww . ja v a 2 s. 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:essex.bigessexnew.TwitterStreamHandler.java
@Override public void run() { StatusListener sl = new StatusListener() { @Override/*from w w w. j a va2s. c o m*/ public void onStatus(Status status) { /*String s = TwitterObjectFactory.getRawJSON(status); System.out.println(s);*/ BasicDBObjectBuilder documentBuilder = BasicDBObjectBuilder.start(); documentBuilder.add("content", status.getText()); collection.insert(documentBuilder.get()); /*DBObject dbObject = (DBObject) JSON.parse(json); collection.insert(dbObject);*/ //System.out.println(json.toString()); } @Override public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { } @Override public void onTrackLimitationNotice(int numberOfLimitedStatuses) { } @Override public void onScrubGeo(long l, long l2) { } @Override public void onStallWarning(StallWarning stallWarning) { } @Override public void onException(Exception ex) { } }; TwitterStream twitterStream = new TwitterStreamFactory().getInstance(); twitterStream.addListener(sl); twitterStream.filter(Params.getProperty("hashtag")); }
From source file:example.justids.java
License:Apache License
/** * Usage: java twitter4j.examples.search.SearchTweets [query] * * @param args//from www. j a va 2s . c om */ public static void main(String[] args) { StatusListener listener = new StatusListener() { public Double count = 0d; Date started = new Date(); Date previous = new Date(); @Override public void onStatus(Status status) { try { File file = new File("Filtered_over1percent_lab_pc_obama_all.txt"); File file2 = new File("Filtered_over1percent_lab_pc_obama.txt"); // if file doesnt exists, then create it FileWriter fw = new FileWriter(file.getAbsoluteFile(), true); BufferedWriter bw = new BufferedWriter(fw); FileWriter fw2 = new FileWriter(file2.getAbsoluteFile(), true); BufferedWriter bw2 = new BufferedWriter(fw2); if (this.count % 1000 == 0) { Date finished10k = new Date(); System.out.println("\n\n\n\n AVERAGE RATE OF TWEETS is " + (this.count * 1000 / (finished10k.getTime() - this.started.getTime()))); System.out.println(1000000d / (finished10k.getTime() - this.previous.getTime())); System.out.println(this.count); System.out.println(finished10k.getTime() + " " + this.started.getTime() + " " + (finished10k.getTime() - this.started.getTime())); System.out.println(finished10k.getTime() + " " + this.previous.getTime() + " " + (finished10k.getTime() - this.previous.getTime())); System.out.println(status.getSource()); System.out.println("\n\n\n\n"); this.previous = finished10k; } this.count++; // System.out.println(status.getUser().getName() + " : " + status.getText()+" "+ this.count); bw.write(status.getId() + "\n"); bw.close(); if (status.getText().contains("obama")) { bw2.write(status.getId() + "\n"); bw2.close(); } } catch (IOException e) { e.printStackTrace(); } } @Override public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { // System.out.println(statusDeletionNotice.getUserId()+" has deleted this tweet"); } @Override public void onTrackLimitationNotice(int numberOfLimitedStatuses) { System.out.println(numberOfLimitedStatuses + " are missing from here"); } @Override public void onException(Exception ex) { ex.printStackTrace(); } @Override public void onScrubGeo(long arg0, long arg1) { // TODO Auto-generated method stub } @Override public void onStallWarning(StallWarning arg0) { // TODO Auto-generated method stub } }; TwitterStream twitterStream = new TwitterStreamFactory().getInstance(); RawStreamListener rawst = new RawStreamListener() { public Double count = 0d; public Double lengthsum = 0d; Date started = new Date(); Date previous = new Date(); @Override public void onMessage(String message) { if (!message.startsWith("{\"delete")) { count++; lengthsum += message.length(); if (count % 1000 == 0) { System.out.println(lengthsum / count); // lengthsum=0d; // count=0d; } } // TODO Auto-generated method stub } @Override public void onException(Exception arg0) { // TODO Auto-generated method stub } }; // twitterStream.addListener(rawst); String[] searchfor = { "language", "people", "problem", "microsoft", "epidemic", "obama", "zoo" }; FilterQuery query = new FilterQuery(); query.track(searchfor); twitterStream.addListener(listener); twitterStream.filter(query); // sample() method internally creates a thread which manipulates TwitterStream and calls these adequate listener methods continuously. // twitterStream.sample(); }
From source file:example.search.java
License:Apache License
/** * Usage: java twitter4j.examples.search.SearchTweets [query] * * @param args//from w w w. ja v a 2s . c om */ public static void main(String[] args) { StatusListener listener = new StatusListener() { public Double count = 0d; Date started = new Date(); Date previous = new Date(); @Override public void onStatus(Status status) { try { File file = new File("whythissucks.txt"); // if file doesnt exists, then create it FileWriter fw = new FileWriter(file.getAbsoluteFile(), true); BufferedWriter bw = new BufferedWriter(fw); if (this.count % 1000 == 0) { Date finished10k = new Date(); System.out.println("\n\n\n\n AVERAGE RATE OF TWEETS is " + (this.count * 1000 / (finished10k.getTime() - this.started.getTime()))); System.out.println(1000000d / (finished10k.getTime() - this.previous.getTime())); System.out.println(this.count); System.out.println(finished10k.getTime() + " " + this.started.getTime() + " " + (finished10k.getTime() - this.started.getTime())); System.out.println(finished10k.getTime() + " " + this.previous.getTime() + " " + (finished10k.getTime() - this.previous.getTime())); System.out.println(status.getSource()); System.out.println("\n\n\n\n"); this.previous = finished10k; } this.count++; // System.out.println(status.getUser().getName() + " : " + status.getText()+" "+ this.count); bw.write(status.getId() + "|" + status.getText() + "\n"); bw.close(); } catch (IOException e) { e.printStackTrace(); } } @Override public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { // System.out.println(statusDeletionNotice.getUserId()+" has deleted this tweet"); } @Override public void onTrackLimitationNotice(int numberOfLimitedStatuses) { System.out.println("limited " + numberOfLimitedStatuses); } @Override public void onException(Exception ex) { ex.printStackTrace(); } @Override public void onScrubGeo(long arg0, long arg1) { // TODO Auto-generated method stub } @Override public void onStallWarning(StallWarning arg0) { // TODO Auto-generated method stub System.out.println(arg0.getMessage()); } }; TwitterInstanceCreator tic = new TwitterInstanceCreator(); RawStreamListener rawst = new RawStreamListener() { public Double count = 0d; public Double lengthsum = 0d; Date started = new Date(); Date previous = new Date(); String filename; String previousfilename = ""; @Override public void onMessage(String message) { if (!message.startsWith("{\"delete")) { try { // if file doesnt exists, then create it if (this.count % 1000 == 0) { Date finished10k = new Date(); System.out.println("\n\n\n\n AVERAGE RATE OF TWEETS is " + (this.count * 1000 / (finished10k.getTime() - this.started.getTime()))); System.out.println(1000000d / (finished10k.getTime() - this.previous.getTime())); System.out.println(this.count); System.out.println(finished10k.getTime() + " " + this.started.getTime() + " " + (finished10k.getTime() - this.started.getTime())); System.out.println(finished10k.getTime() + " " + this.previous.getTime() + " " + (finished10k.getTime() - this.previous.getTime())); System.out.println("\n\n\n\n"); this.previous = finished10k; } File file = new File("tweetstoimport/" + this.filename); FileWriter fw = new FileWriter(file.getAbsoluteFile(), true); BufferedWriter bw = new BufferedWriter(fw); this.count++; // System.out.println(status.getUser().getName() + " : " + status.getText()+" "+ this.count); bw.write(message + "\n"); bw.close(); fw.close(); } catch (IOException e) { e.printStackTrace(); } } // TODO Auto-generated method stub } @Override public void onException(Exception arg0) { // TODO Auto-generated method stub } }; // twitterStream.addListener(rawst); // String[] searchfor={"flu", "influenza", "fever", "cough", "sore", "throat", "sore throat", "headache"}; // FilterQuery query=new FilterQuery(); // query.track(searchfor); TwitterStream twitterStream = tic.getStream(1); twitterStream.addListener(listener); // twitterStream.filter(query); // sample() method internally creates a thread which manipulates TwitterStream and calls these adequate listener methods continuously. twitterStream.sample(); //Filters from the stream //twitterStream.filter(query); }
From source file:flight_ranker.Flight_colllector.java
public static void main(String[] args) throws FileNotFoundException, IOException { ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true);/*from w w w.j a v a2 s . co 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:foo.bar.twitter.sample01.Sample01Activity.java
License:Apache License
/** * start button clicked// w w w . ja va 2 s .com * @param v */ public void onClickStartButton(View v) { Log.d("DEBUG", "Start button clicked!"); if (!isAuthorized) { Log.d("DEBUG", "not authorized"); return; } bt_start.setEnabled(false); //start button disabled bt_cancel.setEnabled(true); //cancel button enabled if (isAuthorized) { twitterStream = new TwitterStreamFactory().getInstance(); twitterStream.setOAuthConsumer(ConstantValue.CONSUMER_KEY, ConstantValue.CONSUMER_SECRET); twitterStream.setOAuthAccessToken(new AccessToken(token, tokenSecret)); StatusListener listener = new StatusListener() { @Override public void onException(Exception arg0) { ;//no process } @Override public void onDeletionNotice(StatusDeletionNotice arg0) { ;//no process } @Override public void onScrubGeo(long arg0, long arg1) { ;//no process } @Override public void onStatus(Status status) { String tweet = status.getText(); Log.d("DEBUG", "tweet: " + tweet); } @Override public void onTrackLimitationNotice(int arg0) { ;//no process } }; twitterStream.addListener(listener); String[] trackArray = { ConstantValue.HASH_TAG }; twitterStream.filter(new FilterQuery(0, null, trackArray)); } }
From source file:gh.polyu.user.TrackUsers.java
License:Apache License
public void track(final int no, final int p) { final TwitterDBHandle handle = new TwitterDBHandle(); handle.intialTwitterDBhandle();/*from w w w . ja va 2 s .co m*/ while (alive) { alive = false; StatusListener listener = new StatusListener() { ArrayList<_TweetLink> listlink = new ArrayList<_TweetLink>(); int cnt = 0; String oldmonth = "20138"; String table = "UserTweet20138"; String oldday = ""; String currentday = ""; String currentmonth = ""; long lastinsert = 0l; long nowinsert = 0l; int newday = 0; String newtime = ""; @Override public void onStatus(Status status) { if (status.getId() == 123 && status.getText().equals("YOU are WORNG!.")) { System.out.println("Connection Need to be rebuilt!!"); alive = true; } else if (status.getLang().equals("en")) { _TweetLink tweet = new _TweetLink(); String Test = status.getText(); tweet.setText(Test); Date time = status.getCreatedAt(); tweet.setTime(time); tweet.setUserName(status.getUser().getName()); HashtagEntity[] hashtagentity = status.getHashtagEntities(); StringBuffer hashen = new StringBuffer(); for (int i = 0; i < hashtagentity.length; i++) { hashen.append(hashtagentity[i].getText()); hashen.append(";"); } tweet.setHashtag(hashen.toString()); URLEntity[] URLEn = status.getURLEntities(); StringBuffer URL = new StringBuffer(); for (int i = 0; i < URLEn.length; i++) { URL.append(URLEn[i].getURL()); URL.append(";"); } tweet.setURL(URL.toString()); //user mention UserMentionEntity[] userEn = status.getUserMentionEntities(); StringBuffer mentuser = new StringBuffer(); for (int i = 0; i < userEn.length; i++) { mentuser.append(userEn[i].getId()); mentuser.append(";"); } tweet.setUerMention(mentuser.toString()); //if(mentuser.length()!=0); //System.out.println("mentuser: "+ mentuser); //tweetID tweet.setTweetID(status.getId()); //if(ID!=null) // original twitterID tweet.setOriginID(status.getInReplyToStatusId()); //original user ID tweet.setOriginUser(status.getInReplyToUserId()); // user ID User users = status.getUser(); tweet.setTweetUser(users.getId()); //places Place Pl = status.getPlace(); String place = ""; if (Pl != null) { place = Pl.getFullName(); //System.out.println("place "+place); } tweet.setPlace(place); // Retweetcoun long num = 0; if (status.getRetweetedStatus() != null) { num = status.getRetweetedStatus().getRetweetCount(); //System.out.println("retweetcount"+num); tweet.setRetweetCount(num); tweet.setRetweet(1); } else { tweet.setRetweetCount(0); tweet.setRetweet(0); } // if(Retweet!=null) //System.out.println("Retweetcount: "+ Retweet); //isfavourate boolean favourate = status.isFavorited(); /*if(favourate) { fav = 1; tweet.setFavourate(fav); fav =0; System.out.println("isf "+ fav); }*/ // is retweet //String other = status.toString(); // tweet.setOther(other); listlink.add(tweet); Calendar cal = Calendar.getInstance(); int year = cal.get(Calendar.YEAR); int month = cal.get(Calendar.MONTH) + 1; int day = cal.get(Calendar.DAY_OF_MONTH); currentmonth = String.valueOf(year) + String.valueOf(month); currentday = String.valueOf(day); if (currentmonth.equals(oldmonth)) { if (currentday.equals(oldday)) ; else { newday = 1; SimpleDateFormat formatter = new SimpleDateFormat("MMddHH:mm:ss "); Date curDate = new Date(System.currentTimeMillis());// newtime = formatter.format(curDate); } } else { try { handle.database_connection(); table = "UserTweet" + String.valueOf(year) + String.valueOf(month); System.out.println("create new table " + table); String CREATE_TABLE = "create table " + table + "(TweetID varchar(100), UserName varchar(200), TwitterUser varchar(145), OriginID varchar(100), OriginUser varchar(100), place varchar(100), RetweetCount varchar(100), isRetweet int(5), Text varchar(500), Time datetime," + "Hashtag varchar(200), URL varchar(200), UerMention varchar(200))"; Statement st = handle.conn.createStatement(); st.execute(CREATE_TABLE); String Create_Index = "alter table " + table + " add index time (Time)"; st.execute(Create_Index); String Create_Index2 = "alter table " + table + " add index userID (TwitterUser)"; st.execute(Create_Index2); String key = "ALTER TABLE " + table + " ADD PRIMARY KEY (TweetID)"; st.execute(key); } catch (SQLException e) { e.printStackTrace(); } handle.close_databasehandle(); try { TwitterDBHandle handle2 = new TwitterDBHandle(); handle2.intialTwitterDBhandle2(); handle2.database_connection(); table = "UserTweet" + String.valueOf(year) + String.valueOf(month); System.out.println("create new table " + table); String CREATE_TABLE = "create table " + table + "(TweetID varchar(100), UserName varchar(200), TwitterUser varchar(145), OriginID varchar(100), OriginUser varchar(100), place varchar(100), RetweetCount varchar(100), isRetweet int(5), Text varchar(500), Time datetime," + "Hashtag varchar(200), URL varchar(200), UerMention varchar(200))"; Statement st = handle2.conn.createStatement(); st.execute(CREATE_TABLE); String Create_Index = "alter table " + table + " add index time (Time)"; st.execute(Create_Index); String Create_Index2 = "alter table " + table + " add index userID (TwitterUser)"; st.execute(Create_Index2); String key = "ALTER TABLE " + table + " ADD PRIMARY KEY (TweetID)"; st.execute(key); handle2.close_databasehandle(); } catch (SQLException e) { e.printStackTrace(); } } //System.out.println("OTHER: "+ other); if ((cnt++) % 1000 == 0) { if (newday == 1) { newday = 0; oldday = currentday; GmailSend gs = new GmailSend("cscchenyoyo@gmail.com", "910316ccy"); gs.send("THREAD" + p + " :" + "program no" + no + "message" + newtime, "I am still alive"); newtime = ""; } try { handle.database_connection(); handle.userTweet(table, listlink); nowinsert = System.currentTimeMillis(); System.err.println( "No: " + no + "program " + "totally " + cnt + " tweets downloaded!\n" + new Date(nowinsert) + " " + new Date(lastinsert)); lastinsert = nowinsert; nowinsert = 0l; handle.close_databasehandle(); } catch (SQLException e) { handle.close_databasehandle(); e.printStackTrace(); // TODO Auto-generated catch block TwitterDBHandle handle2 = new TwitterDBHandle(); handle2.intialTwitterDBhandle2(); handle2.database_connection(); try { handle2.userTweet(table, listlink); nowinsert = System.currentTimeMillis(); System.err.println("New Database No: " + no + "program " + "totally " + cnt + " tweets downloaded!\n" + new Date(nowinsert) + new Date(lastinsert)); lastinsert = nowinsert; nowinsert = 0l; handle2.close_databasehandle(); } catch (SQLException e1) { // TODO Auto-generated catch block GmailSend gs = new GmailSend("cscchenyoyo@gmail.com", "910316ccy"); try { gs.SendSSLMessage("cscchenyoyo@gmail.com", "program error", "both databases are down"); } catch (MessagingException ee) { // TODO Auto-generated catch block e.printStackTrace(); } } GmailSend gs = new GmailSend("cscchenyoyo@gmail.com", "910316ccy"); try { gs.SendSSLMessage("cscchenyoyo@gmail.com", "program error", "change database to another one"); } catch (MessagingException ee) { // TODO Auto-generated catch block e.printStackTrace(); } } listlink.clear(); } } } @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 twitterStream = new TwitterStreamFactory().getInstance(); twitterOAuth twtOauth = new twitterOAuth(); twtOauth.AuthoritywithS(twitterStream, key); twitterStream.addListener(listener); twitterStream.filter(new FilterQuery(0, follow)); /* try { Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }*/ } }
From source file:gov.nasa.jpl.memex.elwha.impl.ElwhaResource.java
License:Apache License
/** * Main entry of this application.// www.j a v a 2 s .c om * * @param userIds follow(comma separated user ids) track(comma separated filter terms) * @throws TwitterException when Twitter service or network is unavailable * @return */ @Override public Elwha get(String userIds) { //return new Elwha().setMessage("Hello, Rest.li!"); // } //if (userIds.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(); } }; TwitterStream twitterStream = new TwitterStreamFactory().getInstance(); twitterStream.addListener(listener); ArrayList<Long> follow = new ArrayList<Long>(); ArrayList<String> track = new ArrayList<String>(); if (isNumericalArgument(userIds)) { for (String id : userIds.split(",")) { follow.add(Long.parseLong(id)); } } else { track.addAll(Arrays.asList(userIds.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)); return new Elwha().setUserIds("Mock stub!"); }