List of usage examples for twitter4j Twitter getUserTimeline
ResponseList<Status> getUserTimeline(String screenName, Paging paging) throws TwitterException;
From source file:org.mariotaku.twidere.loader.UserTimelineLoader.java
License:Open Source License
@Override protected ResponseList<Status> getStatuses(final Twitter twitter, final Paging paging) throws TwitterException { if (twitter == null) return null; final ResponseList<Status> statuses; if (mUserId != -1) { statuses = twitter.getUserTimeline(mUserId, paging); } else if (mUserScreenName != null) { statuses = twitter.getUserTimeline(mUserScreenName, paging); } else// w ww . ja va2 s .c om return null; if (mTotalItemsCount == -1 && !statuses.isEmpty()) { final User user = statuses.get(0).getUser(); if (user != null) { mTotalItemsCount = user.getStatusesCount(); } } return statuses; }
From source file:org.primefaces.examples.service.TwitterAPIService.java
License:Apache License
public List<Status> getTweets(String username) { List<Status> tweets = null; try {/* www .j a va 2 s .c o 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.rhq.plugins.twitter.FeedComponent.java
License:Open Source License
/** * Gather measurement data//from www . jav a2 s.co m * @see org.rhq.core.pluginapi.measurement.MeasurementFacet#getValues(org.rhq.core.domain.measurement.MeasurementReport, java.util.Set) */ public void getValues(MeasurementReport report, Set<MeasurementScheduleRequest> metrics) throws Exception { for (MeasurementScheduleRequest req : metrics) { if (req.getName().equals("tweetCount")) { Twitter twitter = tFactory.getInstance(); Paging paging = new Paging(); MeasurementDataNumeric res; if (isSearch) { Query q = new Query(keyword); q.setSinceId(lastId); if (lastId == NOT_YET_SET) q.setRpp(1); else q.setRpp(20); QueryResult qr = twitter.search(q); List<Tweet> tweets = qr.getTweets(); res = new MeasurementDataNumeric(req, (double) tweets.size()); eventPoller.addTweets(tweets); if (tweets.size() > 0) lastId = tweets.get(0).getId(); } else { List<Status> statuses; if (lastId == NOT_YET_SET) { paging.setCount(1); } else { paging.setCount(100); } paging.setSinceId(lastId); statuses = twitter.getUserTimeline(keyword, paging); res = new MeasurementDataNumeric(req, (double) statuses.size()); eventPoller.addStatuses(statuses); if (statuses.size() > 0) lastId = statuses.get(0).getId(); } report.addData(res); } } }
From source file:org.sakaiproject.myshowcase.tool.MyShowcaseSaveArtefactTwitterController.java
License:Open Source License
/** * Implementation of AbstractController.handleRequestInternal * @param HttpServletRequest request// www. ja 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.fasttrack.project.roadlk.rest.DatasetGenerator.java
License:Open Source License
/** * @param consumerKey/* w w w . j a v a2 s .c om*/ * 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."); }
From source file:Principal.Tracker_Twitter.java
License:Minecraft Mod Public
public void guardarResultados_Twitter(List<ObjetoBuscar> lista, BD base, int contadorBase, int TokenIndice) throws NoSuchAlgorithmException, KeyManagementException { int nuevoContadorBase = 0; if (contadorBase >= lista.size()) { System.out.println("Termino en:" + contadorBase); } else {/*from w w w . j av a 2 s.c o m*/ System.out.println("---------------------------------------------------------------------:P"); TrustManager[] trustAllCerts = { new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { return null; } public void checkClientTrusted(X509Certificate[] certs, String authType) { } public void checkServerTrusted(X509Certificate[] certs, String authType) { } } }; SSLContext sc = SSLContext.getInstance("SSL"); sc.init(null, trustAllCerts, new SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); HostnameVerifier allHostsValid = new HostnameVerifier() { public boolean verify(String hostname, SSLSession session) { return true; } }; HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid); System.out.println("Token : " + TokenIndice); System.out.println(consumerKey[TokenIndice]); try { ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setOAuthConsumerKey(consumerKey[TokenIndice]); cb.setOAuthConsumerSecret(this.consumerSecret[TokenIndice]); cb.setOAuthAccessToken(this.token[TokenIndice]); cb.setOAuthAccessTokenSecret(this.tokenSecret[TokenIndice]); Twitter unauthenticatedTwitter = new TwitterFactory(cb.build()).getInstance(); for (int numtTweets = contadorBase; numtTweets < contadorBase + 5; numtTweets++) { if (((ObjetoBuscar) lista.get(numtTweets)).getUrl().equals("") || ((ObjetoBuscar) lista.get(numtTweets)).getUrl() == null) { } else { System.out.println("Usuario: " + ((ObjetoBuscar) lista.get(numtTweets)).getUrl()); String usuariosinArroba = ((ObjetoBuscar) lista.get(numtTweets)).getUrl().replace("@", ""); System.out.println("" + usuariosinArroba); try { User usuario = unauthenticatedTwitter.showUser(usuariosinArroba); List<Status> ret = unauthenticatedTwitter.getRetweetsOfMe(); List<Status> favoritos = unauthenticatedTwitter.getFavorites(); Paging paging = new Paging(1, 1000); ResponseList<Status> statuses = unauthenticatedTwitter.getUserTimeline(usuario.getId(), paging); System.out.println("Followers: " + usuario.getFollowersCount()); System.out.println("Yo sigo: " + usuario.getFriendsCount()); List<String> listaTweets = new ArrayList(); List<Long> ListaRettewts = new ArrayList(); List<Integer> ListaFavoritos = new ArrayList(); List<Integer> ListaMenciones = new ArrayList(); List<Date> ListaFecha = new ArrayList(); List<Long> ListaIds = new ArrayList(); for (Status sta : statuses) { ListaIds.add(Long.valueOf(sta.getId())); listaTweets.add(sta.getText()); ListaRettewts.add(Long.valueOf(Long.parseLong(sta.getRetweetCount() + ""))); ListaMenciones.add(Integer.valueOf(sta.getUserMentionEntities().length)); ListaFecha.add(sta.getCreatedAt()); ListaFavoritos.add(Integer.valueOf(sta.getFavoriteCount())); } for (int i = 0; i < listaTweets.size(); i++) { base.guardarTrackTwitter_Log((Long) ListaIds.get(i), ((ObjetoBuscar) lista.get(numtTweets)).getUrl(), (String) listaTweets.get(i), (Date) ListaFecha.get(i), (Long) ListaRettewts.get(i), ((Integer) ListaFavoritos.get(i)).intValue(), ((Integer) ListaMenciones.get(i)).intValue()); } } catch (Exception e) { System.err.println("Entro al Try por :" + e); } nuevoContadorBase = numtTweets; } } System.out.println("Numero de Contador Base:" + nuevoContadorBase); guardarResultados_Twitter(lista, base, nuevoContadorBase + 1, TokenIndice + 1); } catch (NumberFormatException e) { System.err.println("Fallo por :" + e); } } }
From source file:timeline.CmdUserTimeline.java
License:Apache License
public static void main(String[] args) throws ClassNotFoundException, SQLException, JSONException, IOException { // Check how many arguments were passed in if ((args == null) || (args.length != 7)) { System.err.println("Please provide command as following."); System.err.println("java -cp twitter4j-multi-oauth-0.3.jar " + "timeline.CmdUserTimeline consumer_key consumer_secret" + " user_token user_secret output_path " + "screen_name_or_userid " + "number_of_tweets_to_get_max_is_3200"); System.exit(-1);/* w w w . j av a 2s. c o m*/ } AppOAuth AppOAuths = new AppOAuth(); Misc helpers = new Misc(); String endpoint = "/statuses/user_timeline"; String consumer_key = null; try { consumer_key = StringEscapeUtils.escapeJava(args[0]); } catch (Exception e) { System.err.println("Argument" + args[0] + " must be an String."); System.exit(-1); } String consumer_secret = null; try { consumer_secret = StringEscapeUtils.escapeJava(args[1]); } catch (Exception e) { System.err.println("Argument" + args[1] + " must be an String."); System.exit(-1); } String user_token = null; try { user_token = StringEscapeUtils.escapeJava(args[2]); } catch (Exception e) { System.err.println("Argument" + args[2] + " must be an String."); System.exit(-1); } String user_secret = null; try { user_secret = StringEscapeUtils.escapeJava(args[3]); } catch (Exception e) { System.err.println("Argument" + args[3] + " must be an String."); System.exit(-1); } String OutputDirPath = null; try { OutputDirPath = StringEscapeUtils.escapeJava(args[4]); } catch (Exception e) { System.err.println("Argument" + args[4] + " must be an String."); System.exit(-1); } String targetedUser = ""; try { targetedUser = StringEscapeUtils.escapeJava(args[5]); } catch (Exception e) { System.err.println("Argument" + args[5] + " must be an String."); System.exit(-1); } int NUMBER_OF_TWEETS = 0; try { NUMBER_OF_TWEETS = Integer.parseInt(args[6]); } catch (Exception e) { System.err.println("Argument" + args[6] + " must be an integer."); System.exit(-1); } try { TwitterFactory tf = AppOAuths.loadOAuthUser(endpoint, consumer_key, consumer_secret, user_token, user_secret); Twitter twitter = tf.getInstance(); int RemainingCalls = AppOAuths.RemainingCalls - 2; int RemainingCallsCounter = 0; System.out.println("Remianing Calls: " + RemainingCalls); // screen_name / user_id provided by arguments System.out.println("Trying to create output directory"); String filesPath = OutputDirPath + "/"; File theDir = new File(filesPath); // If the directory does not exist, create it if (!theDir.exists()) { try { theDir.mkdirs(); } catch (SecurityException se) { System.err.println("Could not create output " + "directory: " + OutputDirPath); System.err.println(se.getMessage()); System.exit(-1); } } String fileName = filesPath + targetedUser; PrintWriter writer = new PrintWriter(fileName, "UTF-8"); // Call different functions for screen_name and id_str Boolean chckedNumaric = helpers.isNumeric(targetedUser); List<Status> statuses = new ArrayList<>(); int size = statuses.size(); int pageno = 1; int totalTweets = 0; boolean tweetCounterReached = false; System.out.println("NUMBER_OF_TWEETS to get:" + NUMBER_OF_TWEETS); while (true) { try { Paging page = new Paging(pageno++, 200); if (chckedNumaric) { long LongValueTargetedUser = Long.valueOf(targetedUser).longValue(); statuses.addAll(twitter.getUserTimeline(LongValueTargetedUser, page)); if (statuses.size() > 0) { for (Status status : statuses) { String rawJSON = TwitterObjectFactory.getRawJSON(status); writer.println(rawJSON); totalTweets += 1; if (totalTweets >= NUMBER_OF_TWEETS) { tweetCounterReached = true; break; } } if (tweetCounterReached) { break; } } } else { statuses.addAll(twitter.getUserTimeline(targetedUser, page)); if (statuses.size() > 0) { for (Status status : statuses) { String rawJSON = TwitterObjectFactory.getRawJSON(status); writer.println(rawJSON); totalTweets += 1; if (totalTweets >= NUMBER_OF_TWEETS) { tweetCounterReached = true; break; } } if (tweetCounterReached) { break; } } } // If user's total tweet are less than 195 then no next call if (size == 0) { if (totalTweets < 195) { break; } } // If user's all tweets parsed then exit if (totalTweets == size) { break; } size = totalTweets; statuses.clear(); } catch (TwitterException e) { // e.printStackTrace(); // do not throw if user has protected tweets, or if they // deleted their account if (e.getStatusCode() == HttpResponseCode.UNAUTHORIZED || e.getStatusCode() == HttpResponseCode.NOT_FOUND) { System.out.println(targetedUser + " is protected or account is deleted"); } else { System.out.println("Tweets Get Exception: " + e.getMessage()); } // If rate limit reached then switch Auth user RemainingCallsCounter++; if (RemainingCallsCounter >= RemainingCalls) { System.out.println("No more remianing calls"); } if (totalTweets < 1) { writer.close(); // Remove file if tweets not found File fileToDelete = new File(fileName); fileToDelete.delete(); } break; } // If rate limit reached then switch Auth user RemainingCallsCounter++; if (RemainingCallsCounter >= RemainingCalls) { System.out.println("No more remianing calls"); break; } } // while get tweets writer.close(); if (totalTweets > 0) { System.out.println("Total dumped tweets of " + targetedUser + " are: " + totalTweets); } else { // Remove file if tweets not found File fileToDelete = new File(fileName); fileToDelete.delete(); } } catch (TwitterException te) { // te.printStackTrace(); System.out.println("Failed to get followers' ids: " + te.getMessage()); System.exit(-1); } System.out.println("!!!! DONE !!!!"); }
From source file:timeline.DBNonThreader.java
License:Apache License
public static void main(String[] args) throws ClassNotFoundException, SQLException, JSONException, IOException { // Check how many arguments were passed in if ((args == null) || (args.length == 0)) { System.err.println("First: 'OUTPUT_PATH' is mendatory."); System.err.println("Second: (int) Number of Tweets to get. Max 3200"); System.err.println("Third: 'screen_name / id_str'" + " is optional."); System.err.println("If 3rd argument not provided then provide" + " Twitter users through database."); System.exit(-1);/*from w w w.j a v a 2 s . co m*/ } MysqlDB DB = new MysqlDB(); AppOAuth AppOAuths = new AppOAuth(); Misc helpers = new Misc(); String endpoint = "/statuses/user_timeline"; String OutputDirPath = null; try { OutputDirPath = StringEscapeUtils.escapeJava(args[0]); } catch (Exception e) { System.err.println("Argument" + args[0] + " must be an String."); System.exit(-1); } int NUMBER_OF_TWEETS = 3200; try { NUMBER_OF_TWEETS = Integer.parseInt(args[1]); } catch (Exception e) { System.err.println("Argument" + args[1] + " must be an integer."); System.exit(-1); } String targetedUser = ""; if (args.length == 3) { try { targetedUser = StringEscapeUtils.escapeJava(args[2]); } catch (Exception e) { System.err.println("Argument" + args[2] + " must be an String."); System.exit(-1); } } try { TwitterFactory tf = AppOAuths.loadOAuthUser(endpoint); Twitter twitter = tf.getInstance(); int RemainingCalls = AppOAuths.RemainingCalls - 2; int RemainingCallsCounter = 0; System.out.println("First Time Remianing Calls: " + RemainingCalls); String Screen_name = AppOAuths.screen_name; System.out.println("First Time Loaded OAuth Screen_name: " + Screen_name); System.out.println("User's Tweets"); // If targetedUser not provided by argument, then look into // database. if (StringUtils.isEmpty(targetedUser)) { String selectQuery = "SELECT `id`,`targeteduser` FROM " + "`twitter_users` WHERE " + "`tweets_dumped_all` = 0"; ResultSet results = DB.selectQ(selectQuery); int numRows = DB.numRows(results); if (numRows < 1) { System.err.println("No User in database to get Tweets"); System.exit(-1); } System.out.println("Trying to create output directory"); String filesPath = OutputDirPath + "/"; File theDir = new File(filesPath); // If the directory does not exist, create it if (!theDir.exists()) { try { theDir.mkdirs(); } catch (SecurityException se) { System.err.println("Could not create output " + "directory: " + OutputDirPath); System.err.println(se.getMessage()); System.exit(-1); } } OUTERMOST: while (results.next()) { int targetedUserID = results.getInt("id"); targetedUser = results.getString("targeteduser"); System.out.println("Targeted User: " + targetedUser); String fileName = filesPath + "/" + targetedUser; PrintWriter writer = new PrintWriter(fileName, "UTF-8"); // Call different functions for screen_name and id_str Boolean chckedNumaric = helpers.isNumeric(targetedUser); List<Status> statuses = new ArrayList<>(); int size = statuses.size(); int pageno = 1; int totalTweets = 0; boolean tweetCounterReached = false; System.out.println("NUMBER_OF_TWEETS to get:" + NUMBER_OF_TWEETS); while (true) { try { Paging page = new Paging(pageno++, 200); if (chckedNumaric) { long LongValueTargetedUser = Long.valueOf(targetedUser).longValue(); statuses.addAll(twitter.getUserTimeline(LongValueTargetedUser, page)); if (statuses.size() > 0) { for (Status status : statuses) { String rawJSON = TwitterObjectFactory.getRawJSON(status); writer.println(rawJSON); totalTweets += 1; if (totalTweets >= NUMBER_OF_TWEETS) { tweetCounterReached = true; break; } } if (tweetCounterReached) { break; } } } else { statuses.addAll(twitter.getUserTimeline(targetedUser, page)); if (statuses.size() > 0) { for (Status status : statuses) { String rawJSON = TwitterObjectFactory.getRawJSON(status); writer.println(rawJSON); totalTweets += 1; if (totalTweets >= NUMBER_OF_TWEETS) { tweetCounterReached = true; break; } } if (tweetCounterReached) { break; } } } // If user's total tweet are less than 195 // then no next call if (size == 0) { if (totalTweets < 195) { break; } } // If user's all tweets parsed // then proceed to next user if (totalTweets == size) { break; } size = totalTweets; statuses.clear(); } catch (TwitterException e) { // e.printStackTrace(); // do not throw if user has protected tweets, // or if they deleted their account if (e.getStatusCode() == HttpResponseCode.UNAUTHORIZED || e.getStatusCode() == HttpResponseCode.NOT_FOUND) { System.out.println(targetedUser + " is protected or account is deleted"); } else { System.out.println("Tweets Get Exception: " + e.getMessage()); } // If rate limit reached then switch Auth user RemainingCallsCounter++; if (RemainingCallsCounter >= RemainingCalls) { // Load OAuth user tf = AppOAuths.loadOAuthUser(endpoint); twitter = tf.getInstance(); System.out.println( "New User Loaded OAuth" + " Screen_name: " + AppOAuths.screen_name); RemainingCalls = AppOAuths.RemainingCalls - 2; RemainingCallsCounter = 0; System.out.println("New Remianing Calls: " + RemainingCalls); } if (totalTweets < 1) { writer.close(); // Remove file if tweets not found File fileToDelete = new File(fileName); fileToDelete.delete(); } // Update stats String fieldValues = "`tweets_dumped_all` = 2"; String where = "id = " + targetedUserID; DB.Update("`twitter_users`", fieldValues, where); continue OUTERMOST; } // If rate limit reached then switch Auth user RemainingCallsCounter++; if (RemainingCallsCounter >= RemainingCalls) { // Load OAuth user tf = AppOAuths.loadOAuthUser(endpoint); twitter = tf.getInstance(); System.out.println("New User Loaded OAuth Screen_name: " + AppOAuths.screen_name); RemainingCalls = AppOAuths.RemainingCalls - 2; RemainingCallsCounter = 0; System.out.println("New Remianing Calls: " + RemainingCalls); } } // while get tweets writer.close(); if (totalTweets > 0) { System.out.println("Total dumped tweets of " + targetedUser + " are: " + totalTweets); // Update stats String fieldValues = "`tweets_dumped_all` = 1, " + " `tweets_all_count` = " + totalTweets; String where = "id = " + targetedUserID; DB.Update("`twitter_users`", fieldValues, where); } else { // Update stats String fieldValues = "`tweets_dumped_all` = 2"; String where = "id = " + targetedUserID; DB.Update("`twitter_users`", fieldValues, where); // Remove file if tweets not found File fileToDelete = new File(fileName); fileToDelete.delete(); } // If rate limit reached then switch Auth user RemainingCallsCounter++; if (RemainingCallsCounter >= RemainingCalls) { // Load OAuth user tf = AppOAuths.loadOAuthUser(endpoint); twitter = tf.getInstance(); System.out.println("New User Loaded OAuth Screen_name: " + AppOAuths.screen_name); RemainingCalls = AppOAuths.RemainingCalls - 2; RemainingCallsCounter = 0; System.out.println("New Remianing Calls: " + RemainingCalls); } } // while get users from database } else { // screen_name / user_id provided by arguments System.out.println("screen_name / user_id provided by arguments"); System.out.println("Trying to create output directory"); String filesPath = OutputDirPath + "/"; File theDir = new File(filesPath); // If the directory does not exist, create it if (!theDir.exists()) { try { theDir.mkdirs(); } catch (SecurityException se) { System.err.println("Could not create output " + "directory: " + OutputDirPath); System.err.println(se.getMessage()); System.exit(-1); } } System.out.println("Targeted User: " + targetedUser); String fileName = filesPath + "/" + targetedUser; PrintWriter writer = new PrintWriter(fileName, "UTF-8"); // Call different functions for screen_name and id_str Boolean chckedNumaric = helpers.isNumeric(targetedUser); List<Status> statuses = new ArrayList<>(); int size = statuses.size(); int pageno = 1; int totalTweets = 0; boolean tweetCounterReached = false; System.out.println("NUMBER_OF_TWEETS to get:" + NUMBER_OF_TWEETS); while (true) { try { Paging page = new Paging(pageno++, 200); if (chckedNumaric) { long LongValueTargetedUser = Long.valueOf(targetedUser).longValue(); statuses.addAll(twitter.getUserTimeline(LongValueTargetedUser, page)); if (statuses.size() > 0) { for (Status status : statuses) { String rawJSON = TwitterObjectFactory.getRawJSON(status); writer.println(rawJSON); totalTweets += 1; if (totalTweets >= NUMBER_OF_TWEETS) { tweetCounterReached = true; break; } } if (tweetCounterReached) { break; } } } else { statuses.addAll(twitter.getUserTimeline(targetedUser, page)); if (statuses.size() > 0) { for (Status status : statuses) { String rawJSON = TwitterObjectFactory.getRawJSON(status); writer.println(rawJSON); totalTweets += 1; if (totalTweets >= NUMBER_OF_TWEETS) { tweetCounterReached = true; break; } } if (tweetCounterReached) { break; } } } // If user's total tweet are less than 195 then no next call if (size == 0) { if (totalTweets < 195) { break; } } // If user's all tweets parsed then proceed to next user if (totalTweets == size) { break; } size = totalTweets; statuses.clear(); } catch (TwitterException e) { // e.printStackTrace(); // do not throw if user has protected tweets, or if they deleted their account if (e.getStatusCode() == HttpResponseCode.UNAUTHORIZED || e.getStatusCode() == HttpResponseCode.NOT_FOUND) { System.out.println(targetedUser + " is protected or account is deleted"); } else { System.out.println("Tweets Get Exception: " + e.getMessage()); } // If rate limit reached then switch Auth user RemainingCallsCounter++; if (RemainingCallsCounter >= RemainingCalls) { // Load OAuth user tf = AppOAuths.loadOAuthUser(endpoint); twitter = tf.getInstance(); System.out.println("New User Loaded OAuth" + " Screen_name: " + AppOAuths.screen_name); RemainingCalls = AppOAuths.RemainingCalls - 2; RemainingCallsCounter = 0; System.out.println("New Remianing Calls: " + RemainingCalls); } if (totalTweets < 1) { writer.close(); // Remove file if tweets not found File fileToDelete = new File(fileName); fileToDelete.delete(); } } // If rate limit reached then switch Auth user RemainingCallsCounter++; if (RemainingCallsCounter >= RemainingCalls) { // Load OAuth user tf = AppOAuths.loadOAuthUser(endpoint); twitter = tf.getInstance(); System.out.println("New User Loaded OAuth Screen_name: " + AppOAuths.screen_name); RemainingCalls = AppOAuths.RemainingCalls - 2; RemainingCallsCounter = 0; System.out.println("New Remianing Calls: " + RemainingCalls); } } // while get tweets writer.close(); if (totalTweets > 0) { System.out.println("Total dumped tweets of " + targetedUser + " are: " + totalTweets); } else { // Remove file if tweets not found File fileToDelete = new File(fileName); fileToDelete.delete(); } } // screen_name / user_id provided by arguments } catch (TwitterException te) { // te.printStackTrace(); System.out.println("Failed to get followers' ids: " + te.getMessage()); System.exit(-1); } System.out.println("!!!! DONE !!!!"); }
From source file:timeline.DBThreaderParser.java
License:Apache License
public static void main(String[] args) throws ClassNotFoundException, SQLException, JSONException, IOException { // Check how many arguments were passed in if ((args == null) || (args.length < 5)) { System.err.println("5 Parameters are required to launch a Job."); System.err.println("First: String 'OUTPUT_PATH'"); System.err.println("Second: (int) Total Number Of Jobs"); System.err.println("Third: (int) This Job Number"); System.err.println("Fourth: (int) Seconds to pause between next launch"); System.err.println("Fifth: (int) Number of Tweets to get. Max 3200"); System.err.println("Example: fileName.class /output/path 10 1 2 3200"); System.exit(-1);/*from w w w . j a v a2 s . c o m*/ } String OutputDirPath = null; try { OutputDirPath = StringEscapeUtils.escapeJava(args[0]); } catch (Exception e) { System.err.println("Argument" + args[1] + " must be an String."); System.exit(1); } int TOTAL_JOBS = 0; try { TOTAL_JOBS = Integer.parseInt(args[1]); } catch (NumberFormatException e) { System.err.println("Argument" + args[1] + " must be an integer."); System.exit(1); } int JOB_NO = 0; try { JOB_NO = Integer.parseInt(args[2]); } catch (NumberFormatException e) { System.err.println("Argument" + args[2] + " must be an integer."); System.exit(1); } int NUMBER_OF_TWEETS = 0; try { NUMBER_OF_TWEETS = Integer.parseInt(args[4]); } catch (NumberFormatException e) { System.err.println("Argument" + args[4] + " must be an integer."); System.exit(1); } MysqlDB DB = new MysqlDB(); AppOAuth AppOAuths = new AppOAuth(); Misc helpers = new Misc(); String endpoint = "/statuses/user_timeline"; try { String totalRowsQuery = "SELECT count( * ) as wLoad FROM `twitter_users`" + " WHERE `tweets_dumped_all` = 0"; ResultSet totalRowsResults = DB.selectQ(totalRowsQuery); int TotalWorkLoad = 0; while (totalRowsResults.next()) { TotalWorkLoad = totalRowsResults.getInt("wLoad"); } if (TotalWorkLoad < 1) { System.err.println("No User in database to get Tweets"); System.exit(-1); } // Free memmory totalRowsResults = null; if (TOTAL_JOBS > TotalWorkLoad) { System.err.println("Number of jobs are more than total work" + " load Please reduce Number of jobs to launch."); System.exit(-1); } float TotalWorkLoadf = TotalWorkLoad; float TOTAL_JOBSf = TOTAL_JOBS; float res = (TotalWorkLoadf / TOTAL_JOBSf); int chunkSize = (int) Math.ceil(res); int offSet = JOB_NO * chunkSize; String selectQuery = "SELECT `id`,`targeteduser` FROM " + "`twitter_users` WHERE " + "`tweets_dumped_all` = 0 LIMIT " + offSet + "," + chunkSize; ResultSet results = DB.selectQ(selectQuery); int numRows = DB.numRows(results); if (numRows < 1) { System.err.println( "No User in database to get Tweets" + " with offset " + offSet + " and limit " + chunkSize); System.exit(-1); } /** * wait before launching actual job */ int secondsToPause = 0; try { secondsToPause = Integer.parseInt(args[3]); } catch (NumberFormatException e) { System.err.println("Argument" + args[3] + " must be an integer."); System.exit(-1); } secondsToPause = (TOTAL_JOBS * secondsToPause) - (JOB_NO * secondsToPause); System.out.println("secondsToPause: " + secondsToPause); helpers.pause(secondsToPause); /** * wait before launching actual job */ TwitterFactory tf = AppOAuths.loadOAuthUser(endpoint, TOTAL_JOBS, JOB_NO); Twitter twitter = tf.getInstance(); int RemainingCalls = AppOAuths.RemainingCalls - 2; int RemainingCallsCounter = 0; System.out.println("First Time Remianing Calls: " + RemainingCalls); String Screen_name = AppOAuths.screen_name; System.out.println("First Time Loaded OAuth Screen_name: " + Screen_name); System.out.println("User's Tweets"); System.out.println("Trying to create output directory"); String filesPath = OutputDirPath + "/"; File theDir = new File(filesPath); // If the directory does not exist, create it if (!theDir.exists()) { try { theDir.mkdirs(); } catch (SecurityException se) { System.err.println("Could not create output " + "directory: " + OutputDirPath); System.err.println(se.getMessage()); System.exit(-1); } } System.out.flush(); OUTERMOST: while (results.next()) { int targetedUserID = results.getInt("id"); String targetedUser = results.getString("targeteduser"); System.out.println("Targeted User: " + targetedUser); // Create User file to push tweets in it String fileName = filesPath + "/" + targetedUser; PrintWriter writer = new PrintWriter(fileName, "UTF-8"); // Call different functions for screen_name and id_str Boolean chckedNumaric = helpers.isNumeric(targetedUser); List<Status> statuses = new ArrayList<>(); int size = statuses.size(); int pageno = 1; int totalTweets = 0; boolean tweetCounterReached = false; System.out.println("NUMBER_OF_TWEETS to get:" + NUMBER_OF_TWEETS); while (true) { try { Paging page = new Paging(pageno++, 200); if (chckedNumaric) { long LongValueTargetedUser = Long.valueOf(targetedUser).longValue(); statuses.addAll(twitter.getUserTimeline(LongValueTargetedUser, page)); if (statuses.size() > 0) { for (Status status : statuses) { String rawJSON = TwitterObjectFactory.getRawJSON(status); writer.println(rawJSON); totalTweets += 1; if (totalTweets >= NUMBER_OF_TWEETS) { tweetCounterReached = true; break; } } if (tweetCounterReached) { break; } } } else { statuses.addAll(twitter.getUserTimeline(targetedUser, page)); if (statuses.size() > 0) { for (Status status : statuses) { String rawJSON = TwitterObjectFactory.getRawJSON(status); writer.println(rawJSON); totalTweets += 1; if (totalTweets >= NUMBER_OF_TWEETS) { tweetCounterReached = true; break; } } if (tweetCounterReached) { break; } } } // If user's total tweet are less // than 195 then no next call if (size == 0) { if (totalTweets < 195) { break; } } // If user's all tweets parsed // then proceed to next user if (totalTweets == size) { break; } size = totalTweets; statuses.clear(); } catch (TwitterException e) { // e.printStackTrace(); // do not throw if user has protected tweets, // or if they deleted their account if (e.getStatusCode() == HttpResponseCode.UNAUTHORIZED || e.getStatusCode() == HttpResponseCode.NOT_FOUND) { System.out.println(targetedUser + " is protected or account is deleted"); } else { System.out.println("Tweets Get Exception: " + e.getMessage()); } // If rate limit reached then switch Auth user RemainingCallsCounter++; if (RemainingCallsCounter >= RemainingCalls) { // Load OAuth user tf = AppOAuths.loadOAuthUser(endpoint, TOTAL_JOBS, JOB_NO); twitter = tf.getInstance(); System.out.println("New User Loaded OAuth" + " Screen_name: " + AppOAuths.screen_name); RemainingCalls = AppOAuths.RemainingCalls - 2; RemainingCallsCounter = 0; System.out.println("New Remianing Calls: " + RemainingCalls); } if (totalTweets < 1) { writer.close(); // Remove file if tweets not found File fileToDelete = new File(fileName); fileToDelete.delete(); } // Update stats String fieldValues = "`tweets_dumped_all` = 2"; String where = "id = " + targetedUserID; DB.Update("`twitter_users`", fieldValues, where); continue OUTERMOST; } // If rate limit reached then switch Auth user RemainingCallsCounter++; if (RemainingCallsCounter >= RemainingCalls) { // Load OAuth user tf = AppOAuths.loadOAuthUser(endpoint, TOTAL_JOBS, JOB_NO); twitter = tf.getInstance(); System.out.println("New User Loaded OAuth Screen_name: " + AppOAuths.screen_name); RemainingCalls = AppOAuths.RemainingCalls - 2; RemainingCallsCounter = 0; System.out.println("New Remianing Calls: " + RemainingCalls); } } // while get tweets writer.close(); if (totalTweets > 0) { System.out.println("Total dumped tweets of " + targetedUser + " are: " + totalTweets); // Update stats String fieldValues = "`tweets_dumped_all` = 1, " + " `tweets_all_count` = " + totalTweets; String where = "id = " + targetedUserID; DB.Update("`twitter_users`", fieldValues, where); } else { // Update stats String fieldValues = "`tweets_dumped_all` = 2, " + " `tweets_all_count` = " + totalTweets; String where = "id = " + targetedUserID; DB.Update("`twitter_users`", fieldValues, where); // Remove file if tweets not found File fileToDelete = new File(fileName); fileToDelete.delete(); } // If rate limit reached then switch Auth user RemainingCallsCounter++; if (RemainingCallsCounter >= RemainingCalls) { // Load OAuth user tf = AppOAuths.loadOAuthUser(endpoint, TOTAL_JOBS, JOB_NO); twitter = tf.getInstance(); System.out.println("New User Loaded OAuth Screen_name: " + AppOAuths.screen_name); RemainingCalls = AppOAuths.RemainingCalls - 2; RemainingCallsCounter = 0; System.out.println("New Remianing Calls: " + RemainingCalls); } System.out.flush(); } // while get users from database } catch (TwitterException te) { // te.printStackTrace(); System.out.println("Failed to get tweets: " + te.getMessage()); System.exit(-1); } System.out.println("!!!! DONE !!!!"); // Close System.out for this thread which will // flush and close this thread. System.out.close(); }
From source file:timeline.FilesThreaderParser.java
License:Apache License
public static void main(String[] args) throws ClassNotFoundException, SQLException, JSONException, IOException { // Check how many arguments were passed in if ((args == null) || (args.length < 7)) { System.err.println("7 Parameters are required to launch a Job."); System.err.println("First: String 'INPUT: " + "/path/to/perline/screen_names/files/'"); System.err.println("Second: String 'OUTPUT_PATH'"); System.err.println("Third: (int) Total Number Of Jobs"); System.err.println("Fourth: (int) This Job Number"); System.err.println("Fifth: (int) Seconds to pause between " + "next launch"); System.err.println("Sixth: (int) Number of Tweets to get. Max 3200"); System.err.println("Name of current threader."); System.err.println("Example: fileName.class /output/path " + "10 2 3200 pool-1-thread-1"); System.exit(-1);//ww w . j a v a 2s. com } String inputPath = null; try { inputPath = StringEscapeUtils.escapeJava(args[0]); } catch (Exception e) { System.err.println("Argument " + args[0] + " must be an String."); System.exit(-1); } String OutputDirPath = null; try { OutputDirPath = StringEscapeUtils.escapeJava(args[1]); } catch (Exception e) { System.err.println("Argument" + args[1] + " must be an String."); System.exit(1); } int TOTAL_JOBS = 0; try { TOTAL_JOBS = Integer.parseInt(args[2]); } catch (NumberFormatException e) { System.err.println("Argument" + args[2] + " must be an integer."); System.exit(1); } int JOB_NO = 0; try { JOB_NO = Integer.parseInt(args[3]); } catch (NumberFormatException e) { System.err.println("Argument" + args[3] + " must be an integer."); System.exit(1); } int NUMBER_OF_TWEETS = 0; try { NUMBER_OF_TWEETS = Integer.parseInt(args[5]); } catch (NumberFormatException e) { System.err.println("Argument" + args[5] + " must be an integer."); System.exit(1); } String ThreadName = null; try { ThreadName = StringEscapeUtils.escapeJava(args[6]); } catch (Exception e) { System.err.println("Argument" + args[6] + " must be an String."); System.exit(1); } AppOAuth AppOAuths = new AppOAuth(); Misc helpers = new Misc(); String endpoint = "/statuses/user_timeline"; try { int TotalWorkLoad = 0; ArrayList<String> allFiles = null; try { final File folder = new File(inputPath); allFiles = helpers.listFilesForSingleFolder(folder); TotalWorkLoad = allFiles.size(); } catch (Exception e) { System.err.println("Input folder is not exists: " + e.getMessage()); System.exit(-1); } System.out.println("Total Workload is: " + TotalWorkLoad); if (TotalWorkLoad < 1) { System.err.println("No input file exists in: " + inputPath); System.exit(-1); } if (TOTAL_JOBS > TotalWorkLoad) { System.err.println("Number of jobs are more than total work" + " load. Please reduce 'Number of jobs' to launch."); System.exit(-1); } float TotalWorkLoadf = TotalWorkLoad; float TOTAL_JOBSf = TOTAL_JOBS; float res = (TotalWorkLoadf / TOTAL_JOBSf); int chunkSize = (int) Math.ceil(res); int offSet = JOB_NO * chunkSize; int chunkSizeToGet = (JOB_NO + 1) * chunkSize; System.out.println("My Share is " + chunkSize); System.out.println("My offSet is " + offSet); System.out.println("My chunkSizeToGet is " + chunkSizeToGet); System.out.println(); /** * wait before launching actual job */ int secondsToPause = 0; try { secondsToPause = Integer.parseInt(args[4]); } catch (NumberFormatException e) { System.err.println("Argument" + args[4] + " must be an integer."); System.exit(-1); } secondsToPause = (TOTAL_JOBS * secondsToPause) - (JOB_NO * secondsToPause); System.out.println("secondsToPause: " + secondsToPause); helpers.pause(secondsToPause); /** * wait before launching actual job */ TwitterFactory tf = AppOAuths.loadOAuthUser(endpoint, TOTAL_JOBS, JOB_NO); Twitter twitter = tf.getInstance(); int RemainingCalls = AppOAuths.RemainingCalls - 2; int RemainingCallsCounter = 0; System.out.println("First Time Remianing Calls: " + RemainingCalls); String Screen_name = AppOAuths.screen_name; System.out.println("First Time Loaded OAuth Screen_name: " + Screen_name); System.out.println("User's Tweets"); System.out.println("Trying to create output directory"); int dirCounter = 1; int fileCounter = 0; String filesPath = OutputDirPath + "/" + ThreadName + "-tweets-" + dirCounter; File theDir = new File(filesPath); // If the directory does not exist, create it if (!theDir.exists()) { try { theDir.mkdirs(); } catch (SecurityException se) { System.err.println("Could not create output " + "directory: " + OutputDirPath + "/" + ThreadName + "-tweets-" + dirCounter); System.err.println(se.getMessage()); System.exit(-1); } } if (JOB_NO + 1 == TOTAL_JOBS) { chunkSizeToGet = TotalWorkLoad; } // to write output in a file System.out.flush(); List<String> myFilesShare = allFiles.subList(offSet, chunkSizeToGet); for (String myFile : myFilesShare) { System.out.println("Going to parse file: " + myFile); System.out.println(); try (BufferedReader br = new BufferedReader(new FileReader(inputPath + "/" + myFile))) { String line; OUTERMOST: while ((line = br.readLine()) != null) { if (fileCounter >= 30000) { dirCounter++; fileCounter = 0; filesPath = OutputDirPath + "/" + ThreadName + "-tweets-" + dirCounter; theDir = new File(filesPath); // If the directory does not exist, create it if (!theDir.exists()) { try { theDir.mkdirs(); } catch (SecurityException se) { System.err.println("Could not create output " + "directory: " + OutputDirPath + "/" + ThreadName + "-tweets-" + dirCounter); System.err.println(se.getMessage()); System.exit(-1); } } } System.out.println("Going to get tweets of " + "Screen-name / user_id: " + line); String targetedUser = line.trim(); System.out.println("Targeted User: " + targetedUser); // Create User file to push tweets in it String fileName = filesPath + "/" + targetedUser; PrintWriter writer = new PrintWriter(fileName, "UTF-8"); // Call different functions for screen_name and id_str Boolean chckedNumaric = helpers.isNumeric(targetedUser); List<Status> statuses = new ArrayList<>(); int size = statuses.size(); int pageno = 1; int totalTweets = 0; boolean tweetCounterReached = false; System.out.println("NUMBER_OF_TWEETS to get:" + NUMBER_OF_TWEETS); System.out.println(); while (true) { try { Paging page = new Paging(pageno++, 200); if (chckedNumaric) { long LongValueTargetedUser = Long.valueOf(targetedUser).longValue(); statuses.addAll(twitter.getUserTimeline(LongValueTargetedUser, page)); if (statuses.size() > 0) { for (Status status : statuses) { String rawJSON = TwitterObjectFactory.getRawJSON(status); writer.println(rawJSON); totalTweets += 1; if (totalTweets >= NUMBER_OF_TWEETS) { tweetCounterReached = true; break; } } if (tweetCounterReached) { break; } } } else { statuses.addAll(twitter.getUserTimeline(targetedUser, page)); if (statuses.size() > 0) { for (Status status : statuses) { String rawJSON = TwitterObjectFactory.getRawJSON(status); writer.println(rawJSON); totalTweets += 1; if (totalTweets >= NUMBER_OF_TWEETS) { tweetCounterReached = true; break; } } if (tweetCounterReached) { break; } } } // If user's total tweet are less // than 195 then no next call if (size == 0) { if (totalTweets < 195) { break; } } // If user's all tweets parsed // then proceed to next user if (totalTweets == size) { break; } size = totalTweets; statuses.clear(); } catch (TwitterException e) { // e.printStackTrace(); // do not throw if user has protected tweets, // or if they deleted their account if (e.getStatusCode() == HttpResponseCode.UNAUTHORIZED || e.getStatusCode() == HttpResponseCode.NOT_FOUND) { System.out.println(targetedUser + " is protected or account is deleted"); } else { System.out.println("Tweets Get Exception: " + e.getMessage()); } // If rate limit reached then switch Auth user RemainingCallsCounter++; if (RemainingCallsCounter >= RemainingCalls) { // Load OAuth user tf = AppOAuths.loadOAuthUser(endpoint, TOTAL_JOBS, JOB_NO); twitter = tf.getInstance(); System.out.println( "New User Loaded OAuth" + " Screen_name: " + AppOAuths.screen_name); RemainingCalls = AppOAuths.RemainingCalls - 2; RemainingCallsCounter = 0; System.out.println("New Remianing Calls: " + RemainingCalls); } if (totalTweets < 1) { writer.close(); // Remove file if tweets not found File fileToDelete = new File(fileName); fileToDelete.delete(); } continue OUTERMOST; } // If rate limit reached then switch Auth user RemainingCallsCounter++; if (RemainingCallsCounter >= RemainingCalls) { // Load OAuth user tf = AppOAuths.loadOAuthUser(endpoint, TOTAL_JOBS, JOB_NO); twitter = tf.getInstance(); System.out.println("New User Loaded OAuth Screen_name: " + AppOAuths.screen_name); RemainingCalls = AppOAuths.RemainingCalls - 2; RemainingCallsCounter = 0; System.out.println("New Remianing Calls: " + RemainingCalls); } } // while get tweets writer.close(); if (totalTweets > 0) { fileCounter++; System.out.println("Total dumped tweets of " + targetedUser + " are: " + totalTweets); } else { // Remove file if tweets not found File fileToDelete = new File(fileName); fileToDelete.delete(); } // If rate limit reached then switch Auth user RemainingCallsCounter++; if (RemainingCallsCounter >= RemainingCalls) { // Load OAuth user tf = AppOAuths.loadOAuthUser(endpoint, TOTAL_JOBS, JOB_NO); twitter = tf.getInstance(); System.out.println("New User Loaded OAuth Screen_name: " + AppOAuths.screen_name); RemainingCalls = AppOAuths.RemainingCalls - 2; RemainingCallsCounter = 0; System.out.println("New Remianing Calls: " + RemainingCalls); } } // while get users } // read my single file catch (IOException e) { System.err.println("Failed to read lines from " + myFile); } File currentFile = new File(inputPath + "/" + myFile); currentFile.delete(); // to write output in a file System.out.flush(); } // all my files share } catch (TwitterException te) { // te.printStackTrace(); System.out.println("Failed to get tweets: " + te.getMessage()); System.exit(-1); } System.out.println("!!!! DONE !!!!"); // Close System.out for this thread which will // flush and close this thread. System.out.close(); }