use of twitter4j.QueryResult in project camel by apache.
the class SearchConsumer method search.
private List<Exchange> search(Query query) throws TwitterException {
Integer numberOfPages = 1;
if (ObjectHelper.isNotEmpty(endpoint.getProperties().getLang())) {
query.setLang(endpoint.getProperties().getLang());
}
if (ObjectHelper.isNotEmpty(endpoint.getProperties().getCount())) {
query.setCount(endpoint.getProperties().getCount());
}
if (ObjectHelper.isNotEmpty(endpoint.getProperties().getNumberOfPages())) {
numberOfPages = endpoint.getProperties().getNumberOfPages();
}
if (ObjectHelper.isNotEmpty(endpoint.getProperties().getLatitude()) && ObjectHelper.isNotEmpty(endpoint.getProperties().getLongitude()) && ObjectHelper.isNotEmpty(endpoint.getProperties().getRadius())) {
GeoLocation location = new GeoLocation(endpoint.getProperties().getLatitude(), endpoint.getProperties().getLongitude());
query.setGeoCode(location, endpoint.getProperties().getRadius(), Unit.valueOf(endpoint.getProperties().getDistanceMetric()));
LOG.debug("Searching with additional geolocation parameters.");
}
LOG.debug("Searching with {} pages.", numberOfPages);
Twitter twitter = getTwitter();
QueryResult qr = twitter.search(query);
List<Status> tweets = qr.getTweets();
for (int i = 1; i < numberOfPages; i++) {
if (!qr.hasNext()) {
break;
}
qr = twitter.search(qr.nextQuery());
tweets.addAll(qr.getTweets());
}
if (endpoint.getProperties().isFilterOld()) {
for (int i = 0; i < tweets.size(); i++) {
setLastIdIfGreater(tweets.get(i).getId());
}
}
return TwitterEventType.STATUS.createExchangeList(endpoint, tweets);
}
use of twitter4j.QueryResult in project camel by apache.
the class SearchProducer method process.
@Override
public void process(Exchange exchange) throws Exception {
long myLastId = lastId;
// KEYWORDS
// keywords from header take precedence
String keywords = exchange.getIn().getHeader(TwitterConstants.TWITTER_KEYWORDS, String.class);
if (keywords == null) {
keywords = endpoint.getProperties().getKeywords();
}
if (keywords == null) {
throw new CamelExchangeException("No keywords to use for query", exchange);
}
Query query = new Query(keywords);
// filter of older tweets
if (endpoint.getProperties().isFilterOld() && myLastId != 0) {
query.setSinceId(myLastId);
}
// since id
Long sinceId = exchange.getIn().getHeader(TwitterConstants.TWITTER_SINCEID, Long.class);
if (sinceId == null) {
sinceId = endpoint.getProperties().getSinceId();
}
if (ObjectHelper.isNotEmpty(sinceId)) {
query.setSinceId(sinceId);
}
// max id
Long maxId = exchange.getIn().getHeader(TwitterConstants.TWITTER_MAXID, Long.class);
if (ObjectHelper.isNotEmpty(maxId)) {
query.setMaxId(maxId);
}
// language
String lang = exchange.getIn().getHeader(TwitterConstants.TWITTER_SEARCH_LANGUAGE, String.class);
if (lang == null) {
lang = endpoint.getProperties().getLang();
}
if (ObjectHelper.isNotEmpty(lang)) {
query.setLang(lang);
}
// number of elements per page
Integer count = exchange.getIn().getHeader(TwitterConstants.TWITTER_COUNT, Integer.class);
if (count == null) {
count = endpoint.getProperties().getCount();
}
if (ObjectHelper.isNotEmpty(count)) {
query.setCount(count);
}
// number of pages
Integer numberOfPages = exchange.getIn().getHeader(TwitterConstants.TWITTER_NUMBER_OF_PAGES, Integer.class);
if (numberOfPages == null) {
numberOfPages = endpoint.getProperties().getNumberOfPages();
}
Twitter twitter = endpoint.getProperties().getTwitter();
log.debug("Searching twitter with keywords: {}", keywords);
QueryResult results = twitter.search(query);
List<Status> list = results.getTweets();
for (int i = 1; i < numberOfPages; i++) {
if (!results.hasNext()) {
break;
}
log.debug("Fetching page");
results = twitter.search(results.nextQuery());
list.addAll(results.getTweets());
}
if (endpoint.getProperties().isFilterOld()) {
for (Status t : list) {
long newId = t.getId();
if (newId > myLastId) {
myLastId = newId;
}
}
}
exchange.getIn().setBody(list);
// update the lastId after finished the processing
if (myLastId > lastId) {
lastId = myLastId;
}
}
use of twitter4j.QueryResult in project ddf by codice.
the class TwitterSource method query.
@Override
public SourceResponse query(QueryRequest request) throws UnsupportedQueryException {
Twitter instance = twitterFactory.getInstance();
try {
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);
}
Aggregations