List of usage examples for twitter4j Query setSince
public void setSince(String since)
From source file:dhbw.clippinggorilla.external.twitter.TwitterUtils.java
/** * @param includedTagsSet//from w w w.j a v a 2s .co m * @param date * @return */ private static Query queryBuilder(Set<String> includedTagsSet, LocalDateTime date) { List<String> includedTags = new ArrayList<>(); includedTags.addAll(includedTagsSet); String query = ""; if (includedTags.size() > 0) { query = replaceWhitespaces(includedTags.get(0)); for (int i = 1; i < includedTags.size(); i++) { query += " OR " + replaceWhitespaces(includedTags.get(i)); } } //System.out.println(date.toString()); //System.out.println(query); Query result = new Query(query); result.setResultType(Query.ResultType.popular); result.setSince(date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); return result; }
From source file:edu.uml.TwitterDataMining.TwitterConsumer.java
/** * Search twitter with the given search term Results will be saved in a file * with the same name as the query//w ww .j av a2 s. c om * * @param searchTerm The search term to be queried * @return a list of tweets */ public static List<Status> queryTwitter(String searchTerm) { Query query = new Query(searchTerm); query.setLang("en"); // we only want english tweets // get and format date to get tweets no more than a year old SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String date = sdf.format(new Date(System.currentTimeMillis() - (long) (365 * 24 * 60 * 60 * 1000))); query.setSince(date); QueryResult result; List<Status> tweets = null; try { //ONLY GETTING FIRST SET OF RESULTS result = twitter.search(query); tweets = result.getTweets(); // for (Status tweet : tweets) { // System.out.println("@" + tweet.getUser().getScreenName() + "\t" + tweet.getText()); // } // Wait loop for reset } catch (TwitterException te) { try { // try block for sleep thread if (!te.isCausedByNetworkIssue()) { // Not really checking for anything else but it is likely that we are out of requests int resetTime = te.getRateLimitStatus().getSecondsUntilReset(); while (resetTime > 0) { Thread.sleep(1000); // 1 second stop System.out.println("seconds till reset: " + resetTime); --resetTime; } } else { te.printStackTrace(); } } catch (InterruptedException ie) { ie.printStackTrace(); System.exit(-1); } } return tweets; }
From source file:it.greenvulcano.gvesb.social.twitter.directcall.TwitterOperationSearch.java
License:Open Source License
@Override public void execute(SocialAdapterAccount account) throws SocialAdapterException { try {/*w w w .j av a2 s .c om*/ Twitter twitter = (Twitter) account.getProxyObject(); Query q = new Query(); if ((query != null) && !"".equals(query)) { q.setQuery(query); } if ((sinceId != null) && !"".equals(sinceId)) { q.setSinceId(Long.parseLong(sinceId)); } if ((maxId != null) && !"".equals(maxId)) { q.setMaxId(Long.parseLong(maxId)); } if ((since != null) && !"".equals(since)) { q.setSince(since); } if ((until != null) && !"".equals(until)) { q.setUntil(until); } if ((count != null) && !"".equals(count)) { q.setCount(Integer.parseInt(count)); } XMLUtils parser = null; try { parser = XMLUtils.getParserInstance(); doc = parser.newDocument("TwitterQuery"); Element root = doc.getDocumentElement(); parser.setAttribute(root, "user", twitter.getScreenName()); parser.setAttribute(root, "userId", String.valueOf(twitter.getId())); parser.setAttribute(root, "createdAt", DateUtils.nowToString(DateUtils.FORMAT_ISO_DATETIME_UTC)); QueryResult result; do { result = twitter.search(q); List<Status> tweets = result.getTweets(); for (Status tweet : tweets) { dumpTweet(parser, root, tweet); } } while ((q = result.nextQuery()) != null); } catch (Exception exc) { logger.error("Error formatting TwitterOperationSearch query[" + query + "], sinceId[" + sinceId + "], maxId[" + maxId + "], since[" + since + "], until[" + until + "] and count[" + count + "] response.", exc); throw new SocialAdapterException("Error formatting TwitterOperationSearch query[" + query + "], sinceId[" + sinceId + "], maxId[" + maxId + "], since[" + since + "], until[" + until + "] and count[" + count + "] response.", exc); } finally { XMLUtils.releaseParserInstance(parser); } } catch (NumberFormatException exc) { logger.error("Call to TwitterOperationSearch failed. Check query[" + query + "], sinceId[" + sinceId + "], maxId[" + maxId + "], since[" + since + "], until[" + until + "] and count[" + count + "] format.", exc); throw new SocialAdapterException("Call to TwitterOperationSearch failed. Check query[" + query + "], sinceId[" + sinceId + "], maxId[" + maxId + "], since[" + since + "], until[" + until + "] and count[" + count + "] format.", exc); } }
From source file:Jums.AllAPI.java
public int TweetCount(String word) { int i = 0;/*from w w w . j a v a 2 s.c o m*/ try { Query query = new Query(word); query.setCount(100); Date date = this.SetDate(); query.setSince(this.SetSDF()); QueryResult result = this.tw.search(query); for (Status status : result.getTweets()) { if (date.compareTo(status.getCreatedAt()) < 0) { i++; } } } catch (TwitterException te) { System.out.println(te.getMessage()); } catch (Exception e) { System.out.println(e.getMessage()); } return i; }
From source file:org.anc.lapps.datasource.twitter.TwitterDatasource.java
/** * Entry point for a Lappsgrid service.//from w w w . j av a2 s. c o m * <p> * Each service on the Lappsgrid will accept {@code org.lappsgrid.serialization.Data} object * and return a {@code Data} object with a {@code org.lappsgrid.serialization.lif.Container} * payload. * <p> * Errors and exceptions that occur during processing should be wrapped in a {@code Data} * object with the discriminator set to http://vocab.lappsgrid.org/ns/error * <p> * See <a href="https://lapp.github.io/org.lappsgrid.serialization/index.html?org/lappsgrid/serialization/Data.html>org.lappsgrid.serialization.Data</a><br /> * See <a href="https://lapp.github.io/org.lappsgrid.serialization/index.html?org/lappsgrid/serialization/lif/Container.html>org.lappsgrid.serialization.lif.Container</a><br /> * * @param input A JSON string representing a Data object * @return A JSON string containing a Data object with a Container payload. */ @Override public String execute(String input) { Data<String> data = Serializer.parse(input, Data.class); String discriminator = data.getDiscriminator(); // Return ERRORS back if (Discriminators.Uri.ERROR.equals(discriminator)) { return input; } // Generate an error if the used discriminator is wrong if (!Discriminators.Uri.GET.equals(discriminator)) { return generateError( "Invalid discriminator.\nExpected " + Discriminators.Uri.GET + "\nFound " + discriminator); } Configuration config = new ConfigurationBuilder().setApplicationOnlyAuthEnabled(true).setDebugEnabled(false) .build(); // Authentication using saved keys Twitter twitter = new TwitterFactory(config).getInstance(); String key = readProperty(KEY_PROPERTY); if (key == null) { return generateError("The Twitter Consumer Key property has not been set."); } String secret = readProperty(SECRET_PROPERTY); if (secret == null) { return generateError("The Twitter Consumer Secret property has not been set."); } twitter.setOAuthConsumer(key, secret); try { twitter.getOAuth2Token(); } catch (TwitterException te) { String errorData = generateError(te.getMessage()); logger.error(errorData); return errorData; } // Get query String from data payload Query query = new Query(data.getPayload()); // Set the type to Popular or Recent if specified // Results will be Mixed by default. if (data.getParameter("type") == "Popular") query.setResultType(Query.POPULAR); if (data.getParameter("type") == "Recent") query.setResultType(Query.RECENT); // Get lang string String langCode = (String) data.getParameter("lang"); // Verify the validity of the language code and add it to the query if it's valid if (validateLangCode(langCode)) query.setLang(langCode); // Get date strings String sinceString = (String) data.getParameter("since"); String untilString = (String) data.getParameter("until"); // Verify the format of the date strings and set the parameters to query if correctly given if (validateDateFormat(untilString)) query.setUntil(untilString); if (validateDateFormat(sinceString)) query.setSince(sinceString); // Get GeoLocation if (data.getParameter("address") != null) { String address = (String) data.getParameter("address"); double radius = (double) data.getParameter("radius"); if (radius <= 0) radius = 10; Query.Unit unit = Query.MILES; if (data.getParameter("unit") == "km") unit = Query.KILOMETERS; GeoLocation geoLocation; try { double[] coordinates = getGeocode(address); geoLocation = new GeoLocation(coordinates[0], coordinates[1]); } catch (Exception e) { String errorData = generateError(e.getMessage()); logger.error(errorData); return errorData; } query.geoCode(geoLocation, radius, String.valueOf(unit)); } // Get the number of tweets from count parameter, and set it to default = 15 if not specified int numberOfTweets; try { numberOfTweets = (int) data.getParameter("count"); } catch (NullPointerException e) { numberOfTweets = 15; } // Generate an ArrayList of the wanted number of tweets, and handle possible errors. // This is meant to avoid the 100 tweet limit set by twitter4j and extract as many tweets as needed ArrayList<Status> allTweets; Data tweetsData = getTweetsByCount(numberOfTweets, query, twitter); String tweetsDataDisc = tweetsData.getDiscriminator(); if (Discriminators.Uri.ERROR.equals(tweetsDataDisc)) return tweetsData.asPrettyJson(); else { allTweets = (ArrayList<Status>) tweetsData.getPayload(); } // Initialize StringBuilder to hold the final string StringBuilder builder = new StringBuilder(); // Append each Status (each tweet) to the initialized builder for (Status status : allTweets) { String single = status.getCreatedAt() + " : " + status.getUser().getScreenName() + " : " + status.getText() + "\n"; builder.append(single); } // Output results Container container = new Container(); container.setText(builder.toString()); Data<Container> output = new Data<>(Discriminators.Uri.LAPPS, container); return output.asPrettyJson(); }
From source file:org.codice.ddf.catalog.twitter.source.TwitterSource.java
License:Open Source License
@Override public SourceResponse query(QueryRequest request) throws UnsupportedQueryException { Twitter instance = twitterFactory.getInstance(); try {//from ww w. j a v a 2 s .c o m instance.getOAuth2Token(); } catch (TwitterException e) { throw new UnsupportedQueryException("Unable to get OAuth2 token.", e); } TwitterFilterVisitor visitor = new TwitterFilterVisitor(); request.getQuery().accept(visitor, null); Query query = new Query(); query.setCount(request.getQuery().getPageSize()); if (visitor.hasSpatial()) { GeoLocation geoLocation = new GeoLocation(visitor.getLatitude(), visitor.getLongitude()); query.setGeoCode(geoLocation, visitor.getRadius(), Query.Unit.km); } if (visitor.getContextualSearch() != null) { query.setQuery(visitor.getContextualSearch().getSearchPhrase()); } if (visitor.getTemporalSearch() != null) { Calendar.Builder builder = new Calendar.Builder(); builder.setInstant(visitor.getTemporalSearch().getStartDate()); Calendar calendar = builder.build(); query.setSince(calendar.get(Calendar.YEAR) + "-" + calendar.get(Calendar.MONTH) + "-" + calendar.get(Calendar.DAY_OF_MONTH)); builder = new Calendar.Builder(); builder.setInstant(visitor.getTemporalSearch().getEndDate()); calendar = builder.build(); query.setUntil(calendar.get(Calendar.YEAR) + "-" + calendar.get(Calendar.MONTH) + "-" + calendar.get(Calendar.DAY_OF_MONTH)); } QueryResult queryResult; try { queryResult = instance.search().search(query); } catch (TwitterException e) { throw new UnsupportedQueryException(e); } List<Result> resultList = new ArrayList<>(queryResult.getCount()); resultList.addAll(queryResult.getTweets().stream().map(status -> new ResultImpl(getMetacard(status))) .collect(Collectors.toList())); return new SourceResponseImpl(request, resultList); }
From source file:org.kuali.mobility.conference.controllers.ConferenceController.java
License:Open Source License
@RequestMapping(value = "twitter-search", method = RequestMethod.GET) public ResponseEntity<String> twitterSearch(@RequestParam(value = "term", required = true) String searchParam, @RequestParam(value = "since", required = false) String sinceParam, HttpServletRequest request) { Twitter twitter = TwitterFactory.getSingleton(); Query query = new Query(searchParam.isEmpty() ? "#kualidays" : searchParam); QueryResult result = null;//from w w w . j a v a2 s .co m query.setSince(!sinceParam.isEmpty() ? sinceParam : "2014-01-01"); query.setCount(100); query.setResultType(Query.MIXED); String json = ""; List<String> tweetList = new ArrayList<String>(); try { result = twitter.search(query); } catch (TwitterException e) { System.err.println("Caught 'twitterSearch' IOException: " + e.getMessage()); } String tweetInfo = ""; for (Status status : result.getTweets()) { tweetInfo = "{"; tweetInfo += "\"id\" : \"" + status.getId() + "\", "; tweetInfo += "\"avatar\" : \"" + status.getUser().getProfileImageURL() + "\", "; tweetInfo += "\"tweetedOn\" : \"" + status.getCreatedAt() + "\", "; tweetInfo += "\"tweetedOnParsed\" : \"" + status.getCreatedAt().getTime() + "\", "; tweetInfo += "\"screenname\" : \"" + status.getUser().getScreenName() + "\", "; tweetInfo += "\"status\" : \"" + StringEscapeUtils.escapeHtml(status.getText().replaceAll("(\\r|\\n)", "")) + "\", "; tweetInfo += "\"truncated\" : \"" + (status.isTruncated() ? "true" : "false") + "\","; tweetInfo += "\"retweeted\" : \"" + (status.isRetweet() ? "true" : "false") + "\""; tweetInfo += "}"; tweetList.add(tweetInfo); } json = "[" + StringUtils.arrayToDelimitedString(tweetList.toArray(), ",") + "]"; return new ResponseEntity<String>(json, HttpStatus.OK); }
From source file:org.mule.twitter.TwitterConnector.java
License:Open Source License
/** * Returns tweets that match a specified query. * <p/>// w ww . ja v a 2 s .c o m * This method calls http://search.twitter.com/search.json * <p/> * {@sample.xml ../../../doc/twitter-connector.xml.sample twitter:search} * * @param query The search query. * @param lang Restricts tweets to the given language, given by an <a href="http://en.wikipedia.org/wiki/ISO_639-1">ISO 639-1 code</a> * @param locale Specify the language of the query you are sending (only ja is currently effective). This is intended for language-specific clients and the default should work in the majority of cases. * @param maxId If specified, returns tweets with status ids less than the given id * @param since If specified, returns tweets since the given date. Date should be formatted as YYYY-MM-DD * @param sinceId Returns tweets with status ids greater than the given id. * @param geocode A {@link String} containing the latitude and longitude separated by ','. Used to get the tweets by users located within a given radius of the given latitude/longitude, where the user's location is taken from their Twitter profile * @param radius The radius to be used in the geocode -ONLY VALID IF A GEOCODE IS GIVEN- * @param unit The unit of measurement of the given radius. Can be 'mi' or 'km'. Miles by default. * @param until If specified, returns tweets with generated before the given date. Date should be formatted as YYYY-MM-DD * @param resultType If specified, returns tweets included popular or real time or both in the responce. Both by default. Can be 'mixed', 'popular' or 'recent'. * @return the {@link QueryResult} * @throws TwitterException when Twitter service or network is unavailable */ @Processor public QueryResult search(String query, @Optional String lang, @Optional String locale, @Optional Long maxId, @Optional String since, @Optional Long sinceId, @Optional String geocode, @Optional String radius, @Default(value = Query.MILES) @Optional String unit, @Optional String until, @Optional String resultType) throws TwitterException { final Query q = new Query(query); if (lang != null) { q.setLang(lang); } if (locale != null) { q.setLocale(locale); } if (maxId != null && maxId.longValue() != 0) { q.setMaxId(maxId.longValue()); } if (since != null) { q.setSince(since); } if (sinceId != null && sinceId.longValue() != 0) { q.setSinceId(sinceId.longValue()); } if (geocode != null) { final String[] geocodeSplit = StringUtils.split(geocode, ','); final double latitude = Double.parseDouble(StringUtils.replace(geocodeSplit[0], " ", "")); final double longitude = Double.parseDouble(StringUtils.replace(geocodeSplit[1], " ", "")); q.setGeoCode(new GeoLocation(latitude, longitude), Double.parseDouble(radius), unit); } if (until != null) { q.setUntil(until); } if (resultType != null) { q.setResultType(resultType); } return twitter.search(q); }
From source file:org.n52.twitter.dao.TwitterSearchDAO.java
License:Open Source License
@Override public Collection<TwitterMessage> search(double latitude, double longitude, int distanceMeters, DateTime fromDate, DateTime toDate) throws TwitterException, DecodingException { Query searchQuery = new Query(); searchQuery.setGeoCode(new GeoLocation(latitude, longitude), distanceMeters / 1000, DEFAULT_UNIT); if (toDate != null) { searchQuery.setUntil(toDate.toString("YYYY-MM-dd")); }// w ww . ja v a 2s. c om if (fromDate != null) { searchQuery.setSince(fromDate.toString("YYYY-MM-dd")); } return executeApiRequest(searchQuery); }
From source file:org.wandora.application.tools.extractors.twitter.TwitterExtractorUI.java
License:Open Source License
public Query[] getSearchQuery() { String query = queryTextField.getText(); String lang = langTextField.getText().trim(); String until = untilTextField.getText().trim(); String since = sinceTextField.getText().trim(); GeoLocation geol = solveGeoLocation(); double distance = solveDistance(); ArrayList<Query> queries = new ArrayList(); Query q = new Query(query); if (lang.length() > 0) q.setLang(lang);/*from w w w . ja v a2 s . c o m*/ if (until.length() > 0) q.setUntil(until); if (since.length() > 0) q.setSince(since); if (geol != null) q.setGeoCode(geol, distance, Query.KILOMETERS); q.count(100); queries.add(q); return queries.toArray(new Query[] {}); }