List of usage examples for twitter4j Paging Paging
public Paging(int page, long sinceId)
From source file:com.temenos.interaction.example.mashup.twitter.Twitter4JConsumer.java
License:Open Source License
/** * @param otherUser// ww w .j ava2 s. c o m * @return */ public Collection<Tweet> requestTweetsByUser(String otherUser) { List<Tweet> tweets = new ArrayList<Tweet>(); try { // The factory instance is re-useable and thread safe. Twitter twitter = new TwitterFactory().getInstance(); AccessToken accessToken = loadAccessToken(1); twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET); twitter.setOAuthAccessToken(accessToken); if (LOGGER.isInfoEnabled()) { LOGGER.info("Fetching latest 100 tweets for [" + otherUser + "]"); } // First param of Paging() is the page number, second is the number per page (this is capped around 200 I think. Paging paging = new Paging(1, 100); List<Status> statuses = twitter.getUserTimeline(otherUser, paging); for (Status status : statuses) { tweets.add(new Tweet(otherUser, status.getText(), (status.getGeoLocation() != null ? status.getGeoLocation().getLatitude() + "," + status.getGeoLocation().getLongitude() : ""))); if (LOGGER.isInfoEnabled()) { LOGGER.info( status.getUser().getName() + "(" + status.getGeoLocation() + "):" + status.getText()); } } } catch (Exception e) { LOGGER.error("Error on requestTweetsByUser", e); throw new TwitterMashupException(e); } return tweets; }
From source file:com.TweetExtractor.java
/** * *// ww w. j a v a2 s. c o m * */ public ArrayList<Tweet> retrieveTweets(String searchWord) { 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; userToSearch = searchWord; 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) { 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); // 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) { return tweetList; // break; } } catch (TwitterException e) { e.printStackTrace(); break; } catch (Exception e) { e.printStackTrace(); break; } } return tweetList; }
From source file:crawling.GetUserTimelineMulti.java
License:Apache License
public void doCrawl() { List<Status> statuses = null; int count = 0; int userIndex = 0; int userTotal = users.size(); boolean overflow = false; for (Long usr : users) { //System.out.println("%" + usr); out.println("%" + usr); Paging paging = null;//w w w . ja va 2s .c o m count = 0; //for (int i = 1; i < 21; i++) { for (int i = 1; i < 4; i++) { paging = new Paging(i, 200); //paging = new Paging(i, 200, sinceId, maxId); overflow = false; try { // statuses = twitter.getUserTimeline(user, paging); statuses = twitters.get(currentCrawl).getUserTimeline(usr, paging); currentCrawl++; if (currentCrawl == this.numCrawl) currentCrawl = 0; /*Thread.currentThread(); try { Thread.sleep(updateFreq); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }*/ } catch (TwitterException te) { te.printStackTrace(); System.out.println("Failed to get timeline: " + te.getMessage()); //System.exit(-1); } if (statuses == null) { break; } if (statuses.isEmpty()) { if (i > 16) overflow = true; break; } for (Status status : statuses) { int len = status.getUserMentionEntities().length; /* * We only need the interactions */ if (len <= 0) continue; String record = ""; record += status.getId(); record += "::" + status.getInReplyToStatusId(); record += "::" + status.getInReplyToUserId(); record += "::" + status.getRetweetCount(); if (status.getRetweetedStatus() != null) record += "::" + status.getRetweetedStatus().getId(); else record += "::" + "-1"; //record += "::" + status.isRetweet(); //int len = status.getUserMentionEntities().length; if (len > 0) { record += "::"; for (int l = 0; l < len; l++) { UserMentionEntity ent = status.getUserMentionEntities()[l]; record += "," + ent.getId(); } } else { record += "::" + "-1"; } len = status.getURLEntities().length; if (len > 0) { record += "::"; for (int l = 0; l < len; l++) { URLEntity ent = status.getURLEntities()[l]; record += "," + ent.getURL() + "|" //+ ent.getDisplayURL() + "|" + ent.getExpandedURL(); } } else record += "::" + "-1"; //record += "::" + cleanText(status.getText()); // We just ignore the text content in this crawling record += "::-1"; record += "::" + // status.getCreatedAt(); (status.getCreatedAt().getTime() - start.getTime()) / 1000; record += "::" + getSource(status.getSource()); // Geo Location if (status.getGeoLocation() != null) record += "::" + status.getGeoLocation(); else record += "::" + "-1"; //System.out.println(record); out.println(record); count++; } //count += statuses.size(); } out.flush(); totalCount += count; out.println("%" + usr + ", " + count + ", " + overflow); outUserId.println(usr + "," + count); //System.out.println("%" + usr + ", " + count + ", " + overflow); System.out.println(userIndex + "/" + userTotal + ", " + count); //+ ", " + (overflow ? "overflow" : "not overflow")); userIndex++; out.println("------------------------------------------"); } System.out.println("Total status count is " + totalCount); outUserId.println("#" + totalCount); this.closeFile(); }
From source file:de.jetsli.twitter.TwitterSearch.java
License:Apache License
public List<Status> getTweets(String screenName) throws TwitterException { List<Status> tweets = twitter.getUserTimeline(screenName, new Paging(1, 100)); rateLimit--;/*from w w w. ja v a 2 s . com*/ return tweets; }
From source file:dgsf.twittertree.TweetsContainerNode.java
License:Open Source License
/** costruttore della classe {@link TweetsContainerNode} * @param fatherFullKey chiave completa del nodo padre * @param key chiave del nodo attuale/* w w w . j a v a 2 s . co m*/ * @param father puntatore al nodo genitore * **/ public TweetsContainerNode(String fatherFullKey, String key, Node father) { super(fatherFullKey, key, father); /* richiama il costruttore di AbstractNode */ try { User user = twitter.showUser( father.getKey()); /* recupera l'utente padre a cui appartengono i tweet del contenitore*/ tweetList = twitter.getUserTimeline(father.getKey(), new Paging(1, user.getStatusesCount())); /* recupera lista dei suoi tweet */ } catch (TwitterException e) { this.tweetList = null; /* recupero fallito */ } }
From source file:edu.cmu.cs.lti.discoursedb.io.twitter.converter.TwitterConverterService.java
License:Open Source License
/** * For each user in the mongodb dataset, import the whole timeline of that user (API limit: latest 3,200 tweets) * /*from ww w .j ava 2s. co m*/ * @param users * @param discourseName * @param datasetName */ public void importUserTimelines(List<String> users, String discourseName, String datasetName) { Twitter twitter = TwitterFactory.getSingleton(); log.info("Importing timelines for " + users.size() + " users into DiscourseDB"); for (String screenname : users) { log.info("Retrieving timeline for user " + screenname); List<Status> tweets = new ArrayList<>(); //There's an API limit of 3,200 tweets you can get from a timeline and 200 per request (page). //This makes 16 requests with 200 tweets per page (pages 1 to 17) //This also works if the users has less than 3,200 tweets for (int i = 1; i < 17; i++) { try { tweets.addAll(twitter.getUserTimeline(screenname, new Paging(i, 200))); } catch (TwitterException e) { log.error("Error retrieving timeline for user " + screenname, e); } } log.info("Retrieved timeline (" + tweets.size() + " Tweets) for user " + screenname); log.info("Mapping tweets for user " + screenname); for (Status tweet : tweets) { log.info("Mapping tweet " + tweet.getId()); mapTweet(discourseName, datasetName, tweet, null); } } }
From source file:edu.proyectofinal.integradorrs.services.tweets.impl.TweetsServiceImpl.java
@Override public Collection<Status> getUserTimeline(String user, String email) { TwitterCredentials tc = TwitterCredentials.getInstance(); ConfigurationBuilder cb = tc.GetCredentials(email); TwitterFactory tf = new TwitterFactory(cb.build()); Twitter twitter = tf.getInstance();/*from ww w . j a v a 2s . c om*/ List<Status> statuses = null; Paging paging = new Paging(1, 30); try { statuses = twitter.getUserTimeline(user, paging); } catch (TwitterException ex) { Logger.getLogger(TweetsServiceImpl.class.getName()).log(Level.SEVERE, null, ex); } Collection<Status> result = (Collection<Status>) statuses; return result; }
From source file:edu.umich.cse.pyongjoo.twittercrawl.GetUserTimeline.java
License:Apache License
/** * Usage: java twitter4j.examples.timeline.GetUserTimeline * * @param args String[]/* ww w. j a v a 2s .c om*/ * @throws IOException */ public static void main(String[] args) throws IOException { OAuthTokenReader oauth = new OAuthTokenReader("oauth_tokens.csv"); TwitterFactory tf = new TwitterFactory(oauth.getNextConfiguration()); // gets Twitter instance with default credentials Twitter twitter = tf.getInstance(); if (args.length < 2) { System.err.println("Usuage: command [username] [outputfile]"); System.exit(-1); } String filename = args[1]; FileWriter fstream = new FileWriter(filename, true); BufferedWriter out = new BufferedWriter(fstream); String user = ""; if (args.length >= 1) { user = args[0]; } // out.write("#document starts with username: " + user + "\n"); for (int i = 1; i <= 1; i++) { Paging pagingOption = new Paging(i, 200); try { List<Status> statuses; statuses = twitter.getUserTimeline(user, pagingOption); System.out.println("My Custom Showing @" + user + "'s user timeline."); for (Status status : statuses) { out.write(status.toString() + '\n'); System.out.println(status.getUser().getScreenName() + "tweets written."); } } catch (TwitterException te) { te.printStackTrace(); System.out.println("Failed to get timeline: " + te.getMessage()); // close the file out.close(); // output.close(); System.exit(-1); } } // close the file out.close(); // output.close(); }
From source file:es.upm.oeg.examples.watson.servlets.TwitterAnalysisServlet.java
License:Apache License
/** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) *///from ww w . ja v a2 s . co m protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String twitterUsername = request.getParameter("twitter_name"); // The factory instance is re-useable and thread safe. Twitter twitter = new TwitterFactory().getInstance(); env = System.getenv(); consumer_key = env.get("TWITTER_CONSUMER_KEY"); consumer_secret = env.get("TWITTER_CONSUMER_SECRET"); access_token = env.get("TWITTER_ACCESS_TOKEN"); access_key = env.get("TWITTER_ACCESS_KEY"); AccessToken accessToken = new AccessToken(access_token, access_key); try { twitter.setOAuthConsumer(consumer_key, consumer_secret); twitter.setOAuthAccessToken(accessToken); twitter4j.User a_name = twitter.showUser(twitterUsername); int followerCount = a_name.getFollowersCount(); List<Status> retweets = twitter.getUserTimeline(twitterUsername, new Paging(1, 20)); // get the first twenty tweets int retweetCount = 0; List<String> langs = new ArrayList<>(); List<String> translated = new ArrayList<>(); StringBuilder aggregatedTextBuilder = new StringBuilder(); String personalityInsights = null; for (Status tweet : retweets) { String tweetText = tweet.getText(); try { String lang = languageIdentification.getLang(tweetText); langs.add(lang); String englishText; //TODO do the same for french and portuguese if (LanguageIdentificationService.ES_ES.equals(lang)) { englishText = machineTranslation.translate(tweetText, MachineTranslationService.ES_TO_EN); } else if (LanguageIdentificationService.FR_FR.equals(lang)) { englishText = machineTranslation.translate(tweetText, MachineTranslationService.FR_TO_EN); } else if (LanguageIdentificationService.PT_BR.equals(lang)) { englishText = machineTranslation.translate(tweetText, MachineTranslationService.PT_TO_EN); } else { englishText = tweetText; } translated.add(englishText); aggregatedTextBuilder.append(englishText); personalityInsights = personalityInsightsService.analyse(aggregatedTextBuilder.toString()); } catch (Exception e) { // Log something and return an error message logger.log(Level.SEVERE, "got error: " + e.getMessage(), e); request.setAttribute("error", e.getMessage()); } } request.setAttribute("t_name", twitterUsername); request.setAttribute("rtweets", retweets); request.setAttribute("langs", langs); request.setAttribute("translated", translated); request.setAttribute("personalityInsights", personalityInsights); request.setAttribute("aggregatedText", aggregatedTextBuilder.toString()); request.getRequestDispatcher("/myTweets.jsp").forward(request, response); } catch (TwitterException e) { e.printStackTrace(); if (e.getErrorCode() == 215 || e.getErrorCode() == 32) { response.sendRedirect("../index.html?message=errorcode215"); } else if (e.getErrorCode() == -1 || e.getErrorCode() == 34) { response.sendRedirect("../index.html?message=errorcode-1"); } else { response.sendRedirect("../index.html?message=errorcode99"); } //throw new ServletException("Encountered a problem fetching data from Twitter - " + e.getErrorMessage()); } }
From source file:info.maslowis.twitterripper.command.impl.twitter.TimelineGetId.java
License:Open Source License
@Override public void execute() throws ExecuteCmdException { try {/*from w ww .java 2s. co m*/ ResponseList<Status> statuses = twitter.getUserTimeline(id, new Paging(page, count)); if (statuses.isEmpty()) { out.println("No statuses"); } else { for (Status status : statuses) { out.println(Util.toString(status)); } } } catch (TwitterException e) { throw new ExecuteCmdException(e); } }