List of usage examples for twitter4j Status getCreatedAt
Date getCreatedAt();
From source file:org.smarttechie.servlet.SimpleStream.java
public void getStream(TwitterStream twitterStream, String[] parametros, final Session session)//,PrintStream out) { listener = new StatusListener() { @Override//from w w w. j av a 2s . c om 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) { Twitter twitter = new TwitterFactory().getInstance(); User user = status.getUser(); // gets Username String username = status.getUser().getScreenName(); System.out.println(""); String profileLocation = user.getLocation(); System.out.println(profileLocation); long tweetId = status.getId(); System.out.println(tweetId); String content = status.getText(); System.out.println(content + "\n"); JSONObject obj = new JSONObject(); obj.put("User", status.getUser().getScreenName()); obj.put("ProfileLocation", user.getLocation().replaceAll("'", "''")); obj.put("Id", status.getId()); obj.put("UserId", status.getUser().getId()); //obj.put("User", status.getUser()); obj.put("Message", status.getText().replaceAll("'", "''")); obj.put("CreatedAt", status.getCreatedAt().toString()); obj.put("CurrentUserRetweetId", status.getCurrentUserRetweetId()); //Get user retweeteed String otheruser; try { if (status.getCurrentUserRetweetId() != -1) { User user2 = twitter.showUser(status.getCurrentUserRetweetId()); otheruser = user2.getScreenName(); System.out.println("Other user: " + otheruser); } } catch (Exception ex) { System.out.println("ERROR: " + ex.getMessage().toString()); } obj.put("IsRetweet", status.isRetweet()); obj.put("IsRetweeted", status.isRetweeted()); obj.put("IsFavorited", status.isFavorited()); obj.put("InReplyToUserId", status.getInReplyToUserId()); //In reply to obj.put("InReplyToScreenName", status.getInReplyToScreenName()); obj.put("RetweetCount", status.getRetweetCount()); if (status.getGeoLocation() != null) { obj.put("GeoLocationLatitude", status.getGeoLocation().getLatitude()); obj.put("GeoLocationLongitude", status.getGeoLocation().getLongitude()); } JSONArray listHashtags = new JSONArray(); String hashtags = ""; for (HashtagEntity entity : status.getHashtagEntities()) { listHashtags.add(entity.getText()); hashtags += entity.getText() + ","; } if (!hashtags.isEmpty()) obj.put("HashtagEntities", hashtags.substring(0, hashtags.length() - 1)); if (status.getPlace() != null) { obj.put("PlaceCountry", status.getPlace().getCountry()); obj.put("PlaceFullName", status.getPlace().getFullName()); } obj.put("Source", status.getSource()); obj.put("IsPossiblySensitive", status.isPossiblySensitive()); obj.put("IsTruncated", status.isTruncated()); if (status.getScopes() != null) { JSONArray listScopes = new JSONArray(); String scopes = ""; for (String scope : status.getScopes().getPlaceIds()) { listScopes.add(scope); scopes += scope + ","; } if (!scopes.isEmpty()) obj.put("Scopes", scopes.substring(0, scopes.length() - 1)); } obj.put("QuotedStatusId", status.getQuotedStatusId()); JSONArray list = new JSONArray(); String contributors = ""; for (long id : status.getContributors()) { list.add(id); contributors += id + ","; } if (!contributors.isEmpty()) obj.put("Contributors", contributors.substring(0, contributors.length() - 1)); System.out.println("" + obj.toJSONString()); insertNodeNeo4j(obj); //out.println(obj.toJSONString()); String statement = "INSERT INTO TweetsClassification JSON '" + obj.toJSONString() + "';"; executeQuery(session, statement); } @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(); fq.track(parametros); twitterStream.addListener(listener); twitterStream.filter(fq); }
From source file:org.sociotech.communitymashup.source.twitter.TwitterSourceService.java
License:Open Source License
/** * Creates a content for the given tweet, adds it to the data set and sets * the author.//from w w w . java2 s . c o m * * @param author * Person corresponding to the twitter user which authored the * tweet * @param tweet * The tweet * @return The Content created from the tweet, null in error case. */ private Content createContentFromTweet(Person author, Status tweet) { if (tweet == null) { return null; } String tweetText = tweet.getText(); if (tweetText == null || tweetText.isEmpty()) { return null; } String ident = tweet.getId() + ""; if (this.getContentWithSourceIdent(ident) != null) { // status already created return null; } Content tweetContent = factory.createContent(); tweetContent.setStringValue(tweetText); tweetContent.setName(createTitleFromTwitterText(tweetText)); tweetContent = (Content) this.add(tweetContent, ident); if (tweetContent == null) { return null; } tweetContent.metaTag(TwitterTags.TWITTER); tweetContent.setCreated(tweet.getCreatedAt()); if (author != null) { tweetContent.setAuthor(author); } // and tag the status HashtagEntity[] hashtags = tweet.getHashtagEntities(); tagIOwithHashtags(tweetContent, hashtags); UserMentionEntity[] mentionedUsers = tweet.getUserMentionEntities(); if (mentionedUsers != null && mentionedUsers.length > 0 && source.isPropertyTrue(TwitterProperties.ADD_MENTIONED_PEOPLE_PROPERTY)) { for (int i = 0; i < mentionedUsers.length; i++) { Person mentionedPerson = getPersonForTwitterUserId(mentionedUsers[i].getId()); if (mentionedPerson == null) { continue; } tweetContent.addContributor(mentionedPerson); } } URLEntity[] urlEntities = tweet.getURLEntities(); if (urlEntities != null && urlEntities.length > 0 && source.isPropertyTrue(TwitterProperties.ADD_URL_ENTITIES_PROPERTY)) { for (int i = 0; i < urlEntities.length; i++) { String url = urlEntities[i].getURL(); if (url != null) { // attach url as website tweetContent.addWebSite(url); } } } // no more available // String language = tweet.getIsoLanguageCode(); // if(language != null && !language.isEmpty()) // { // // set in content // tweetContent.setLocale(language); // // set as meta tag // tweetContent.metaTag(language); // } // TODO check media entities // MediaEntity[] mediaEntities = twitterStatus.getMediaEntities(); // add location GeoLocation tweetLocation = tweet.getGeoLocation(); Place place = tweet.getPlace(); if (tweetLocation != null || place != null) { Location location = factory.createLocation(); if (place != null) { location.setStreet(place.getStreetAddress()); location.setCountry(place.getCountry()); location.setStringValue(place.getFullName()); } if (tweetLocation != null) { location.setLatitude(tweetLocation.getLatitude() + ""); location.setLongitude(tweetLocation.getLongitude() + ""); } location = (Location) this.add(location, "tloc_" + tweet.getId()); if (location != null) { location.metaTag(TwitterTags.TWITTER); tweetContent.extend(location); if (place != null) { location.metaTag(place.getCountryCode()); location.metaTag(place.getPlaceType()); } } } return tweetContent; }
From source file:org.talend.spark.utils.twitter.TwitterUtil.java
License:Open Source License
public static Object parse(TwitterParameter parameter, Status status) { if (parameter == TwitterParameter.USERNAME) { return status.getUser().getName(); } else if (parameter == TwitterParameter.TEXT) { return status.getText(); } else if (parameter == TwitterParameter.SOURCE) { return status.getSource(); } else if (parameter == TwitterParameter.ACCESSLEVEL) { return status.getAccessLevel(); } else if (parameter == TwitterParameter.DATE) { return status.getCreatedAt(); } else if (parameter == TwitterParameter.ID) { return status.getId(); } else if (parameter == TwitterParameter.GEOLOCATION_LATITUDE) { return status.getGeoLocation().getLatitude(); } else if (parameter == TwitterParameter.GEOLOCATION_LONGITUDE) { return status.getGeoLocation().getLongitude(); } else if (parameter == TwitterParameter.HASHTAG) { String hashTags = ""; HashtagEntity[] hashTagArray = status.getHashtagEntities(); for (int i = 0; i < hashTagArray.length; i++) { if (hashTags.equals("")) { hashTags = hashTagArray[i].getText(); } else { hashTags = hashTags + "," + hashTagArray[i].getText(); }// w w w. j a va 2s . c o m } return hashTags; } return null; }
From source file:org.todoke.countstream.Callback.java
License:Apache License
public void increment(Status status) { logger.info("got: " + status.toString()); increment(status.getText(), status.getCreatedAt()); }
From source file:org.tweetalib.android.model.TwitterStatus.java
License:Apache License
public TwitterStatus(Status status) { User statusUser = status.getUser();/*from w w w.j a v a 2 s .c om*/ mCreatedAt = status.getCreatedAt(); mId = status.getId(); if (status.getInReplyToStatusId() != -1) { mInReplyToStatusId = status.getInReplyToStatusId(); } if (status.getInReplyToUserId() != -1) { mInReplyToUserId = status.getInReplyToUserId(); } mInReplyToUserScreenName = status.getInReplyToScreenName(); mIsFavorited = status.isFavorited(); mIsRetweet = status.isRetweet(); mIsRetweetedByMe = status.isRetweetedByMe(); mSource = TwitterUtil.stripMarkup(status.getSource()); if (statusUser != null) { mUserId = statusUser.getId(); mUserName = statusUser.getName(); mUserScreenName = statusUser.getScreenName(); } mMediaEntity = TwitterMediaEntity.createMediaEntity(status); boolean useDefaultAuthor = true; if (mIsRetweet) { if (status.getRetweetedStatus() != null && status.getRetweetedStatus().getUser() != null) { SetProfileImagesFromUser(new TwitterUser(status.getRetweetedStatus().getUser())); } mOriginalRetweetId = status.getRetweetedStatus().getId(); // You'd think this check wasn't necessary, but apparently not... UserMentionEntity[] userMentions = status.getUserMentionEntities(); if (userMentions != null && userMentions.length > 0) { useDefaultAuthor = false; UserMentionEntity authorMentionEntity = status.getUserMentionEntities()[0]; mAuthorId = authorMentionEntity.getId(); mAuthorName = authorMentionEntity.getName(); mAuthorScreenName = authorMentionEntity.getScreenName(); Status retweetedStatus = status.getRetweetedStatus(); mStatus = retweetedStatus.getText(); setStatusMarkup(retweetedStatus); mRetweetCount = retweetedStatus.getRetweetCount(); mUserMentions = TwitterUtil.getUserMentions(retweetedStatus.getUserMentionEntities()); mIsRetweetedByMe = retweetedStatus.isRetweetedByMe(); } } else { if (statusUser != null) { SetProfileImagesFromUser(new TwitterUser(statusUser)); } } if (useDefaultAuthor) { if (statusUser != null) { mAuthorId = statusUser.getId(); } mStatus = status.getText(); setStatusMarkup(status); mRetweetCount = status.getRetweetCount(); mUserMentions = TwitterUtil.getUserMentions(status.getUserMentionEntities()); } /* * if (status.getId() == 171546910249852928L) { mStatus = * "<a href=\"http://a.com\">@chrismlacy</a> You've been working on Tweet Lanes for ages. Is it done yet?" * ; mStatusMarkup = * "<a href=\"http://a.com\">@chrismlacy</a> You've been working on Tweet Lanes for ages. Is it done yet?" * ; mAuthorScreenName = "emmarclarke"; mStatusMarkup = mStatus; } else * if (status.getId() == 171444098698457089L) { mStatus = * "<a href=\"http://a.com\">@chrismlacy</a> How's that app of yours coming along?" * ; mStatusMarkup = * "<a href=\"http://a.com\">@chrismlacy</a> How's that app of yours coming along?" * ; mStatusMarkup = mStatus; } */ }
From source file:org.unimonk.flume.source.twitter.TwitterSource.java
License:Apache License
@Override public void start() { final ChannelProcessor channel = getChannelProcessor(); StatusListener listener = new StatusListener() { @Override//from www . j av a 2 s . c o m public void onStatus(Status status) { Tweet tweet = new Tweet(); tweet.setId(status.getId()); tweet.setUserId(status.getUser().getId()); tweet.setLatitude(status.getGeoLocation().getLatitude()); tweet.setLongitude(status.getGeoLocation().getLongitude()); tweet.setText(status.getText()); tweet.setCreatedAt(new Timestamp(status.getCreatedAt().getTime())); Event event = EventBuilder.withBody(tweet.toString(), Charsets.UTF_8); sourceCounter.incrementAppendReceivedCount(); try { channel.processEvent(event); sourceCounter.incrementEventAcceptedCount(); } catch (ChannelException ex) { logger.error("In Twitter source {} : Unable to process event due to exception {}.", getName(), ex); } } // This listener will ignore everything except for new tweets @Override public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { } @Override public void onTrackLimitationNotice(int numberOfLimitedStatuses) { } @Override public void onScrubGeo(long userId, long upToStatusId) { } @Override public void onException(Exception ex) { } @Override public void onStallWarning(StallWarning arg0) { } }; logger.debug("Setting up Twitter sample stream using consumer key {} and" + " access token {}", new String[] { consumerKey, accessToken }); twitterStream = new TwitterStreamFactory().getInstance(); twitterStream.addListener(listener); twitterStream.setOAuthConsumer(consumerKey, consumerSecret); AccessToken token = new AccessToken(accessToken, accessTokenSecret); twitterStream.setOAuthAccessToken(token); if (keywords.length == 0) { logger.debug("Starting up Twitter sampling..."); twitterStream.sample(); } else { logger.debug("Starting up Twitter filtering..."); FilterQuery query = new FilterQuery().track(keywords); twitterStream.filter(query); } this.sourceCounter.start(); super.start(); }
From source file:org.vsepml.storm.sensapp.SensAppBolt.java
License:Apache License
@Override public void execute(Tuple tuple) { Status tweet = (Status) tuple.getValueByField("tweet"); LinkedList<ValueJsonModel> l = new LinkedList<ValueJsonModel>(); l.add(new StringValueJsonModel(tweet.getText(), tweet.getCreatedAt().getTime())); StringMeasureJsonModel m = new StringMeasureJsonModel(name, tweet.getCreatedAt().getTime(), "m", l); try {/*w w w. ja v a2s .c o m*/ String jsonString = mapper.writeValueAsString(m); RestRequest.putData(new URI(endPoint), jsonString); } catch (IOException e) { e.printStackTrace(); } catch (URISyntaxException e) { e.printStackTrace(); } }
From source file:org.wandora.application.tools.extractors.twitter.AbstractTwitterExtractor.java
License:Open Source License
public Topic reifyTweet(Status t, TopicMap tm) { Topic tweetTopic = null;//from w w w . j a v a 2 s . co m try { long tId = t.getId(); String msg = t.getText(); User user = t.getUser(); if (user == null) { tweetTopic = reifyTweet(tId, null, msg, tm); } else { String userScreenName = user.getScreenName(); tweetTopic = reifyTweet(tId, userScreenName, msg, tm); Topic userTopic = reifyTwitterUser(user, tm); if (tweetTopic != null && userTopic != null) { Association a = tm.createAssociation(getTwitterFromUserType(tm)); a.addPlayer(tweetTopic, getTweetType(tm)); a.addPlayer(userTopic, getTwitterUserType(tm)); } } /* String toUser = t.getToUser(); if(toUser != null) { long toUid = t.getToUserId(); Topic toUserTopic = reifyTwitterUser(toUser, toUid, tm); if(tweetTopic != null && toUserTopic != null) { Association a = tm.createAssociation(getTwitterToUserType(tm)); a.addPlayer(tweetTopic, getTweetType(tm)); a.addPlayer(toUserTopic, getTwitterUserType(tm)); } } */ Date d = t.getCreatedAt(); if (tweetTopic != null && d != null) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateStr = df.format(d); tweetTopic.setData(getTweetDateType(tm), TMBox.getLangTopic(tweetTopic, DEFAULT_LANG), dateStr); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); dateStr = sdf.format(d); Topic dateTopic = ExtractHelper.getOrCreateTopic(DATE_SI_BODY + dateStr, dateStr, getTweetDateType(tm), tm); if (dateTopic != null) { Association a = tm.createAssociation(getTweetDateType(tm)); a.addPlayer(tweetTopic, getTweetType(tm)); a.addPlayer(dateTopic, getTweetDateType(tm)); } } /* String l = t.getIsoLanguageCode(); if(l != null) { Topic tweetLangTopic = TMBox.getLangTopic(tweetTopic, l); if(tweetLangTopic != null) { Association a = tm.createAssociation(getTweetLangType(tm)); a.addPlayer(tweetTopic, getTweetType(tm)); a.addPlayer(tweetLangTopic, getTweetLangType(tm)); } } */ GeoLocation geo = t.getGeoLocation(); if (geo != null) { double lat = geo.getLatitude(); double lon = geo.getLongitude(); String geoLocStr = lat + "," + lon; tweetTopic.setData(getTweetGeoLocationType(tm), TMBox.getLangTopic(tweetTopic, DEFAULT_LANG), geoLocStr); } HashtagEntity[] entities = t.getHashtagEntities(); if (entities != null && entities.length > 0) { for (int i = 0; i < entities.length; i++) { Topic entityTopic = reifyHashtagEntity(entities[i], tm); if (entityTopic != null) { Association a = tm.createAssociation(getHashtagType(tm)); a.addPlayer(tweetTopic, getTweetType(tm)); a.addPlayer(entityTopic, getHashtagType(tm)); } } } MediaEntity[] mediaEntities = t.getMediaEntities(); if (mediaEntities != null && mediaEntities.length > 0) { for (int i = 0; i < mediaEntities.length; i++) { Topic entityTopic = reifyMediaEntity(mediaEntities[i], tm); if (entityTopic != null) { Association a = tm.createAssociation(getMediaEntityType(tm)); a.addPlayer(tweetTopic, getTweetType(tm)); a.addPlayer(entityTopic, getMediaEntityType(tm)); } } } URLEntity[] urlEntities = t.getURLEntities(); if (urlEntities != null && urlEntities.length > 0) { for (int i = 0; i < urlEntities.length; i++) { Topic entityTopic = reifyUrlEntity(urlEntities[i], tm); if (entityTopic != null) { Association a = tm.createAssociation(getURLEntityType(tm)); a.addPlayer(tweetTopic, getTweetType(tm)); a.addPlayer(entityTopic, getURLEntityType(tm)); } } } } catch (Exception e) { log(e); } return tweetTopic; }
From source file:org.wso2.cep.uima.demo.TweetExtractor.java
License:Open Source License
/*** * *//*from w ww.ja va2 s .com*/ private void retrieveTweets(int maxTweets) { Paging paging; // set the lowest value of the tweet ID initially to one less than Long.MAX_VALUE long min_id = Long.MAX_VALUE - 1; int count = 0; int index = 0; boolean maxValueReached = false; logger.info("Started Extracting Tweets of user: " + userToSearch); // iterate through the timeline untill the iteration returns no tweets while (true) { try { //count = tweetList.size(); // paging tweets at a rate of 100 per page paging = new Paging(1, 100); // if this is not the first iteration set the new min_id value for the page if (count != 0) { logger.info("Extracted Tweet Count : " + count); paging.setMaxId(min_id - 1); } // get a page of the tweet timeline with tweets with ids less than the min_id value List<Status> tweetTempList = twitterApp.getUserTimeline(userToSearch, paging); // iterate the results and add to tweetList for (Status s : tweetTempList) { if (count == maxTweets) { maxValueReached = true; break; } count++; Tweet tweet = new Tweet(s.getId(), s.getCreatedAt(), s.getText()); tweetList.add(tweet); logger.debug(" " + (index++) + " " + tweet.toString()); // set the value for the min value for the next iteration if (s.getId() < min_id) { min_id = s.getId(); } } // if the results for this iteration is zero, means we have reached the API limit or we have extracted the maximum // possible, so break if (tweetTempList.size() == 0 || maxValueReached) { break; } } catch (TwitterException e) { e.printStackTrace(); break; } catch (Exception e) { e.printStackTrace(); break; } } }
From source file:org.wso2.fasttrack.project.roadlk.rest.DatasetGenerator.java
License:Open Source License
/** * @param consumerKey/*from www.j a va2s.co m*/ * Twitter Consumer Key (API Key) * @param consumerSecret * Twitter Consumer Secret (API Secret) * @param accessToken * Twitter Access Token * @param accessTokenSecret * Twitter Access Token Secret * @param pageLimit * Maximum pages to be retrieved * @throws IOException * @throws TwitterException */ @SuppressWarnings("resource") public void generateDataset(String consumerKey, String consumerSecret, String accessToken, String accessTokenSecret, int pageLimit) throws IOException, TwitterException { // Twitter object of Twitter4J library Twitter twitter = TwitterFactory.getSingleton(); // Twitter API authentication twitter.setOAuthConsumer(consumerKey, consumerSecret); twitter.setOAuthAccessToken(new AccessToken(accessToken, accessTokenSecret)); PrintWriter printWriter = null; try { printWriter = new PrintWriter(new BufferedWriter(new FileWriter("dataset.txt", true))); } catch (IOException e) { throw new IOException(e); } LOGGER.debug("Twitter feed extraction started."); for (int i = 1; i < pageLimit; i = i + 1) { Paging paging = new Paging(i, 100); ResponseList<Status> statuses = null; try { statuses = twitter.getUserTimeline("road_lk", paging); } catch (TwitterException e) { //LOGGER.error("TwitterException occurred." + e.getMessage()); throw new TwitterException(e); } for (Status status : statuses) { printWriter.println(status.getCreatedAt() + ": " + status.getText()); } } printWriter.close(); LOGGER.debug("Twitter feed extraction completed."); }