List of usage examples for twitter4j Paging Paging
public Paging(int page, long sinceId)
From source file:org.bireme.interop.toJson.TweetLoader.java
License:Open Source License
private void loadUserIdTweets() throws TwitterException { final Paging paging = new Paging(++pageNum, MAX_PAGE_SIZE); final ResponseList<Status> respLst = twitter.getUserTimeline(userId, paging); if ((lowerDate == null) || respLst.get(respLst.size() - 1).getCreatedAt().after(lowerDate)) { tweetIterator = respLst.iterator(); } else {/*from www . ja v a 2s . com*/ tweetIterator = null; } retweetIterator = null; }
From source file:org.botlibre.sense.twitter.Twitter.java
License:Open Source License
/** * Check status.//from w ww .j a v a 2 s. co m */ public void checkStatus() { if (!getProcessStatus()) { return; } log("Checking status", Level.FINE); try { Network memory = getBot().memory().newMemory(); Vertex twitter = memory.createVertex(getPrimitive()); Vertex vertex = twitter.getRelationship(Primitive.LASTTIMELINE); long last = 0; if (vertex != null) { last = ((Number) vertex.getData()).longValue(); } long max = 0; ResponseList<Status> timeline = null; boolean more = true; int page = 1; int count = 0; this.errors = 0; while (more && (count <= this.maxStatus) && page <= this.maxPage) { if (last == 0) { timeline = getConnection().getHomeTimeline(); more = false; } else { Paging paging = new Paging(page, last); timeline = getConnection().getHomeTimeline(paging); if ((timeline == null) || (timeline.size() < 20)) { more = false; } page++; } if ((timeline == null) || timeline.isEmpty()) { break; } log("Processing status", Level.INFO, timeline.size()); for (int index = timeline.size() - 1; index >= 0; index--) { if (count >= this.maxStatus) { break; } if (this.errors > this.maxErrors) { break; } Status status = timeline.get(index); String name = status.getUser().getScreenName(); if (!name.equals(this.userName)) { long statusTime = status.getCreatedAt().getTime(); long statusId = status.getId(); if (statusId > max) { max = statusId; } if ((System.currentTimeMillis() - statusTime) > DAY) { log("Day old status", Level.INFO, statusId, statusTime); more = false; continue; } if (statusId > last) { if (Utils.checkProfanity(status.getText())) { continue; } boolean match = false; List<String> statusWords = new TextStream(status.getText().toLowerCase()).allWords(); if (getListenStatus()) { this.languageState = LanguageState.Listening; match = true; } else { for (String text : getStatusKeywords()) { List<String> keywords = new TextStream(text.toLowerCase()).allWords(); if (!keywords.isEmpty() && statusWords.containsAll(keywords)) { match = true; break; } } } if (getLearn()) { learnTweet(status, true, true, memory); } if (match) { count++; input(status); Utils.sleep(500); } else { log("Skipping status, missing keywords", Level.FINE, status.getText()); if (!status.isRetweet() && !status.getUser().isProtected() && !status.isRetweetedByMe()) { boolean retweeted = false; // Check retweet. for (String keywords : getRetweet()) { List<String> keyWords = new TextStream(keywords.toLowerCase()).allWords(); if (!keyWords.isEmpty()) { if (statusWords.containsAll(keyWords)) { retweeted = true; count++; retweet(status); Utils.sleep(500); break; } } } if (!retweeted) { log("Skipping rewteet, missing keywords", Level.FINE, status.getText()); } } else if (!getRetweet().isEmpty()) { if (status.isRetweet()) { log("Skipping rewteet", Level.FINE, status.getText()); } else if (status.getUser().isProtected()) { log("Skipping protected user", Level.FINE, status.getText()); } else if (status.isRetweetedByMe()) { log("Skipping already retweeted", Level.FINE, status.getText()); } } } } else { log("Old status", Level.INFO, statusId, statusTime); } } } } if (max != 0) { twitter.setRelationship(Primitive.LASTTIMELINE, memory.createVertex(max)); memory.save(); } } catch (Exception exception) { log(exception); } // Wait for language processing. int count = 0; while (count < 60 && !getBot().memory().getActiveMemory().isEmpty()) { Utils.sleep(1000); } }
From source file:org.botlibre.sense.twitter.Twitter.java
License:Open Source License
/** * Check messages to this user./*from w w w .ja va 2 s . c om*/ */ public void checkMentions() { if (!getReplyToMentions()) { return; } try { log("Checking mentions", Level.FINE); Network memory = getBot().memory().newMemory(); Vertex twitter = memory.createVertex(getPrimitive()); Vertex vertex = twitter.getRelationship(Primitive.LASTMENTION); long last = 0; if (vertex != null) { last = ((Number) vertex.getData()).longValue(); } long max = 0; ResponseList<Status> mentions = null; boolean more = true; int page = 1; while (more) { if (last == 0) { mentions = getConnection().getMentionsTimeline(); more = false; } else { Paging paging = new Paging(page, last); mentions = getConnection().getMentionsTimeline(paging); if ((mentions == null) || (mentions.size() < 20)) { more = false; } page++; } if ((mentions == null) || mentions.isEmpty()) { break; } log("Processing mentions", Level.FINE, mentions.size()); for (int index = mentions.size() - 1; index >= 0; index--) { Status tweet = mentions.get(index); long statusTime = tweet.getCreatedAt().getTime(); long statusId = tweet.getId(); if (statusId > max) { max = statusId; } if ((System.currentTimeMillis() - statusTime) > DAY) { log("Day old mention", Level.INFO, statusId, statusTime); more = false; continue; } // Exclude self if (tweet.getUser().getScreenName().equals(getUserName())) { continue; } if (statusId > last) { log("Processing mention", Level.INFO, tweet.getText(), tweet.getUser().getScreenName()); input(tweet); Utils.sleep(100); } else { log("Old mention", Level.INFO, statusId, statusTime); } } } if (max != 0) { twitter.setRelationship(Primitive.LASTMENTION, memory.createVertex(max)); memory.save(); } } catch (Exception exception) { log(exception); } // Wait for language processing. int count = 0; while (count < 60 && !getBot().memory().getActiveMemory().isEmpty()) { Utils.sleep(1000); } this.languageState = LanguageState.Discussion; }
From source file:org.examproject.tweet.service.SimpleTweetService.java
License:Apache License
private ResponseList<Status> getResponseList() { LOG.debug("called."); long cursol = -1; int listId = 0; int count = 50; int pageNumber = 1; Paging paging = new Paging(pageNumber, count); try {/*ww w . ja v a 2 s . com*/ // TODO: polymorphism to here? -> plugin. // home if (paramValue.getResponseListMode().equals("home")) { Twitter twitter = getTwitter(); return twitter.getHomeTimeline(paging); } // user if (paramValue.getResponseListMode().equals("user")) { Twitter twitter = getTwitter(); return twitter.getUserTimeline(paging); } // list if (paramValue.getResponseListMode().equals("list")) { if (paramValue.getUserListName().length() != 0) { Twitter twitter = getTwitter(); PagableResponseList<UserList> lists = twitter.getUserLists(twitter.getScreenName(), cursol); for (UserList list : lists) { String listFullName = list.getFullName(); if (listFullName.equals(paramValue.getUserListName())) { listId = list.getId(); return twitter.getUserListStatuses(listId, paging); } } } } // default.. Twitter twitter = getTwitter(); return twitter.getHomeTimeline(); } catch (TwitterException te) { // TODO: transition to an error page here? throw new RuntimeException(te); } }
From source file:org.mule.twitter.TwitterConnector.java
License:Open Source License
protected Paging getPaging(int page, int count, long sinceId) { Paging paging = new Paging(page, count); if (sinceId > 0) { paging.setSinceId(sinceId);/*from w w w . java 2 s .c o m*/ } return paging; }
From source file:org.nsoft.openbus.assynctask.UpdateTimelineTask.java
License:Open Source License
@Override protected Void doInBackground(Void... params) { try {//w w w . j a va2s. c om last = Facade.getInstance(ctx).getMensagemOf(Mensagem.TIPO_STATUS); AccessToken accessToken = uParams.getToken(); ResponseList<twitter4j.Status> list; int n_page = 1; int qtd_feeds = Constants.QTD_FEEDS; if (uParams != null) { n_page = uParams.getPage(); qtd_feeds = uParams.getQtdFeeds(); } Paging page = new Paging(n_page, qtd_feeds); list = TwitterUtils.getTwitter(accessToken).getHomeTimeline(page); ResponseUpdate response = TwitterUtils.updateTweets(ctx, list, Mensagem.TIPO_STATUS); messages = response.mensagens; /* * Mensagem lastMessage = response.lastMessage; * * boolean top = true; if (n_page > 1) top = false; * * if (mensagens.size() < Constants.QTD_FEEDS && !top) { // Atuliza * no topo page = new Paging(1, qtd_feeds); list = * TwitterUtils.getTwitter(accessToken).getHomeTimeline( page); * response = updateTweets(list); mensagens = response.mensagens; * * if (TimelineActivity.getCurrent() != null) * TimelineActivity.getCurrent().setCurrentList(mensagens); Intent * intent = new Intent( "com.twittemarkup.reciever.UPDATE_MSG"); * * Bundle b = new Bundle(); b.putBoolean("top", true); * intent.putExtras(b); ctx.sendBroadcast(intent); * * // atualiza nova pagina int num_feeds = * Facade.getInstance(ctx).getCountMensagem( Mensagem.TIPO_STATUS); * int currentPage = num_feeds / Constants.QTD_FEEDS; * * page = new Paging(currentPage + 1, qtd_feeds); list = * TwitterUtils.getTwitter(accessToken).getHomeTimeline( page); * response = updateTweets(list); messages = response.mensagens; * /*if (TimelineActivity.getCurrent() != null) * TimelineActivity.getCurrent().setCurrentList(mensagens); Intent * intent2 = new Intent( "com.twittemarkup.reciever.UPDATE_MSG"); * * Bundle b2 = new Bundle(); b.putBoolean("top", false); * intent2.putExtras(b2); ctx.sendBroadcast(intent2); * * return null; } else if (mensagens.size() >= Constants.QTD_FEEDS) * { Facade.getInstance(ctx).deleteAllTo( * lastMessage.getData().getTime()); } if * (TimelineActivity.getCurrent() != null) * TimelineActivity.getCurrent().setCurrentList(mensagens); Intent * intent = new Intent("com.twittemarkup.reciever.UPDATE_MSG"); * * Bundle b = new Bundle(); b.putBoolean("top", top); * intent.putExtras(b); ctx.sendBroadcast(intent); */ } catch (TwitterException e) { e.printStackTrace(); } return null; }
From source file:org.primefaces.examples.service.TwitterAPIService.java
License:Apache License
public List<Status> getTweets(String username) { List<Status> tweets = null; try {/*ww w. j a va 2 s .co m*/ if (username != null && !username.equals("")) { Twitter twitter = new TwitterFactory().getInstance(); twitter.setOAuthConsumer(twitter_consumer_key, twitter_consumer_secret); twitter.setOAuthAccessToken(new AccessToken(access_token, access_token_secret)); Paging p = new Paging(1, 200); tweets = twitter.getUserTimeline(username, p); } } catch (Exception e) { logger.severe(e.getMessage()); } return tweets; }
From source file:org.sakaiproject.myshowcase.tool.MyShowcaseSaveArtefactTwitterController.java
License:Open Source License
/** * Implementation of AbstractController.handleRequestInternal * @param HttpServletRequest request/*from w ww. j a va 2s . c o m*/ * @param HttpServletResponse response * @return ModelAndView * @throws Exception */ @Override protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { readRequest(request); Owner owner = myshowcaseService.getOwnerById(new Long(ownerId)); StatusMessage sm = new StatusMessage(); List<StatusMessage> sms = new ArrayList<StatusMessage>(); boolean tweetsFound = true; int totalTweets = 0; int artefactsCount = 0; List<Artefact> artefacts = new ArrayList<Artefact>(); try { Twitter twitter = new TwitterFactory().getInstance(); User user; user = twitter.showUser(aDataValue); int pageNo = 1; int pageSize = 200; int totalMatches = 0; String matchString = aSearchFilter; while (tweetsFound) { Paging paging = new Paging(pageNo, pageSize); ResponseList<Status> statuses = twitter.getUserTimeline(user.getId(), paging); for (Status status : statuses) { if ((matchString.length() == 0) || (status.getText().contains(matchString))) { totalMatches++; Artefact artefact = new Artefact(); ArtefactDetail artefactDetail = new ArtefactDetail(); ArtefactType artefactType = myshowcaseService.getArtefactTypeByName(aType); artefact.setOwner(owner); artefact.setDescription(aDescription); artefact.setName(aTitle); artefact.setShortDesc(aTitle); artefact.setType(artefactType); artefactDetail.setTwitterUserName(aDataValue); artefactDetail.setDetail(status.getText()); artefact.setArtefactDetail(artefactDetail); artefacts.add(artefact); // myshowcaseService.saveArtefact(artefact) ; // artefactsCount++; } } totalTweets += statuses.size(); pageNo++; tweetsFound = (statuses.size() > 0); } } catch (Exception e) { System.out.println("Exception: MyShowcaseSaveArtefactTwitter: " + e.toString()); } if (artefacts.size() > 0) { sm = new StatusMessage("OK", "A total of " + artefacts.size() + " tweets have been added to your evidence stream."); for (Artefact artefact : artefacts) { myshowcaseService.saveArtefact(artefact); } } else { sm = new StatusMessage("ERROR", "MyShowcase has been unable to collect any tweets for your request. No evidence has been added to your evidence stream."); } ; sms.add(sm); response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); PrintWriter out = response.getWriter(); out.write(new Gson().toJson(sms)); out.flush(); out.close(); return null; }
From source file:org.wso2.cep.uima.demo.TweetExtractor.java
License:Open Source License
/*** * *///from w w w .j a v a 2s. co m 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 .ja v a 2 s .com * 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."); }