List of usage examples for twitter4j Query setCount
public void setCount(int count)
From source file:twitterrest.SearchTweet.java
License:Apache License
public static void main(String[] args) throws TwitterException { //?/*from w w w . jav a2s. c om*/ Configuration configuration = new ConfigurationBuilder().setOAuthConsumerKey(CONSUMER_KEY) .setOAuthConsumerSecret(CONSUMER_SECRET).setOAuthAccessToken(ACCESS_TOKEN) .setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET).build(); Twitter twitter = new TwitterFactory(configuration).getInstance(); Query query = new Query(); try { File file = new File("./file/tweets.txt"); PrintWriter pw; //??? pw = new PrintWriter(new BufferedWriter(new FileWriter(file, true))); // ??? query.setQuery("?"); query.setLang("ja"); // 1????Tweet?100? query.setCount(100); //????????? //query.setSince("2013-06-13"); //query.setUntil("2013-06-17"); // 150015 for (int i = 1; i <= 15; i++) { // QueryResult result = twitter.search(query); System.out.println(": " + result.getTweets().size()); System.out.println(" : " + new Integer(i).toString()); // ??? for (Status tweet : result.getTweets()) { // String str = tweet.getText(); System.out.println(str); // System.out.println(tweet.getUser()); // System.out.println(tweet.getCreatedAt()); // ??URL? StringTokenizer sta = new StringTokenizer(str, " "); //? while (sta.hasMoreTokens()) { String wk = sta.nextToken(); if (wk.indexOf("#") == -1 && wk.indexOf("http") == -1 && wk.indexOf("RT") == -1 && wk.indexOf("@") == -1) { pw.println(wk); } } String u = tweet.getUser().getName(); pw.println(u); } if (result.hasNext()) { query = result.nextQuery(); } else { break; } } pw.close(); } catch (IOException e) { e.printStackTrace(); } }
From source file:twitterrest.UserSearch.java
License:Apache License
public static void main(String[] args) throws TwitterException { //?// www . j av a2 s.c o m Configuration configuration = new ConfigurationBuilder().setOAuthConsumerKey(CONSUMER_KEY) .setOAuthConsumerSecret(CONSUMER_SECRET).setOAuthAccessToken(ACCESS_TOKEN) .setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET).build(); Twitter twitter = new TwitterFactory(configuration).getInstance(); Query query = new Query(); // masason?Tweet //query.setQuery("from:anondroid3 OR to:anondroid3"); query.setQuery("from:anondroid3"); // ??? QueryResult result = twitter.search(query); // System.out.println(":" + result.getTweets().size()); // 1????Tweet?100? query.setCount(100); for (Status tweet : result.getTweets()) { System.out.println("tweet:" + tweet.getText());// System.out.println("UserID:" + tweet.getUser().getId());//ID System.out.println("Application:" + tweet.getSource());// System.out.println("Created Date:" + tweet.getCreatedAt());//? System.out.println("GeoLocation:" + tweet.getGeoLocation());// } }
From source file:twittersentimentanalysis.TwitterSentimentAnalysis.java
private static ArrayList<Tweet> getAllTweets(Twitter twitter) { logger.info("getAllTweets"); List<Status> listTweets = null; ArrayList<Tweet> listOfTweets = new ArrayList<Tweet>(); try {//from ww w . ja va 2s. c om Trends trends = twitter.getPlaceTrends(23424977); // System.out.println(trends.getLocation()); Trend trend[] = trends.getTrends(); logger.info("Number of Trends : " + trend.length); for (Trend trendTemp : trend) { //System.out.println("Name = " + trendTemp.getName()); //System.out.println("***************"); Query query = new Query(trendTemp.getQuery()); query.setCount(100); QueryResult queryResult = twitter.search(query); listTweets = queryResult.getTweets(); StanfordCoreNLPTool.init(); for (Status status : listTweets) { //System.out.println(status.getText()); Tweet tweet = getTweetObject(status); if (tweet != null) { tweet.setTrend(trendTemp.getName()); listOfTweets.add(tweet); } } } } catch (TwitterException ex) { Logger.getLogger(TwitterSentimentAnalysis.class.getName()).log(Level.SEVERE, null, ex); logger.info(ex.getMessage()); } return listOfTweets; }
From source file:TwitterStats.Facade.Twitter.java
public List<Status> getTuitsCuenta(String user, Date desde, Date hasta, int cantidad) throws TwitterException { SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd"); Query q = new Query("from:" + user + " since:" + dt.format(desde) + " until:" + dt.format(hasta)); q.setCount(100); List<Status> lista = new ArrayList<>(); List<Status> res = twitter.search(q).getTweets(); while (res.size() > 1 && lista.size() < 3000) { lista.addAll(res);// ww w . j a v a 2s .c o m q.setMaxId(res.get(res.size() - 1).getId()); res = twitter.search(q).getTweets(); } Collections.sort(lista, new Comparator<Status>() { public int compare(Status s1, Status s2) { return s1.getFavoriteCount() > s2.getFavoriteCount() ? -1 : (s1.getFavoriteCount() < s2.getFavoriteCount()) ? 1 : 0; } }); if (lista.size() > cantidad) { return lista.subList(0, cantidad); } else { return lista; } }
From source file:TwitterStats.Facade.Twitter.java
public Map<String, Integer> getTendencias(String user, Date desde, Date hasta) throws TwitterException { Map<String, Integer> tendencias = new HashMap<>(); SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd"); Query q = new Query("from:" + user + " since:" + dt.format(desde) + " until:" + dt.format(hasta)); q.setCount(100); List<Status> lista = new ArrayList<>(); List<Status> res = twitter.search(q).getTweets(); while (res.size() > 1 && lista.size() < 3000) { lista.addAll(res);// w ww .j a v a2 s .c om q.setMaxId(res.get(res.size() - 1).getId()); res = twitter.search(q).getTweets(); } for (Status status : lista) { HashtagEntity[] ht = status.getHashtagEntities(); for (int i = 0; i < ht.length; i++) { String hash = ht[i].getText(); if (tendencias.containsKey(hash)) { tendencias.put(hash, tendencias.get(hash) + 1); } else { tendencias.put(hash, 1); } } } return sortByValue(tendencias); }
From source file:twittertestingagain.TwitterTesting.java
/** * @param args the command line arguments * @throws twitter4j.TwitterException/*from w w w .j a va 2s . c o m*/ * @throws java.io.IOException */ public static void main(String[] args) throws TwitterException, IOException { /* ---------------------------Setting up twitter account authentication-------------------------------*/ ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey("YjICBJeNlnxAf3tFw7awLaCzS") .setOAuthConsumerSecret("8IfPzkr4opePnhCLLloKMP6X44IeNav0fLDrmtBrPbaHoxd1nO") .setOAuthAccessToken("4146680697-oOEPVezvvZ82vB7iP9HSbkoTG9ze9gH69XLrSCP") .setOAuthAccessTokenSecret("HZjsaabmVjeSkSX6vvVFdT3GWZek8xJ9RKfwaR57RDyEG"); /* ---------------------------------File Writing Variables------------------------------------------------*/ File outfile = new File("output.txt"); FileWriter fwriter = new FileWriter(outfile); try (PrintWriter pWriter = new PrintWriter(fwriter)) { /*----------------------------------Search Parameters-------------------------------------*/ String search = "chinese food"; String lang = "en"; /*------------------------End Search Parameters----------------------------------------*/ int numTweets = 0; long maxID = -1; TwitterFactory tf = new TwitterFactory(cb.build()); Twitter twitter = tf.getInstance(); try { Map<String, RateLimitStatus> rateLimitStatus = twitter.getRateLimitStatus("search"); //System.out.println(rateLimitStatus); RateLimitStatus searchTweetsRateLimit = rateLimitStatus.get("/search/tweets"); /*System.out.printf("You have %d calls remaining out of %d, Limit resets in %d seconds\n", searchTweetsRateLimit.getRemaining(), searchTweetsRateLimit.getLimit(), searchTweetsRateLimit.getSecondsUntilReset()); */ for (int queryNumber = 0; queryNumber < maxQueries; queryNumber++) { System.out.printf("\n\n!!! Starting loop %d\n\n", queryNumber); pWriter.println("\n\n!!! Starting iteration #" + queryNumber + "\n\n"); if (searchTweetsRateLimit.getRemaining() == 0) { System.out.printf("!!! Sleeping for %d seconds due to rate limits\n", searchTweetsRateLimit.getSecondsUntilReset()); Thread.sleep((searchTweetsRateLimit.getSecondsUntilReset() + 2) * 1000l); } //here is where we can send an object to the query Query query = new Query(search); query.setCount(tweetsPerQuery); query.resultType(Query.ResultType.recent); query.setLang(lang); if (maxID != -1) { query.setMaxId(maxID - 1); } QueryResult result = twitter.search(query); if (result.getTweets().size() == 0) { break; } for (Status s : result.getTweets()) { numTweets++; if (maxID == -1 || s.getId() < maxID) { maxID = s.getId(); } System.out.printf("On %s, @%-20s said: %s\n", s.getCreatedAt().toString(), s.getUser().getScreenName(), cleanText(s.getText())); pWriter.println("On " + s.getCreatedAt().toString() + " @" + s.getUser().getScreenName() + " " + cleanText(s.getText())); } searchTweetsRateLimit = result.getRateLimitStatus(); } } catch (Exception e) { System.out.println("Broken"); e.printStackTrace(); } System.out.printf("\n\nA total of %d tweets retrieved\n", numTweets); pWriter.println("\n\nA total of " + numTweets + " tweets retrieved\n\n"); pWriter.close(); /*while (result.hasNext()){ numTweets += result.getCount(); System.out.println(numTweets); for (Status status : result.getTweets()) { System.out.println("@" + status.getUser().getScreenName() + ":" + status.getText()); } result.nextQuery(); } /* QueryForm qf = new QueryForm(); qf.show(); */ } }
From source file:uk.ac.susx.tag.method51.twitter.params.QueryParams.java
License:Apache License
public Query buildQuery() throws SQLException { List<String> keywords = getKeywords(); if (keywords.size() == 0 && getLocation().size() == 0) { throw new RuntimeException("No keywords or locations provided"); }/*from w ww .jav a2s. c o m*/ final String q; if (keywords.size() > 1) { q = "\"" + StringUtils.join(keywords, "\" OR \"") + "\""; } else { q = keywords.get(0); } //LOG.info("Searching keywords: {}", q); //LOG.info("Query length: {}", q.length()); Query query = new Query(q); query.setCount(100); query.setResultType(Query.RECENT); if (getAcceptedLanguages().size() > 0) { query.setLang(StringUtils.join(getAcceptedLanguages(), ",")); } if (getUntilId() != null) { query.setMaxId(getUntilId()); } if (getSinceId() != null) { query.setSinceId(getSinceId()); } else if (getSinceMaxId()) { try (Connection con = dbParams.get().buildConnection()) { long maxId = Util.getMaxID(con, getMysqlTweetOutputTable()); query.setSinceId(maxId); LOG.info("Determined {} as max id, searching since that.", maxId); } } if (getSinceId() == null && getSinceDate() != null) { long approxSinceId = Util.getMinSnowflakeId(getSinceDate()); query.setSinceId(approxSinceId); LOG.info("Determined min Snowflake ID {}", approxSinceId); } if (getUntilId() == null && getUntilDate() != null) { long approxUntilId = Util.getMaxSnowflakeId(getUntilDate()); query.setMaxId(approxUntilId); LOG.info("Determined max Snowflake ID {}", approxUntilId); } if (getLocation().size() == 1) { try { String[] geo = getLocation().get(0).split("\\|"); query.setGeoCode(new GeoLocation(Double.parseDouble(geo[0]), Double.parseDouble(geo[1])), Double.parseDouble(geo[2]), Query.KILOMETERS); } catch (NumberFormatException e) { throw new IllegalArgumentException("invalid location param " + getLocation().get(0), e); } } return query; }
From source file:uta.ak.CollectTweets.java
public void collectTweetsByFileList(String sinceDate, String untilDate, String tag) { try {//from w w w . ja va 2 s .co m // The factory instance is re-useable and thread safe. System.out.println("Start to collect tweets from :"); Set<String> mediaList = new HashSet<String>(); Resource res = new ClassPathResource("new-social-meida-list.txt"); // File stopwords=res.getFile(); // File stopwords=new File("/Users/zhangcong/dev/corpus/StopWordTable2.txt"); InputStreamReader isr = new InputStreamReader(res.getInputStream()); // File medias=new File(path); // BufferedReader mdsreader = new BufferedReader(new FileReader(medias)); BufferedReader mdsreader = new BufferedReader(isr); String tempString = mdsreader.readLine(); while ((tempString = mdsreader.readLine()) != null) { System.out.println(tempString.toLowerCase()); mediaList.add(tempString.toLowerCase()); } ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey("LuhVZOucqdHX6x0lcVgJO6QK3") .setOAuthConsumerSecret("6S7zbGLvHMXDMgRXq7jRIA6QmMpdI8i5IJNpnjlB55vpHpFMpj") .setOAuthAccessToken("861637891-kLunD37VRY8ipAK3TVOA0YKOKxeidliTqMtNb7wf") .setOAuthAccessTokenSecret("vcKDxs6qHnEE8fhIJr5ktDcTbPGql5o3cNtZuztZwPYl4"); TwitterFactory tf = new TwitterFactory(cb.build()); Twitter twitter = tf.getInstance(); /* Connection con = null; //MYSQL Class.forName("com.mysql.jdbc.Driver").newInstance(); //MYSQL con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/USTTMP", "root", "root.123"); //MYSQL System.out.println("connection yes"); String insertSQL="INSERT INTO c_rawtext(mme_lastupdate, mme_updater, title, text, tag, text_createdate) " + "VALUES (NOW(), \"AK\", ?, ?, ?, ?)"; PreparedStatement insertPS = con.prepareStatement(insertSQL); */ //?usttmp?? String restUrl = "http://192.168.0.103:8991/usttmp_textreceiver/rest/addText"; Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, 1); SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); for (String mediastr : mediaList) { Query query = new Query("from:" + mediastr); query.setSince(sinceDate); query.setUntil(untilDate); query.setCount(100); query.setLang("en"); QueryResult result = twitter.search(query); for (Status status : result.getTweets()) { System.out.println("@" + status.getUser().getScreenName() + " | " + status.getCreatedAt().toString() + ":" + status.getText()); System.out.println("Inserting the record into the table..."); String formattedDate = format1.format(status.getCreatedAt()); /* insertPS.setString (1, status.getUser().getScreenName()); insertPS.setString (2, status.getText()); insertPS.setString (3, tag); insertPS.setString (4, formattedDate); insertPS.addBatch();*/ // if(null!=status.getText()){ // break; // } String interfaceMsg = "<message> " + " <title> " + ((null != status.getUser().getScreenName()) ? status.getUser().getScreenName() : "NO TITLE") + " </title> " + " <text> " + StringEscapeUtils.escapeXml10(status.getText()) + " </text> " + " <textCreatetime> " + formattedDate + " </textCreatetime> " + " <tag> " + tag + " </tag> " + "</message>"; // String restUrl="http://127.0.0.1:8991/usttmp_textreceiver/rest/addText"; RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.TEXT_XML); headers.setAccept(Arrays.asList(MediaType.TEXT_XML)); // headers.setContentLength(); HttpEntity<String> entity = new HttpEntity<String>(interfaceMsg, headers); ResponseEntity<String> resresult = restTemplate.exchange(restUrl, HttpMethod.POST, entity, String.class); System.out.println(resresult.getBody()); if (resresult.getBody().contains("<result>failed</result>")) { throw new RuntimeException("response message error"); } } } // System.out.println("Start to insert records..."); // insertPS.clearParameters(); // int[] results = insertPS.executeBatch(); } catch (Exception te) { te.printStackTrace(); System.out.println("Failed: " + te.getMessage()); System.exit(-1); } }
From source file:uta.ak.CollectTweets.java
public void collectTweetsByKeyWords(String keyWords, String sinceDate, String untilDate, String tag) { try {// w w w. ja v a2s . c o m ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey("LuhVZOucqdHX6x0lcVgJO6QK3") .setOAuthConsumerSecret("6S7zbGLvHMXDMgRXq7jRIA6QmMpdI8i5IJNpnjlB55vpHpFMpj") .setOAuthAccessToken("861637891-kLunD37VRY8ipAK3TVOA0YKOKxeidliTqMtNb7wf") .setOAuthAccessTokenSecret("vcKDxs6qHnEE8fhIJr5ktDcTbPGql5o3cNtZuztZwPYl4"); TwitterFactory tf = new TwitterFactory(cb.build()); Twitter twitter = tf.getInstance(); Connection con = null; //MYSQL Class.forName("com.mysql.jdbc.Driver").newInstance(); //MYSQL con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/USTTMP", "root", "root.123"); //MYSQL System.out.println("connection yes"); String insertSQL = "INSERT INTO c_rawtext(mme_lastupdate, mme_updater, title, text, tag, text_createdate) VALUES (NOW(), \"AK\", ?, ?, ?, ?)"; PreparedStatement insertPS = con.prepareStatement(insertSQL); Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, 1); SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd"); Query query = new Query(keyWords); query.setSince(sinceDate); query.setUntil(untilDate); query.setCount(100); query.setLang("en"); QueryResult result = twitter.search(query); for (Status status : result.getTweets()) { // System.out.println("@" + status.getUser().getScreenName() + // " | " + status.getCreatedAt().toString() + // ":" + status.getText()); // System.out.println("Inserting the record into the table..."); String formattedDate = format1.format(status.getCreatedAt()); insertPS.setString(1, status.getUser().getScreenName()); insertPS.setString(2, status.getText()); insertPS.setString(3, tag); insertPS.setString(4, formattedDate); insertPS.addBatch(); } System.out.println("Start to insert records..."); insertPS.clearParameters(); int[] results = insertPS.executeBatch(); } catch (Exception te) { te.printStackTrace(); System.out.println("Failed: " + te.getMessage()); System.exit(-1); } }
From source file:webServices.RestServiceImpl.java
@GET @Path("/findTwittsRest") @Produces({ MediaType.TEXT_PLAIN })//from www . j a v a 2 s. c o m public String twitterSearchRest(@QueryParam("keys") String searchKeys, @QueryParam("sinceId") long sinceId, @QueryParam("maxId") long maxId, @QueryParam("update") boolean update, @QueryParam("location") String location) { final Vector<String> results = new Vector<String>(); String output = ""; long higherStatusId = Long.MIN_VALUE; long lowerStatusId = Long.MAX_VALUE; Query searchQuery = new Query(searchKeys); searchQuery.setCount(50); searchQuery.setResultType(Query.ResultType.recent); if (sinceId != 0) { if (update) { searchQuery.setSinceId(sinceId); } higherStatusId = sinceId; } if (maxId != 0) { if (!update) { searchQuery.setMaxId(maxId); } lowerStatusId = maxId; } if (location != null) { double lat = Double.parseDouble(location.substring(0, location.indexOf(","))); double lon = Double.parseDouble(location.substring(location.indexOf(",") + 1, location.length())); searchQuery.setGeoCode(new GeoLocation(lat, lon), 10, Query.KILOMETERS); } try { QueryResult qResult = twitter.search(searchQuery); for (Status status : qResult.getTweets()) { //System.out.println(Long.toString(status.getId())+" *** "+Long.toString(status.getUser().getId())+" *** "+status.isRetweet()+" *** "+status.isRetweeted()); higherStatusId = Math.max(status.getId(), higherStatusId); lowerStatusId = Math.min(status.getId(), lowerStatusId); if (!status.isRetweet()) { if (status.getGeoLocation() != null) { System.out.println(Long.toString(status.getId()) + "@" + Double.toString(status.getGeoLocation().getLatitude()) + "," + Double.toString(status.getGeoLocation().getLongitude())); results.add(Long.toString(status.getId()) + "@" + Double.toString(status.getGeoLocation().getLatitude()) + "," + Double.toString(status.getGeoLocation().getLongitude())); } else { results.add(Long.toString(status.getId()) + "@null"); } } } } catch (TwitterException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } TwitterResults resultsObj = new TwitterResults(results.toString(), higherStatusId, lowerStatusId); ObjectMapper mapper = new ObjectMapper(); try { output = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(resultsObj); } catch (JsonProcessingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return output; }