Example usage for twitter4j GeoLocation getLongitude

List of usage examples for twitter4j GeoLocation getLongitude

Introduction

In this page you can find the example usage for twitter4j GeoLocation getLongitude.

Prototype

public double getLongitude() 

Source Link

Document

returns the longitude of the geo location

Usage

From source file:uk.co.cathtanconsulting.twitter.TwitterSource.java

License:Apache License

/**
 * @param status// w w w .  jav a2 s  . c om
 * 
 * Generate a TweetRecord object and submit to 
 * 
 */
private void extractRecord(Status status) {
    TweetRecord record = new TweetRecord();

    //Basic attributes
    record.setId(status.getId());
    //Using SimpleDateFormat "yyyy-MM-dd'T'HH:mm:ss'Z'"
    record.setCreatedAtStr(formatterTo.format(status.getCreatedAt()));
    //Use millis since epoch since Avro doesn't do dates
    record.setCreatedAtLong(status.getCreatedAt().getTime());
    record.setTweet(status.getText());

    //User based attributes - denormalized to keep the Source stateless
    //but also so that we can see user attributes changing over time.
    //N.B. we could of course fork this off as a separate stream of user info
    User user = status.getUser();
    record.setUserId(user.getId());
    record.setUserScreenName(user.getScreenName());
    record.setUserFollowersCount(user.getFollowersCount());
    record.setUserFriendsCount(user.getFriendsCount());
    record.setUserLocation(user.getLocation());

    //If it is zero then leave the value null
    if (status.getInReplyToStatusId() != 0) {
        record.setInReplyToStatusId(status.getInReplyToStatusId());
    }

    //If it is zero then leave the value null
    if (status.getInReplyToUserId() != 0) {
        record.setInReplyToUserId(status.getInReplyToUserId());
    }

    //Do geo. N.B. Twitter4J doesn't give use the geo type
    GeoLocation geo = status.getGeoLocation();
    if (geo != null) {
        record.setGeoLat(geo.getLatitude());
        record.setGeoLong(geo.getLongitude());
    }

    //If a status is a retweet then the original tweet gets bundled in
    //Because we can't guarantee that we'll have the original we can
    //extract the original tweet and process it as we have done this time
    //using recursion. Note: we will end up with dupes.
    Status retweetedStatus = status.getRetweetedStatus();
    if (retweetedStatus != null) {
        record.setRetweetedStatusId(retweetedStatus.getId());
        record.setRetweetedUserId(retweetedStatus.getUser().getId());
        extractRecord(retweetedStatus);
    }

    //Submit the populated record onto the channel
    processRecord(record);
}

From source file:wise.TwitterUtils.java

public ArrayList<CheckinObject> dataList(String cityCode, String selectedDate, String maxTweetParam,
        String topTweetParam) throws IOException, JSONException, ParseException {
    ArrayList<CheckinObject> checkinList = new ArrayList<>();

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey(Constants.CONSUMER_KEY)
            .setOAuthConsumerSecret(Constants.CONSUMER_SECRET).setOAuthAccessToken(Constants.TOKEN)
            .setOAuthAccessTokenSecret(Constants.TOKEN_SECRET);

    Integer maxTweet = Integer.parseInt(maxTweetParam);
    Integer topTweet = Integer.parseInt(topTweetParam);

    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();//  ww  w .j  av  a  2  s.c o  m

    DateFormat format = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH);

    Date startDate = format.parse(selectedDate);

    Calendar c = Calendar.getInstance();
    c.setTime(startDate);
    c.add(Calendar.DATE, 1);
    Date endDate = c.getTime();

    Query query = SetQueryString(cityCode, startDate, endDate);

    QueryResult result = null;
    try {
        while (query != null && checkinList.size() <= maxTweet) {
            result = twitter.search(query);
            if (result != null) {
                for (Status status : result.getTweets()) {

                    for (URLEntity urlEntity : status.getURLEntities()) {
                        String urlCheckinId = urlEntity.getExpandedURL()
                                .substring(urlEntity.getExpandedURL().lastIndexOf("/") + 1);
                        GeoLocation geo = status.getGeoLocation();

                        CheckinObject checkin = new CheckinObject();
                        checkin.CheckinId = urlCheckinId;
                        checkin.Count = 1;//status.getRetweetCount() + status.getFavoriteCount() + 1;
                        checkin.Latitude = geo.getLatitude();
                        checkin.Longitude = geo.getLongitude();

                        if (!checkin.containsSameCoordinates(checkinList, checkin.Latitude,
                                checkin.Longitude)) {
                            checkinList.add(checkin);
                        } else {
                            int indexOfCheckinId = checkin.getIndexByCoordinates(checkinList, checkin.Latitude,
                                    checkin.Longitude);
                            checkinList.get(indexOfCheckinId).setCheckinCount(checkin.Count);
                            checkinList.get(indexOfCheckinId).setReplacementCheckinId(checkin.CheckinId);
                        }
                    }
                }

                query = result.nextQuery();
            } else {
                query = null;
            }
        }

    } catch (TwitterException e) {
    }

    Collections.sort(checkinList, new CountComparator());

    if (checkinList.size() > topTweet) {
        checkinList.subList(topTweet, checkinList.size()).clear(); // get top x
    }

    FourSquareCheckin fsq = new FourSquareCheckin();

    for (CheckinObject checkin : checkinList) {
        fsq = getVenueInfo(checkin);
        checkin.LocationName = fsq.VenueName;
        checkin.Image = fsq.VenueImage;
    }

    return checkinList;
}