Example usage for twitter4j Query setResultType

List of usage examples for twitter4j Query setResultType

Introduction

In this page you can find the example usage for twitter4j Query setResultType.

Prototype

public void setResultType(ResultType resultType) 

Source Link

Document

Default value is Query.MIXED if parameter not specified

Usage

From source file:ai.ilikeplaces.logic.Listeners.ListenerMain.java

License:Open Source License

/**
 * @param request__/*  w w  w  . j  a  v a 2 s .c o m*/
 * @param response__
 */
@Override
public void processRequest(final ItsNatServletRequest request__, final ItsNatServletResponse response__) {
    new AbstractListener(request__, response__) {
        protected String location;

        protected String superLocation;

        protected Long WOEID;

        /**
         * Initialize your document here by appending fragments
         */
        @Override
        @SuppressWarnings("unchecked")
        @_todo(task = "If location is not available, it should be added through a w" + "id"
                + "get(or fragment maybe?)")
        protected final void init(final ItsNatHTMLDocument itsNatHTMLDocument__,
                final HTMLDocument hTMLDocument__, final ItsNatDocument itsNatDocument__,
                final Object... initArgs) {
            final SmartLogger sl = SmartLogger.start(Loggers.LEVEL.DEBUG, "Location Page.", 60000, null, true);

            //this.location = (String) request_.getServletRequest().getAttribute(RBGet.config.getString("HttpSessionAttr.location"));
            getLocationSuperLocation: {
                final String[] attr = ((String) request__.getServletRequest()
                        .getAttribute(RBGet.globalConfig.getString(HTTP_SESSION_ATTR_LOCATION))).split("_");
                location = attr[0];
                if (attr.length == 3) {
                    superLocation = attr[2];
                } else {
                    superLocation = EMPTY;
                }
                tryLocationId: {
                    try {
                        final String w = request__.getServletRequest().getParameter(Location.WOEID);
                        if (w != null) {
                            WOEID = Long.parseLong(request__.getServletRequest().getParameter(Location.WOEID));
                        }
                    } catch (final NumberFormatException e) {
                        Loggers.USER_EXCEPTION.error(WRONG_WOEID_FORMAT, e);
                    }
                }
            }

            sl.appendToLogMSG(RETURNING_LOCATION + location + TO_USER);

            final ResourceBundle gUI = ResourceBundle.getBundle(AI_ILIKEPLACES_RBS_GUI);

            layoutNeededForAllPages: {
                setLoginWidget: {
                    try {
                        new SignInOn(request__, $(Main_login_widget),
                                new SignInOnCriteria().setHumanId(new HumanId(getUsername()))
                                        .setSignInOnDisplayComponent(
                                                SignInOnCriteria.SignInOnDisplayComponent.MOMENTS)) {
                        };
                    } catch (final Throwable t) {
                        sl.l(ERROR_IN_UC_SET_LOGIN_WIDGET, t);
                    }
                }

                SEO: {
                    try {
                        setMainTitle: {
                            $(mainTitle).setTextContent(
                                    MessageFormat.format(gUI.getString("woeidpage.title"), location));
                        }
                        setMetaDescription: {
                            $(mainMetaDesc).setAttribute(MarkupTag.META.content(),
                                    MessageFormat.format(gUI.getString("woeidpage.desc"), location));
                        }
                    } catch (final Throwable t) {
                        sl.l(ERROR_IN_UC_SEO, t);
                    }
                }
                signOnDisplayLink: {
                    try {
                        if (getUsername() != null) {
                            $(Main_othersidebar_identity).setTextContent(
                                    gUI.getString(AI_ILIKEPLACES_LOGIC_LISTENERS_LISTENER_MAIN_0004)
                                            + getUsernameAsValid());
                        } else {
                            $(Main_othersidebar_identity).setTextContent(
                                    gUI.getString(AI_ILIKEPLACES_LOGIC_LISTENERS_LISTENER_MAIN_0005)
                                            + location);
                        }
                    } catch (final Throwable t) {
                        sl.l(ERROR_IN_UC_SIGN_ON_DISPLAY_LINK, t);
                    }
                }
                setProfileLink: {
                    try {
                        if (getUsername() != null) {
                            $(Main_othersidebar_profile_link).setAttribute(MarkupTag.A.href(),
                                    Controller.Page.Profile.getURL());
                        } else {
                            $(Main_othersidebar_profile_link).setAttribute(MarkupTag.A.href(),
                                    Controller.Page.signup.getURL());
                        }
                    } catch (final Throwable t) {
                        sl.l(ERROR_IN_UC_SET_PROFILE_LINK, t);
                    }
                }
                setProfilePhotoLink: {
                    try {
                        if (getUsername() != null) {
                            /**
                             * TODO check for db failure
                             */
                            String url = DB.getHumanCRUDHumanLocal(true)
                                    .doDirtyRHumansProfilePhoto(new HumanId(getUsernameAsValid()))
                                    .returnValueBadly();
                            url = url == null ? null : RBGet.globalConfig.getString(PROFILE_PHOTOS) + url;
                            if (url != null) {
                                $(Main_profile_photo).setAttribute(MarkupTag.IMG.src(), url);
                                //displayBlock($(Main_profile_photo));
                            } else {
                                //displayNone($(Main_profile_photo));
                            }
                        } else {
                            //displayNone($(Main_profile_photo));
                        }
                    } catch (final Throwable t) {
                        sl.l(ERROR_IN_UC_SET_PROFILE_PHOTO_LINK, t);
                    }
                }
            }

            final Return<Location> r;
            if (WOEID != null) {
                r = DB.getHumanCRUDLocationLocal(true).doRLocation(WOEID, REFRESH_SPEC);
            } else {
                r = new ReturnImpl<Location>(ExceptionCache.UNSUPPORTED_OPERATION_EXCEPTION,
                        "Search Unavailable", false);
                //r = DB.getHumanCRUDLocationLocal(true).dirtyRLocation(location, superLocation);
            }

            if (r.returnStatus() == 0 && r.returnValue() != null) {
                final Location existingLocation_ = r.returnValue();

                GEO: {
                    if (existingLocation_.getLocationGeo1() == null
                            || existingLocation_.getLocationGeo2() == null) {
                        final Client ygpClient = YAHOO_GEO_PLANET_FACTORY
                                .getInstance(RBGet.globalConfig.getString("where.yahooapis.com.v1.place"));
                        final Place place = ygpClient.getPlace(existingLocation_.getLocationId().toString());
                        existingLocation_.setLocationGeo1(Double.toString(place.getCoordinates().getY()));
                        existingLocation_.setLocationGeo2(Double.toString(place.getCoordinates().getX()));

                        new Thread(new Runnable() {

                            @Override
                            public void run() {
                                DB.getHumanCRUDLocationLocal(true).doULocationLatLng(
                                        new Obj<Long>(existingLocation_.getLocationId()),
                                        new Obj<Double>(place.getCoordinates().getY()),
                                        new Obj<Double>(place.getCoordinates().getX()));
                            }
                        }).run();
                    }
                }

                final Location locationSuperSet = existingLocation_.getLocationSuperSet();
                GEO_WIDGET: {
                    new ai.ilikeplaces.logic.Listeners.widgets.schema.thing.Comment(request__,
                            new CommentCriteria(), $(Controller.Page.Main_right_column)) {
                        @Override
                        protected void init(CommentCriteria commentCriteria) {
                            final ai.ilikeplaces.logic.Listeners.widgets.schema.thing.Place place = new ai.ilikeplaces.logic.Listeners.widgets.schema.thing.Place(
                                    request__, new PlaceCriteria()
                                            //This Place
                                            .setPlaceNamePre("Exciting events in ")
                                            .setPlaceName(existingLocation_.getLocationName())
                                            .setPlaceLat(existingLocation_.getLocationGeo1())
                                            .setPlaceLng(existingLocation_.getLocationGeo2())
                            //Parent Place
                            //.setPlaceSuperName(locationSuperSet.getLocationName())
                            //.setPlaceSuperLat(locationSuperSet.getLocationGeo1())
                            //.setPlaceSuperLng(locationSuperSet.getLocationGeo2())
                            //Parent WOEID
                            //.setPlaceSuperWOEID(locationSuperSet.getWOEID().toString())
                            , $$(CommentIds.commentPerson));
                            place.$$displayNone(place.$$(
                                    ai.ilikeplaces.logic.Listeners.widgets.schema.thing.Place.PlaceIds.placeWidget));
                        }
                    };
                }

                final List<String> titleManifest = new ArrayList<String>();

                EVENTS_WIDGETS: {
                    try {
                        /* final JSONObject jsonObject = Modules.getModules().getYahooUplcomingFactory()
                            .getInstance("http://upcoming.yahooapis.com/services/rest/")
                            .get("",
                                    new HashMap<String, String>() {
                                        {//Don't worry, this is a static initializer of this map :)
                                            put("method", "event.search");
                                            put("woeid", WOEID.toString());
                                            put("format", "json");
                                        }
                                    }
                                
                            );
                         final JSONArray events = jsonObject.getJSONObject("rsp").getJSONArray("event");*/

                        final JSONObject jsonObject = Modules.getModules().getEventulFactory()
                                .getInstance("http://api.eventful.com/json/events/search/")
                                .get("", new HashMap<String, String>() {
                                    {//Don't worry, this is a static initializer of this map :)
                                        put("location", "" + existingLocation_.getLocationGeo1() + ","
                                                + existingLocation_.getLocationGeo2());
                                        put("within", "" + 100);
                                    }
                                }

                        );

                        Loggers.debug("Eventful Reply:" + jsonObject.toString());

                        //                            final String eventName = eventJSONObject.getString("title");
                        //                            final String eventUrl = eventJSONObject.getString("url");
                        //                            final String eventDate = eventJSONObject.getString("start_time");
                        //                            final String eventVenue = eventJSONObject.getString("venue_name");

                        final JSONArray events = jsonObject.getJSONObject("events").getJSONArray("event");

                        for (int i = 0; i < events.length(); i++) {
                            final JSONObject eventJSONObject = new JSONObject(events.get(i).toString());

                            titleManifest.add(eventJSONObject.get("title").toString());

                            final String photoUrl;
                            String temp = null;
                            try {
                                temp = eventJSONObject.getJSONObject("image").get("url").toString();
                            } catch (final Throwable e) {
                                SmartLogger.g().l(e.getMessage());
                            } finally {
                                photoUrl = temp;
                            }
                            //The pain I go through to make variables final :D

                            new Event(request__, new EventCriteria()
                                    .setEventName(eventJSONObject.get("title").toString())
                                    .setEventStartDate(eventJSONObject.get("start_time").toString())
                                    .setEventPhoto(photoUrl)
                                    .setPlaceCriteria(new PlaceCriteria()
                                            .setPlaceNamePre("This event is taking place in ")
                                            .setPlaceName(existingLocation_.getLocationName())
                                            .setPlaceLat(eventJSONObject.get("latitude").toString())
                                            .setPlaceLng(eventJSONObject.get("longitude").toString())
                                            //Parent Place
                                            .setPlaceSuperNamePre(
                                                    existingLocation_.getLocationName() + " is located in ")
                                            .setPlaceSuperName(locationSuperSet.getLocationName())
                                            .setPlaceSuperLat(locationSuperSet.getLocationGeo1())
                                            .setPlaceSuperLng(locationSuperSet.getLocationGeo2())
                                            //Parent WOEID
                                            .setPlaceSuperWOEID(locationSuperSet.getWOEID().toString())

                            ), $(Controller.Page.Main_right_column));
                        }
                    } catch (final JSONException e) {
                        sl.l("Error fetching data from Yahoo Upcoming: " + e.getMessage());
                    }
                }

                TWITTER_WIDGETS: {
                    try {
                        final Query _query = new Query(
                                "fun OR happening OR enjoy OR nightclub OR restaurant OR party OR travel :)");
                        _query.geoCode(
                                new GeoLocation(Double.parseDouble(existingLocation_.getLocationGeo1()),
                                        Double.parseDouble(existingLocation_.getLocationGeo2())),
                                40, Query.MILES);
                        _query.setResultType(Query.MIXED);
                        final QueryResult result = TWITTER.search(_query);

                        //final QueryResult result = TWITTER.search(new Query("Happy").geoCode(new GeoLocation(Double.parseDouble(existingLocation_.getLocationGeo1()), Double.parseDouble(existingLocation_.getLocationGeo2())), 160, Query.MILES));
                        for (Status tweet : result.getTweets()) {
                            new ai.ilikeplaces.logic.Listeners.widgets.schema.thing.Person(request__,
                                    new PersonCriteria().setPersonName(tweet.getUser().getName())
                                            .setPersonPhoto(tweet.getUser().getProfileImageURL())
                                            .setPersonData(tweet.getText()),
                                    $(Main_right_column));
                            titleManifest.add(tweet.getText());
                        }
                        if (result.getTweets().size() == 0) {
                            sl.l("No twitter results found");
                        }
                    } catch (final Throwable t) {
                        sl.l("An error occurred during twitter fetch:" + t.getMessage());
                    }
                }

                SEO: {
                    setMetaGEOData: {
                        $(Main_ICBM).setAttribute(MarkupTag.META.content(), existingLocation_.getLocationGeo1()
                                + COMMA + existingLocation_.getLocationGeo2());
                        $(Main_geoposition).setAttribute(MarkupTag.META.content(),
                                existingLocation_.getLocationGeo1() + COMMA
                                        + existingLocation_.getLocationGeo2());
                        $(Main_geoplacename).setAttribute(MarkupTag.META.content(),
                                existingLocation_.getLocationName());
                        $(Main_georegion).setAttribute(MarkupTag.META.content(),
                                locationSuperSet.getLocationName());
                    }
                }

                setLocationIdForJSReference: {
                    try {
                        final Element hiddenLocationIdInputTag = $(INPUT);
                        hiddenLocationIdInputTag.setAttribute(INPUT.type(), INPUT.typeValueHidden());
                        hiddenLocationIdInputTag.setAttribute(INPUT.id(), JSCodeToSend.LocationId);
                        hiddenLocationIdInputTag.setAttribute(INPUT.value(),
                                existingLocation_.getLocationId().toString());
                        hTMLDocument__.getBody().appendChild(hiddenLocationIdInputTag);

                        $(Main_location_name).setAttribute(INPUT.value(),
                                existingLocation_.getLocationName() + "");
                        $(Main_super_location_name).setAttribute(INPUT.value(),
                                locationSuperSet.getLocationName() + "");
                    } catch (final Throwable t) {
                        sl.l(ERROR_IN_UC_SET_LOCATION_ID_FOR_JSREFERENCE, t);
                    }
                }

                setLocationNameForJSReference: {
                    try {
                        final Element hiddenLocationIdInputTag = $(INPUT);
                        hiddenLocationIdInputTag.setAttribute(INPUT.type(), INPUT.typeValueHidden());
                        hiddenLocationIdInputTag.setAttribute(INPUT.id(), JSCodeToSend.LocationName);
                        hiddenLocationIdInputTag.setAttribute(INPUT.value(),
                                existingLocation_.getLocationName() + OF + locationSuperSet.getLocationName());
                        hTMLDocument__.getBody().appendChild(hiddenLocationIdInputTag);
                    } catch (final Throwable t) {
                        sl.l(ERROR_IN_UC_SET_LOCATION_NAME_FOR_JSREFERENCE, t);
                    }
                }

                setLocationAsPageTopic: {
                    try {

                        final StringBuilder title = new StringBuilder();

                        for (final String titleGuest : titleManifest) {
                            title.append(titleGuest);
                        }

                        final String finalTitle = title.toString();

                        $(Main_center_main_location_title).setTextContent(finalTitle.isEmpty()
                                ? (THIS_IS + existingLocation_.getLocationName() + OF + locationSuperSet)
                                : finalTitle);

                        for (final Element element : generateLocationLinks(DB.getHumanCRUDLocationLocal(true)
                                .doDirtyRLocationsBySuperLocation(existingLocation_))) {
                            $(Main_location_list).appendChild(element);
                            displayBlock($(Main_notice_sh));
                        }
                    } catch (final Throwable t) {
                        sl.l(ERROR_IN_UC_SET_LOCATION_AS_PAGE_TOPIC, t);
                    }
                }
            } else {
                noSupportForNewLocations: {
                    try {
                        //                            $(Main_notice).appendChild(($(P).appendChild(
                        //                                    hTMLDocument__.createTextNode(RBGet.logMsgs.getString("CANT_FIND_LOCATION")
                        //                                            + " Were you looking for "
                        //                                    ))));
                        //                            for (final Element element : generateLocationLinks(DB.getHumanCRUDLocationLocal(true).dirtyRLikeLocations(location))) {
                        //                                $(Main_notice).appendChild(element);
                        //                                displayBlock($(Main_notice_sh));
                        //                            }
                        NotSupportingLikeSearchTooForNow: {
                            $(Main_notice).appendChild(($(P).appendChild(hTMLDocument__
                                    .createTextNode(RBGet.logMsgs.getString("CANT_FIND_LOCATION")))));
                        }
                    } catch (final Throwable t) {
                        sl.l(ERROR_IN_UC_NO_SUPPORT_FOR_NEW_LOCATIONS, t);
                    }
                }
            }
            sl.complete(Loggers.LEVEL.SERVER_STATUS, Loggers.DONE);
        }

        /**
         * Use ItsNatHTMLDocument variable stored in the AbstractListener class
         */
        @Override
        protected void registerEventListeners(final ItsNatHTMLDocument itsNatHTMLDocument__,
                final HTMLDocument hTMLDocument__, final ItsNatDocument itsNatDocument__) {
        }

        private List<Element> generateLocationLinks(final List<Location> locationList) {
            final ElementComposer UList = ElementComposer.compose($(UL)).$ElementSetAttribute(MarkupTag.UL.id(),
                    PLACE_LIST);

            for (Location location : locationList) {
                final Element link = $(A);

                link.setTextContent(TRAVEL_TO + location.getLocationName() + OF
                        + location.getLocationSuperSet().getLocationName());

                link.setAttribute(A.href(),
                        PAGE + location.getLocationName() + _OF_
                                + location.getLocationSuperSet().getLocationName()
                                + Parameter.get(Location.WOEID, location.getWOEID().toString(), true));

                link.setAttribute(A.alt(), PAGE + location.getLocationName() + _OF_
                        + location.getLocationSuperSet().getLocationName());

                link.setAttribute(A.title(), CLICK_TO_EXPLORE + location.getLocationName() + OF
                        + location.getLocationSuperSet().getLocationName());

                link.setAttribute(A.classs(), VTIP);

                final Element linkDiv = $(DIV);

                linkDiv.appendChild(link);

                UList.wrapThis(ElementComposer.compose($(LI)).wrapThis(linkDiv).get());
            }

            final List<Element> elements = new ArrayList<Element>();
            elements.add(UList.get());
            return elements;
        }

        private Element generateLocationLink(final Location location) {
            final Element link = $(A);
            link.setTextContent(TRAVEL_TO + location.getLocationName() + OF
                    + location.getLocationSuperSet().getLocationName());
            link.setAttribute(A.href(),
                    PAGE + location.getLocationName() + _OF_ + location.getLocationSuperSet().getLocationName()
                            + Parameter.get(Location.WOEID, location.getWOEID().toString(), true));

            link.setAttribute(A.alt(), PAGE + location.getLocationName() + _OF_
                    + location.getLocationSuperSet().getLocationName());
            return link;
        }

        private Element generateSimpleLocationLink(final Location location) {
            final Element link = $(A);
            link.setTextContent(location.getLocationName());
            link.setAttribute(A.href(), PAGE + location.getLocationName()
                    + Parameter.get(Location.WOEID, location.getWOEID().toString(), true));

            link.setAttribute(A.alt(), PAGE + location.getLocationName());
            return link;
        }
    };//Listener
}

From source file:Collector.TweetCollector.java

public static List<Status> getTweets(final String q) {
    Timer timer = new Timer();
    TimerTask hourlyTask = new TimerTask() {

        @Override/*from  www .  j  a va2 s .  c  o m*/
        public void run() {

            long amountOfTweets = 0;

            try {

                long maxID = -1;

                Query query = new Query(q);
                //printTimeLine(query);
                Map<String, RateLimitStatus> rateLimitStatus = twitter.getRateLimitStatus("search");
                RateLimitStatus searchLimit = rateLimitStatus.get("/search/tweets");
                for (int batchNumber = 0; MAX_QUERIES < 10; batchNumber++) {

                    System.out.printf("\n\n!!! batch %d\n\n", batchNumber);

                    if (searchLimit.getRemaining() == 0) {
                        // so as to not get blocked by twitter
                        Thread.sleep(searchLimit.getSecondsUntilReset() + 3 * 1001);
                    }

                    query.setCount(TWEETS_PER_QUERY);// constant value of 100
                    query.setResultType(Query.ResultType.recent);
                    query.setLang("en");// only english tweets

                    if (maxID != -1) {
                        query.setMaxId(maxID - 1);// so the first querys not set to previous max
                    }
                    QueryResult result = twitter.search(query);
                    if (result.getTweets().size() == 0) {
                        break;
                    }

                    for (Status s : result.getTweets()) {
                        amountOfTweets++;
                        if (maxID == -1 || s.getId() < maxID) {
                            maxID = s.getId();
                        }
                        storeTweet(s);// where stored in db

                        System.out.printf("At%s : %s\n", // debugging purposes
                                s.getCreatedAt().toString(), s.getText());
                        searchLimit = result.getRateLimitStatus(); //resets
                        System.out.printf("\n\nA total of %d tweet retrieved\n", amountOfTweets);

                    }

                }

            } catch (TwitterException te) {

                System.out.println("Error Code :" + te.getErrorCode());
                System.out.println("Exception Code " + te.getExceptionCode());
                System.out.println("Status Code " + te.getStatusCode());

                if (te.getStatusCode() == 401) {
                    System.out.println("Twitter Error :\nAuthentication "
                            + "credentials (https://dev.twitter.com/auth) "
                            + " are either missing of incorrect, " + "\nplease check consumer key /secret");
                }
            } catch (InterruptedException ex) {

            }

        }
    };

    // schedule the task to run starting now and then every hour...
    timer.schedule(hourlyTask, 0l, 1000 * 60 * 60);
    return statuses;

}

From source file:com.daemon.Minion.java

License:Open Source License

/**
 * Sends a request specified as search term to Twitter and returns the
 * results Twitter returned.//from  w ww  .  j ava  2  s. co  m
 * 
 * @param term The search term object.
 * @return The answer of the sent request as QueryResult.
 * @throws TwitterException Thrown whenever there is a problem querying Twitter
 * (i. e the Rate Limit was reached).
 */
public QueryResult search(SearchTerm term) throws TwitterException {
    Query query = new Query(term.getTerm());
    query.setCount(100);
    query.setResultType(Query.RECENT);
    if (term.getLastFetchedTweetId() == null) {
        // Start a new backwards search fron the current given start
        if (term.getOldStart() == null)
            query.setUntil(Localization.DATETIME_FORMATTER.print(term.getCurrentStart().plusDays(1)));
    } else
        // Continue the current search from the last fetched tweet id
        query.setMaxId(term.getLastFetchedTweetId());

    return _twitter.search(query);
}

From source file:com.ibm.TwitterAsyncService.java

License:Open Source License

@Override
public void run() {
    String bookTitle = ac.getRequest().getParameter("title");
    String bookAuthor = ac.getRequest().getParameter("author");
    boolean translate = Boolean.parseBoolean(ac.getRequest().getParameter("enable"));

    logger.debug("Requested book title {} and author {}", bookTitle, bookAuthor);

    Query query = new Query(bookTitle + " " + bookAuthor);
    query.setResultType(Query.RECENT);

    Twitter twitter = TwitterFactory.getSingleton();

    try {/* w w  w  .ja  v a 2 s.c om*/
        PrintWriter writer = ac.getResponse().getWriter();
        Locale locale = ac.getRequest().getLocale();
        WatsonTranslate watson = new WatsonTranslate(locale);

        // Just get the first page of results to avoid exceeding the Twitter rate limit
        QueryResult result = twitter.search(query);

        Alchemy alchemy = new Alchemy();

        List<Status> tweets = result.getTweets();

        logger.debug("Current tweets {}", tweets.toString());

        for (Status tweetMessage : tweets) {
            JSONObject json = new JSONObject();
            JSONObject tweet = new JSONObject();
            // We need to put the tweet and link into an inner object
            // so that we can use a special formatter in bootstrap table

            json.put("screenName", tweetMessage.getUser().getScreenName());

            if (translate) {
                String message = watson.translate(tweetMessage.getText());
                tweet.put("message", message);
                json.put("sentiment", alchemy.getSentiment(message));
            } else {
                tweet.put("message", tweetMessage.getText());
                json.put("sentiment", alchemy.getSentiment(tweetMessage.getText()));
            }

            String url = "https://twitter.com/" + tweetMessage.getUser().getScreenName() + "/status/"
                    + tweetMessage.getId();

            tweet.put("link", url);

            json.put("tweet", tweet);

            writer.write(("data: " + json.toString() + "\n\n"));
            writer.flush();
        }

        writer.write(("event: finished\n"));
        writer.write(("data: \n\n"));
        writer.flush();
        writer.close();
    } catch (TwitterException e) {
        logger.error("Twitter Error {}", e.getMessage());
    } catch (IOException e) {
        logger.error("could not write SSE {}", e.getMessage());
    } catch (NullPointerException e) {
        logger.error("Exception Twitter Async Service {}", e.getMessage());
    } finally {
        ac.complete();
    }
}

From source file:com.twitter.TwitterCrawler.java

/**
 *
 * @return @throws TwitterException/*  ww w .ja  v a2  s  .c  o  m*/
 * @throws InterruptedException returns the number of tweets
 */
public TwitterApiResponse stream() throws TwitterException, InterruptedException {
    //************************ Variables *************************
    TwitterApiResponse response = new TwitterApiResponse();
    long timeNow = System.currentTimeMillis();
    long afterInterval = timeNow + (interval * 1000);
    int countTweets = 0;
    //define the tweet id that the application should read relative to the IDs of Tweets it has already processed
    long maxID = 0;
    ArrayList<Status> tweets = new ArrayList<Status>();

    Query query = new Query(keywords);

    //************************ Action *************************
    //max 450 calls in 15 mins -->30/min, 5 calls/10 secs
    while (timeNow < afterInterval) {
        try {
            if (sinceID != 0) {
                query.setSinceId(sinceID);
            }

            if (maxID != 0) {
                query.setMaxId(maxID);
            }

            query.setResultType(Query.ResultType.recent);
            query.setCount(100);
            QueryResult result = twitter.search(query);
            tweets.addAll(result.getTweets());

            if (tweets.size() == 100) {
                countTweets += tweets.size();
                maxID = tweets.get(tweets.size() - 1).getId() - 1;
            } else {
                countTweets += tweets.size();
                break;
            }
            timeNow = System.currentTimeMillis();
        } catch (TwitterException te) {
            System.out.println("Couldn't connect: " + te);
        }
    }

    sinceID = tweets.get(0).getId();
    response.setCount(countTweets);
    response.setSinceID(sinceID);

    return response;
}

From source file:de.jetsli.twitter.TwitterSearch.java

License:Apache License

Query createQuery(String str) {
    Query q = new Query(str);
    q.setResultType(Query.RECENT);
    return q;
}

From source file:de.jetsli.twitter.TwitterSearch.java

License:Apache License

long search(String term, Collection<Status> result, Map<String, User> userMap, int tweets,
        long lastMaxCreateTime) throws TwitterException {
    long maxId = 0L;
    long maxMillis = 0L;
    // TODO it looks like only one page is possible with 4.0.0
    int maxPages = 1;
    int hitsPerPage = tweets;

    boolean breakPaging = false;
    for (int page = 0; page < maxPages; page++) {
        Query query = new Query(term);
        // RECENT or POPULAR
        query.setResultType(Query.MIXED);

        // avoid that more recent results disturb our paging!
        if (page > 0)
            query.setMaxId(maxId);//ww w .ja  va2  s.  c  om

        query.setCount(hitsPerPage);
        QueryResult res = twitter.search(query);

        // is res.getTweets() sorted?
        for (Status twe : res.getTweets()) {
            // determine maxId in the first page
            if (page == 0 && maxId < twe.getId())
                maxId = twe.getId();

            if (maxMillis < twe.getCreatedAt().getTime())
                maxMillis = twe.getCreatedAt().getTime();

            if (twe.getCreatedAt().getTime() + 1000 < lastMaxCreateTime)
                breakPaging = true;
            else {
                String userName = twe.getUser().getScreenName().toLowerCase();
                User user = userMap.get(userName);
                if (user == null)
                    userMap.put(userName, twe.getUser());

                result.add(twe);
            }
        }

        // minMillis could force us to leave earlier than defined by maxPages
        // or if resulting tweets are less then request (but -10 because of twitter strangeness)
        if (breakPaging || res.getTweets().size() < hitsPerPage - 10)
            break;
    }

    return maxMillis;
}

From source file:de.jetwick.tw.NewClass.java

License:Apache License

/** A thread using the search API */
public Thread search() {
    return new Thread() {

        @Override/*from  w w  w .java2s  . c om*/
        public void run() {
            int MINUTES = 2;
            Twitter twitter = new TwitterFactory().getInstance();
            try {
                while (!isInterrupted()) {
                    Query query = new Query(queryTerms);
                    // RECENT or POPULAR or MIXED
                    // doesn't make a difference if MIXED or RECENT
                    query.setResultType(Query.MIXED);
                    query.setPage(1);
                    query.setRpp(100);
                    QueryResult res = twitter.search(query);
                    for (Tweet tw : res.getTweets()) {
                        searchMap.put(tw.getId(), tw.getText());
                    }
                    Thread.sleep(MINUTES * 60 * 1000L);
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    };

}

From source file:de.jetwick.tw.TwitterSearch.java

License:Apache License

long search(String term, Collection<JTweet> result, Map<String, JUser> userMap, int tweets,
        long lastMaxCreateTime) throws TwitterException {
    long maxId = 0L;
    long maxMillis = 0L;
    int hitsPerPage;
    int maxPages;
    if (tweets < 100) {
        hitsPerPage = tweets;//from ww w.j av  a 2 s.c om
        maxPages = 1;
    } else {
        hitsPerPage = 100;
        maxPages = tweets / hitsPerPage;
        if (tweets % hitsPerPage > 0)
            maxPages++;
    }

    boolean breakPaging = false;
    for (int page = 0; page < maxPages; page++) {
        Query query = new Query(term);
        // RECENT or POPULAR
        query.setResultType(Query.MIXED);

        // avoid that more recent results disturb our paging!
        if (page > 0)
            query.setMaxId(maxId);

        query.setPage(page + 1);
        query.setRpp(hitsPerPage);
        QueryResult res = twitter.search(query);

        // is res.getTweets() sorted?
        for (Object o : res.getTweets()) {
            Tweet twe = (Tweet) o;
            // determine maxId in the first page
            if (page == 0 && maxId < twe.getId())
                maxId = twe.getId();

            if (maxMillis < twe.getCreatedAt().getTime())
                maxMillis = twe.getCreatedAt().getTime();

            if (twe.getCreatedAt().getTime() + 1000 < lastMaxCreateTime)
                breakPaging = true;
            else {
                String userName = twe.getFromUser().toLowerCase();
                JUser user = userMap.get(userName);
                if (user == null) {
                    user = new JUser(userName).init(twe);
                    userMap.put(userName, user);
                }

                result.add(new JTweet(twe, user));
            }
        }

        // minMillis could force us to leave earlier than defined by maxPages
        // or if resulting tweets are less then request (but -10 because of twitter strangeness)
        if (breakPaging || res.getTweets().size() < hitsPerPage - 10)
            break;
    }

    return maxMillis;
}

From source file:dhbw.clippinggorilla.external.twitter.TwitterUtils.java

/**
 * @param includedTagsSet/*from  w w  w .j  a v a2s. c o 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;

}