Search in sources :

Example 16 with QueryResult

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);
}
Also used : Status(twitter4j.Status) QueryResult(twitter4j.QueryResult) Twitter(twitter4j.Twitter) GeoLocation(twitter4j.GeoLocation) TwitterEndpoint(org.apache.camel.component.twitter.TwitterEndpoint)

Example 17 with QueryResult

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;
    }
}
Also used : Status(twitter4j.Status) CamelExchangeException(org.apache.camel.CamelExchangeException) QueryResult(twitter4j.QueryResult) Query(twitter4j.Query) Twitter(twitter4j.Twitter) TwitterEndpoint(org.apache.camel.component.twitter.TwitterEndpoint)

Example 18 with QueryResult

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);
}
Also used : Query(twitter4j.Query) SourceResponseImpl(ddf.catalog.operation.impl.SourceResponseImpl) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) ConfigurationBuilder(twitter4j.conf.ConfigurationBuilder) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) Twitter(twitter4j.Twitter) ResultImpl(ddf.catalog.data.impl.ResultImpl) QueryResult(twitter4j.QueryResult) Result(ddf.catalog.data.Result) QueryResult(twitter4j.QueryResult) GeoLocation(twitter4j.GeoLocation) TwitterException(twitter4j.TwitterException)

Aggregations

QueryResult (twitter4j.QueryResult)18 Twitter (twitter4j.Twitter)17 Status (twitter4j.Status)16 Activity (android.app.Activity)12 Query (twitter4j.Query)12 TimelineArrayAdapter (com.klinker.android.twitter.adapters.TimelineArrayAdapter)8 TwitterException (twitter4j.TwitterException)5 ComposeActivity (com.klinker.android.twitter.activities.compose.ComposeActivity)3 SettingsActivity (com.klinker.android.twitter.settings.SettingsActivity)3 GeoLocation (twitter4j.GeoLocation)3 View (android.view.View)2 AbsListView (android.widget.AbsListView)2 DrawerActivity (com.klinker.android.twitter.activities.drawer_activities.DrawerActivity)2 ArrayList (java.util.ArrayList)2 TwitterEndpoint (org.apache.camel.component.twitter.TwitterEndpoint)2 AsyncListView (org.lucasr.smoothie.AsyncListView)2 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)2 Location (android.location.Location)1 LinearLayout (android.widget.LinearLayout)1 Mastodon4jRequestException (com.sys1yagi.mastodon4j.api.exception.Mastodon4jRequestException)1