List of usage examples for twitter4j Query lang
String lang
To view the source code for twitter4j Query lang.
Click Source Link
From source file:collector.TwitterCollector.java
public LinkedHashSet<Tweet> search(String queryExpression, int maxResults) { Query query = new Query(queryExpression); int numberOfTweets = maxResults;//512; long lastID = Long.MAX_VALUE; query = query.lang("pt"); List<Status> tweets = new ArrayList<>(); boolean finish = false; while ((tweets.size() < numberOfTweets) && !finish) { System.out.print("."); if (numberOfTweets - tweets.size() > 100) {//100) { query.setCount(100);//100); } else {//w w w . jav a2 s. c o m query.setCount(numberOfTweets - tweets.size()); } try { QueryResult result = twitter.search(query); List<Status> resultList = result.getTweets(); if (resultList == null || resultList.isEmpty()) { finish = true; System.out.println("no foram encontrados mais tweets"); } else { tweets.addAll(resultList); for (Status t : tweets) { if (t.getId() < lastID) { lastID = t.getId(); } } } } catch (TwitterException ex) { System.err.println(ex.getMessage()); } query.setMaxId(lastID - 1); } LinkedHashSet<Tweet> out = new LinkedHashSet<>(); for (Status status : tweets) { if (!status.getText().startsWith("RT")) { TwitterUser user; user = new TwitterUser().addID(status.getUser().getId()).addName(status.getUser().getName()) .addLocation(status.getUser().getLocation()).addDateSignin(status.getUser().getCreatedAt()) .addCountTweets(status.getUser().getStatusesCount()) .addCountFavorites(status.getUser().getFavouritesCount()) .addCountFriends(status.getUser().getFriendsCount()) .addCountFollowers(status.getUser().getFollowersCount()); Tweet tweet = new Tweet().addUser(user).addText(status.getText()).addID(status.getId()) .addDate(status.getCreatedAt()) .addLatitude(status.getGeoLocation() != null ? status.getGeoLocation().getLatitude() : Double.MAX_VALUE) .addLongitude(status.getGeoLocation() != null ? status.getGeoLocation().getLongitude() : Double.MAX_VALUE); out.add(tweet); } } return out; }
From source file:com.ontotext.s4.TwitterVisualization.downloadTweets.SearchTweets.java
License:Open Source License
/** * This method executes search query on Twitter. The results are saved into the preconfigured result folder. * @param queryString//from w w w . j a v a2 s . com * the search query string. For more information refer to https://dev.twitter.com/docs/using-search * @throws TwitterException * any problem related to authentication, communication, search, etc. */ public void search(String queryString) throws TwitterException { // configures twitter API keys Twitter twitter = ConfigurateTwitterAccount(); // preparint the query Query query = new Query(queryString); // search in both popular and recent tweets query.resultType(Query.MIXED); // restrict result to English language query.lang("en"); // result of the query QueryResult result; do { logger.debug(query); // executing the query result = twitter.search(query); // collect all tweets available List<Status> tweets = result.getTweets(); // save each tweet into a file for (Status tweet : tweets) { saveTweetIntoFile(tweet); } } // handle the paging of the results while ((query = result.nextQuery()) != null); }
From source file:controllers.modules.CorpusModule.java
License:Open Source License
public static Result update(UUID corpus) { OpinionCorpus corpusObj = null;/*from www . jav a2 s .co m*/ if (corpus != null) { corpusObj = fetchResource(corpus, OpinionCorpus.class); } OpinionCorpusFactory corpusFactory = null; MultipartFormData formData = request().body().asMultipartFormData(); if (formData != null) { // if we have a multi-part form with a file. if (formData.getFiles() != null) { // get either the file named "file" or the first one. FilePart filePart = ObjectUtils.defaultIfNull(formData.getFile("file"), Iterables.getFirst(formData.getFiles(), null)); if (filePart != null) { corpusFactory = (OpinionCorpusFactory) new OpinionCorpusFactory().setFile(filePart.getFile()) .setFormat(FilenameUtils.getExtension(filePart.getFilename())); } } } else { // otherwise try as a json body. JsonNode json = request().body().asJson(); if (json != null) { OpinionCorpusFactoryModel optionsVM = Json.fromJson(json, OpinionCorpusFactoryModel.class); if (optionsVM != null) { corpusFactory = optionsVM.toFactory(); } else { throw new IllegalArgumentException(); } if (optionsVM.grabbers != null) { if (optionsVM.grabbers.twitter != null) { if (StringUtils.isNotBlank(optionsVM.grabbers.twitter.query)) { TwitterFactory tFactory = new TwitterFactory(); Twitter twitter = tFactory.getInstance(); twitter.setOAuthConsumer( Play.application().configuration().getString("twitter4j.oauth.consumerKey"), Play.application().configuration().getString("twitter4j.oauth.consumerSecret")); twitter.setOAuthAccessToken(new AccessToken( Play.application().configuration().getString("twitter4j.oauth.accessToken"), Play.application().configuration() .getString("twitter4j.oauth.accessTokenSecret"))); Query query = new Query(optionsVM.grabbers.twitter.query); query.count(ObjectUtils.defaultIfNull(optionsVM.grabbers.twitter.limit, 10)); query.resultType(Query.RECENT); if (StringUtils.isNotEmpty(corpusFactory.getLanguage())) { query.lang(corpusFactory.getLanguage()); } else if (corpusObj != null) { query.lang(corpusObj.getLanguage()); } QueryResult qr; try { qr = twitter.search(query); } catch (TwitterException e) { throw new IllegalArgumentException(); } StringBuilder tweets = new StringBuilder(); for (twitter4j.Status status : qr.getTweets()) { // quote for csv, normalize space, and remove higher unicode characters. String text = StringEscapeUtils.escapeCsv(StringUtils .normalizeSpace(status.getText().replaceAll("[^\\u0000-\uFFFF]", ""))); tweets.append(text + System.lineSeparator()); } corpusFactory.setContent(tweets.toString()); corpusFactory.setFormat("txt"); } } } } else { // if not json, then just create empty. corpusFactory = new OpinionCorpusFactory(); } } if (corpusFactory == null) { throw new IllegalArgumentException(); } if (corpus == null && StringUtils.isEmpty(corpusFactory.getTitle())) { corpusFactory.setTitle("Untitled corpus"); } corpusFactory.setOwnerId(SessionedAction.getUsername(ctx())).setExistingId(corpus).setEm(em()); DocumentCorpusModel corpusVM = null; corpusObj = corpusFactory.create(); if (!em().contains(corpusObj)) { em().persist(corpusObj); corpusVM = (DocumentCorpusModel) createViewModel(corpusObj); corpusVM.populateSize(em(), corpusObj); return created(corpusVM.asJson()); } for (PersistentObject obj : corpusObj.getDocuments()) { if (em().contains(obj)) { em().merge(obj); } else { em().persist(obj); } } em().merge(corpusObj); corpusVM = (DocumentCorpusModel) createViewModel(corpusObj); corpusVM.populateSize(em(), corpusObj); return ok(corpusVM.asJson()); }
From source file:crawler.TwitterFeed.java
/** * *///from w w w . ja va2 s .co m public void get_tweets() throws SQLException { CSVReader cr = new CSVReader(); ArrayList<String> names = new ArrayList<>(); Map<String, String> name_ticker; names = cr.get_company_names(tickers); System.out.println(names); ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setOAuthConsumerKey("WNSycI2GS33ZAwHJ9Fb4A"); cb.setOAuthConsumerSecret("BBWeSThT3ZV2g9c5BDuUduDyNAWyzouMI0XjQy7KUc"); cb.setOAuthAccessToken("1852271029-SLfE061bImfcRxWQZpy1pAgpEkfFhg3757Q9JRf"); cb.setOAuthAccessTokenSecret("1uPSfYPbaENtXFYWsryBIAHnUsmG3pT6lGH0NzxTCGW7O"); Twitter twitter = new TwitterFactory(cb.build()).getInstance(); for (String s : names) { String[] temp; if (s.contains(",")) { temp = s.split(","); } else { temp = s.split(" "); } System.out.println( "--------------------------------------------------------------------------------------" + temp[0]); Query query = new Query("#" + temp[0]); query.lang("en"); int numberOfTweets = 500; long lastID = Long.MAX_VALUE; ArrayList<Status> tweets = new ArrayList<>(); while (tweets.size() < numberOfTweets) { if (numberOfTweets - tweets.size() > 100) { query.setCount(100); } else { query.setCount(numberOfTweets - tweets.size()); } try { QueryResult result = twitter.search(query); tweets.addAll(result.getTweets()); System.out.println("Gathered " + tweets.size() + " tweets"); for (Status t : tweets) { if (t.getId() < lastID) { lastID = t.getId(); } } } catch (TwitterException te) { System.out.println("Couldn't connect: " + te); } query.setMaxId(lastID - 1); } Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/CrawledData", "root", ""); for (int i = 0; i < tweets.size(); i++) { Status t = (Status) tweets.get(i); //GeoLocation loc = t.getGeoLocation(); String user = t.getUser().getScreenName(); String msg = t.getText(); String time = t.getCreatedAt().toString(); //if (loc!=null) { // Double lat = t.getGeoLocation().getLatitude(); // Double lon = t.getGeoLocation().getLongitude(); // System.out.println(i + " USER: " + user + " wrote: " + msg + " located at " + lat + ", " + lon); //} //else{ System.out.println(i + " USER: " + user + " wrote: " + msg + " at: " + time); //} try { Class.forName("com.mysql.jdbc.Driver"); PreparedStatement ps = con .prepareStatement("INSERT IGNORE INTO TwitterFeed VALUES (?,?,?,?,?)"); ps.setString(1, s); ps.setString(2, cr.get_ticker_from_company(s)); ps.setString(3, user); ps.setString(4, msg); ps.setString(5, time); int k = ps.executeUpdate(); if (k <= 0) { System.out.println("Entry Unsuccessful"); } else { System.out.println("Entry Successful"); } } catch (ClassNotFoundException | SQLException e) { System.out.println(e); } } con.close(); } }