List of usage examples for twitter4j Query Query
public Query()
From source file:dk.netarkivet.harvester.tools.TwitterDecidingScope.java
License:Open Source License
/** * This routine makes any necessary Twitter API calls and queues the content discovered. * * @param controller The controller for this crawl. *//*w ww .j a v a 2s . com*/ @Override public void initialize(CrawlController controller) { super.initialize(controller); twitter = (new TwitterFactory()).getInstance(); keywords = null; try { keywords = (StringList) super.getAttribute(ATTR_KEYWORDS); pages = ((Integer) super.getAttribute(ATTR_PAGES)).intValue(); geoLocations = (StringList) super.getAttribute(ATTR_GEOLOCATIONS); language = (String) super.getAttribute(ATTR_LANG); if (language == null) { language = "all"; } resultsPerPage = (Integer) super.getAttribute(ATTR_RESULTS_PER_PAGE); queueLinks = (Boolean) super.getAttribute(ATTR_QUEUE_LINKS); queueUserStatus = (Boolean) super.getAttribute(ATTR_QUEUE_USER_STATUS); queueUserStatusLinks = (Boolean) super.getAttribute(ATTR_QUEUE_USER_STATUS_LINKS); queueKeywordLinks = (Boolean) super.getAttribute(ATTR_QUEUE_KEYWORD_LINKS); } catch (AttributeNotFoundException e1) { e1.printStackTrace(); throw new RuntimeException(e1); } catch (MBeanException e1) { e1.printStackTrace(); throw new RuntimeException(e1); } catch (ReflectionException e1) { e1.printStackTrace(); throw new RuntimeException(e1); } for (Object keyword : keywords) { log.info("Twitter Scope keyword: {}", keyword); } // If keywords or geoLocations is missing, add a list with a single empty string so that the main loop is // executed at least once. if (keywords == null || keywords.isEmpty()) { keywords = new StringList("keywords", "empty keyword list", new String[] { "" }); } if (geoLocations == null || geoLocations.isEmpty()) { geoLocations = new StringList("geolocations", "empty geolocation list", new String[] { "" }); } log.info("Twitter Scope will queue {} page(s) of results.", pages); // Nested loop over keywords, geo_locations and pages. for (Object keyword : keywords) { String keywordString = (String) keyword; for (Object geoLocation : geoLocations) { String urlQuery = (String) keyword; Query query = new Query(); query.setRpp(resultsPerPage); if (language != null && !language.equals("")) { query.setLang(language); urlQuery += " lang:" + language; keywordString += " lang:" + language; } urlQuery = "http://twitter.com/search/" + URLEncoder.encode(urlQuery); if (queueKeywordLinks) { addSeedIfLegal(urlQuery); } for (int page = 1; page <= pages; page++) { query.setPage(page); if (!keyword.equals("")) { query.setQuery(keywordString); } if (!geoLocation.equals("")) { String[] locationArray = ((String) geoLocation).split(","); try { GeoLocation location = new GeoLocation(Double.parseDouble(locationArray[0]), Double.parseDouble(locationArray[1])); query.setGeoCode(location, Double.parseDouble(locationArray[2]), locationArray[3]); } catch (NumberFormatException e) { e.printStackTrace(); } } try { final QueryResult result = twitter.search(query); List<Tweet> tweets = result.getTweets(); for (Tweet tweet : tweets) { long id = tweet.getId(); String fromUser = tweet.getFromUser(); String tweetUrl = "http://www.twitter.com/" + fromUser + "/status/" + id; addSeedIfLegal(tweetUrl); tweetCount++; if (queueLinks) { extractEmbeddedLinks(tweet); } if (queueUserStatus) { String statusUrl = "http://twitter.com/" + tweet.getFromUser() + "/"; addSeedIfLegal(statusUrl); linkCount++; if (queueUserStatusLinks) { queueUserStatusLinks(tweet.getFromUser()); } } } } catch (TwitterException e1) { log.error(e1.getMessage()); } } } } System.out.println( TwitterDecidingScope.class + " added " + tweetCount + " tweets and " + linkCount + " other links."); }
From source file:dk.netarkivet.harvester.tools.TwitterDecidingScope.java
License:Open Source License
/** * Searches for a given users recent tweets and queues and embedded material found. * * @param user The twitter username (without the @ prefix). */// w ww . j av a2 s . c o m private void queueUserStatusLinks(String user) { Query query = new Query(); query.setQuery("@" + user); query.setRpp(20); if (!language.equals("")) { query.setLang(language); } try { List<Tweet> results = twitter.search(query).getTweets(); if (results != null && !results.isEmpty()) { System.out.println("Extracting embedded links for user " + user); } for (Tweet result : results) { if (result.getIsoLanguageCode().equals(language) || language.equals("")) { extractEmbeddedLinks(result); } } } catch (TwitterException e) { e.printStackTrace(); } }
From source file:it.greenvulcano.gvesb.social.twitter.directcall.TwitterOperationSearch.java
License:Open Source License
@Override public void execute(SocialAdapterAccount account) throws SocialAdapterException { try {//from w w w. j ava2s . co m 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:net.lacolaco.smileessence.twitter.task.SearchTask.java
License:Open Source License
public static Query getBaseQuery(MainActivity activity, String queryString) { Configuration config = activity.getResources().getConfiguration(); Query query = new Query(); query.setQuery(queryString);//from w w w . j a v a 2 s .co m query.setLang(config.locale.getLanguage()); query.setCount(TwitterUtils.getPagingCount(activity)); query.setResultType(Query.RECENT); return query; }
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 w ww .j ava2 s . com*/ 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.gabrielebaldassarre.twitter.commodities.querybuilder.TwitterQueryBuilder.java
License:Open Source License
/** * Build a valid Twitter API {@link Query}ery from the inputed conditions * /*from ww w. j a v a2s . c o m*/ * @return a reference to a valid {@link Query} object */ public Query build() { ResourceBundle rb = ResourceBundle.getBundle("tTwitterInput", Locale.getDefault()); String item; query = new Query(); sb = new StringBuilder(); Iterator<Entry<String, TwitterQueryBuilderOperator>> qf = queryFragments.entrySet().iterator(); while (qf.hasNext()) { @SuppressWarnings("rawtypes") Map.Entry f = (Map.Entry) qf.next(); if (TwitterQueryBuilderLogicalOperator.OR.equals(op) && TwitterQueryBuilderOperator.EXCLUDE.equals(f.getValue())) throw new IllegalStateException(rb.getString("exception.excludingOr")); item = ((f.getValue() == null ? TwitterQueryBuilderOperator.INCLUDE : f.getValue()).toString() + f.getKey()); if (sb.length() > 0) sb.append(op.toString()); sb.append((item.split("\\s+").length <= 1) ? item : ("\"" + item + "\"")); qf.remove(); // avoids a ConcurrentModificationException } if (getFilterLinksCondition()) sb.append(" " + TwitterQueryBuilder.LINKFRAGMENT); if (getFilterQuestionsCondition()) sb.append(" " + TwitterQueryBuilder.QUESTIONFRAGMENT); if (!TwitterQueryBuilderAttitude.NOFILTER.equals(getAttitude())) sb.append(" " + getAttitude().toString()); try { query.setQuery(URLEncoder.encode(sb.toString(), "UTF-8")); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } if (lang != null) query.lang(lang); if (rt != null) query.resultType(rt.getNativeType()); if (since != null) query.since(since); if (sinceId != null) query.sinceId(sinceId); if (until != null) query.until(until); if (maxId != null) query.maxId(maxId); if (count != null) query.count(count); return query; }
From source file:org.ipccenter.newsagg.impl.twitterapi.TwitterPuller.java
public void findPosts() throws TwitterException { StringBuilder searchURL = new StringBuilder(); searchURL.append("https://api.twitter.com/1.1/search/tweets.json&q="); QueryResult searchResult = null;//from ww w . j a v a 2 s.com List<Status> posts = new ArrayList<Status>(); Query q = new Query(); LOG.info("Query: {}", q.getQuery()); List<String> requests = new ArrayList<String>(); requests.add(""); requests.add(""); requests.add(""); for (String request : requests) { q.query(request.toString()); LOG.info("New query: {}", q.getQuery()); searchResult = twitter.search(q); posts.addAll(searchResult.getTweets()); } LOG.info("Posts amount: {}", posts.size()); for (Status status : posts) { parsePost(status); } }
From source file:org.mixare.utils.TwitterClient.java
License:Open Source License
/** * Query the twitter search API using oAuth 2.0 * @return/* w ww.j ava 2 s . c o m*/ */ public static String queryData() { ConfigurationBuilder cb = new ConfigurationBuilder(); //to be configured in a properties... cb.setDebugEnabled(true).setOAuthConsumerKey("mt10dv6tTKacqlm14lw5w") .setOAuthConsumerSecret("4kRV1E1XIU3kj4JQj2R5LE1yct0RRaRl9sB5PpPrB0") .setOAuthAccessToken("390019380-IQ5VdvUKvxY9JOsTToEU8ElCabebc76H9X2g3QX4") .setOAuthAccessTokenSecret("ghJn4LTfDr7uHUCsbt6ycmpeVTwwpa3hZnXyEjyZvs"); cb.setJSONStoreEnabled(true); TwitterFactory tf = new TwitterFactory(cb.build()); Twitter twitter = tf.getInstance(); Query query = new Query(); query = query.geoCode(new GeoLocation(lat, lon), rad, Query.KILOMETERS); String jsonArrayAsString = "{\"results\":[";//start try { QueryResult result = twitter.search(query); int size = 0; for (Status status : result.getTweets()) { { if (status.getGeoLocation() != null) { String jsonSingleObject = DataObjectFactory.getRawJSON(status); if (size == 0) jsonArrayAsString += jsonSingleObject; else jsonArrayAsString += "," + jsonSingleObject; size++; } } } jsonArrayAsString += "]}";//close array return jsonArrayAsString; } catch (Exception e) { Log.e(Config.TAG, "Error querying twitter data :" + e); e.printStackTrace(); } return null; }
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 .j av a 2 s .com if (fromDate != null) { searchQuery.setSince(fromDate.toString("YYYY-MM-dd")); } return executeApiRequest(searchQuery); }
From source file:org.shredzone.flufftron.service.TwitterService.java
License:Open Source License
/** * Polls new fluff tweets for a {@link Person}. * * @param person// w ww. ja va 2 s.c om * {@link Person} to find fluff tweets for * @throws TwitterException * if the fluff tweets could not be retrieved */ public void pollNewFluffs(Person person) throws TwitterException { Timeline timeline = person.getTimeline(); String user = timeline.getTwitter(); if (user == null || user.isEmpty()) { return; } Query q = new Query().resultType("recent").rpp(50); q.query(HASHTAG + " @" + user); if (timeline.getLastId() != 0) { q.setSinceId(timeline.getLastId()); } Date lastFluff = timeline.getLastFluff(); QueryResult r = twitter.search(q); // The iterator is a workaround because twitter4j seems to be built in a funny // way that allows Generics and JDK1.4, but breaks compatibility with Java 7. Iterator<Tweet> it = r.getTweets().iterator(); while (it.hasNext()) { Tweet tweet = it.next(); if (user.equalsIgnoreCase(tweet.getFromUser())) { // ignore eigenflausch continue; } Fluff fluff = new Fluff(); fluff.setPersonId(person.getId()); fluff.setTwitId(tweet.getId()); fluff.setCreated(tweet.getCreatedAt()); fluff.setFrom(tweet.getFromUser()); fluff.setText(tweet.getText()); fluffDao.save(fluff); if (lastFluff == null || (fluff.getCreated() != null && fluff.getCreated().after(lastFluff))) { lastFluff = fluff.getCreated(); } } timeline.setLastId(r.getMaxId()); timeline.setLastFluff(lastFluff); personDao.save(person); }