Example usage for twitter4j Status getGeoLocation

List of usage examples for twitter4j Status getGeoLocation

Introduction

In this page you can find the example usage for twitter4j Status getGeoLocation.

Prototype

GeoLocation getGeoLocation();

Source Link

Document

Returns The location that this tweet refers to if available.

Usage

From source file:crawler.DataStorage.java

License:Apache License

private static void sqlStore(Status status) throws SQLException {
    long sql_pid = Settings.pid;
    Settings.pid++;// ww w . ja v a 2 s  . com

    SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd, HH:mm:ss, z");
    String sqlCreateAt = tempDate.format(new java.util.Date(status.getCreatedAt().getTime()));

    double sqlGeoLocationLat = 0;
    double sqlGeoLocationLong = 0;

    if (status.getGeoLocation() != null) {
        sqlGeoLocationLat = status.getGeoLocation().getLatitude();
        sqlGeoLocationLong = status.getGeoLocation().getLongitude();
    }

    String sqlPlace = (status.getPlace() != null ? status.getPlace().getFullName() : "");
    long sqlId = status.getId();
    String sqlTweet = status.getText().replace("'", "''");
    String sqlSource = status.getSource().replace("'", "''");
    sqlSource = sqlSource.replace("\\", "\\\\");
    String sqlLang = status.getUser().getLang();
    String sqlScreenName = status.getUser().getScreenName();
    String sqlReplyTo = status.getInReplyToScreenName();
    long sqlRtCount = status.getRetweetCount();

    HashtagEntity[] hashs = status.getHashtagEntities();
    String sqlHashtags = "";
    for (HashtagEntity hash : hashs)
        sqlHashtags += hash.getText() + " ";

    pstm.setLong(1, sql_pid);
    pstm.setString(2, sqlCreateAt);
    pstm.setDouble(3, sqlGeoLocationLat);
    pstm.setDouble(4, sqlGeoLocationLong);
    pstm.setString(5, sqlPlace);
    pstm.setLong(6, sqlId);
    pstm.setString(7, sqlTweet);
    pstm.setString(8, sqlSource);
    pstm.setString(9, sqlLang);
    pstm.setString(10, sqlScreenName);
    pstm.setString(11, sqlReplyTo);
    pstm.setLong(12, sqlRtCount);
    pstm.setString(13, sqlHashtags);

    pstm.addBatch();
}

From source file:crawling.FoundUsersBySearchGeo.java

License:Apache License

private static void doASearch(Twitter twitter, Query query) throws TwitterException {
    //try {//from   w w w.  j  a v a  2 s. co m
    QueryResult result = twitter.search(query);
    List<Status> tweets = result.getTweets();
    int thisCount = 0;
    for (Status tweet : tweets) {
        //System.out.println("@" + tweet.getFromUser() + tweet.getId() + " - " + tweet.getText());

        // Check the tweet
        Long id = tweet.getUser().getId();
        String screenName = tweet.getUser().getScreenName();

        /*String location = "";
        if (tweet.getUser().getLocation() != null){
          location += tweet.getUser().getLocation();
        if (tweet.getGeoLocation() != null){
          location += "::" + tweet.getGeoLocation().toString();
        }*/
        String location = tweet.getUser().getLocation() + "::" + tweet.getGeoLocation();
        // If the tweet is a retweet, the source of the tweet is from the target area
        if (tweet.isRetweet()) {
            id = tweet.getRetweetedStatus().getUser().getId();
            screenName = tweet.getRetweetedStatus().getUser().getScreenName();
            //location = tweet.getRetweetedStatus().getUser().getLocation();
            location = tweet.getRetweetedStatus().getUser().getLocation() + "::"
                    + tweet.getRetweetedStatus().getGeoLocation();
        }

        if (discoveredUsers.containsKey(id)) {
            //System.out.println("Already found this user: " + id);
            long num = discoveredUsers.get(id);
            discoveredUsers.put(id, num + 1);
        } else {
            discoveredUsers.put(id, (long) 1);
            storeUserID(id, screenName, location);
            thisCount++;
        }
    }
    if (currentIndex < histCount) {
        avgUsers[currentIndex] = thisCount;
        currentIndex++;
    } else {
        currentIndex = 0;
    }

    /* Calculate the average #users in last 10 times */
    double sum = 0;
    for (int num : avgUsers)
        sum += num;
    System.out.print(
            "\r" + count + ", the average number of users in last " + histCount + " is: " + sum / histCount);

    //} catch (TwitterException te) {
    //    te.printStackTrace();
    //    System.out.println("Failed to search tweets: " + te.getMessage());
    //    System.exit(-1);
    //}

}

From source file:crawling.FoundUsersBySearchHashtag.java

License:Apache License

private static void doASearch(Twitter twitter, Query query) throws TwitterException {
    //try {//w ww  .j  av a 2s . c om
    QueryResult result = twitter.search(query);
    List<Status> tweets = result.getTweets();
    int thisCount = 0;
    for (Status tweet : tweets) {
        //System.out.println("@" + tweet.getFromUser() + tweet.getId() + " - " + tweet.getText());

        // Check the tweet
        Long id = tweet.getUser().getId();
        String screenName = tweet.getUser().getScreenName();

        /*String location = "";
        if (tweet.getUser().getLocation() != null){
          location += tweet.getUser().getLocation();
        if (tweet.getGeoLocation() != null){
          location += "::" + tweet.getGeoLocation().toString();
        }*/
        String location = tweet.getUser().getLocation() + "::" + tweet.getGeoLocation();
        // If the tweet is a retweet, the source of the tweet is from the target area
        /*if (tweet.isRetweet()){
          id = tweet.getRetweetedStatus().getUser().getId();
          screenName = tweet.getRetweetedStatus().getUser().getScreenName();
          //location = tweet.getRetweetedStatus().getUser().getLocation();
          location = tweet.getRetweetedStatus().getUser().getLocation() + "::" + tweet.getRetweetedStatus().getGeoLocation();
        }*/

        if (discoveredUsers.containsKey(id)) {
            //System.out.println("Already found this user: " + id);
            long num = discoveredUsers.get(id);
            discoveredUsers.put(id, num + 1);
        } else {
            discoveredUsers.put(id, (long) 1);
            storeUserID(id, screenName, location);
            thisCount++;
        }
    }
    if (currentIndex < histCount) {
        avgUsers[currentIndex] = thisCount;
        currentIndex++;
    } else {
        currentIndex = 0;
    }

    /* Calculate the average #users in last 10 times */
    double sum = 0;
    for (int num : avgUsers)
        sum += num;
    System.out.print(
            "\r" + count + ", the average number of users in last " + histCount + " is: " + sum / histCount);

    //} catch (TwitterException te) {
    //    te.printStackTrace();
    //    System.out.println("Failed to search tweets: " + te.getMessage());
    //    System.exit(-1);
    //}

}

From source file:crawling.GetUserTimelineMulti.java

License:Apache License

public void doCrawl() {
    List<Status> statuses = null;
    int count = 0;
    int userIndex = 0;
    int userTotal = users.size();

    boolean overflow = false;
    for (Long usr : users) {
        //System.out.println("%" + usr);

        out.println("%" + usr);

        Paging paging = null;/* w  ww.j a  va 2s .com*/
        count = 0;
        //for (int i = 1; i < 21; i++) {
        for (int i = 1; i < 4; i++) {
            paging = new Paging(i, 200);
            //paging = new Paging(i, 200, sinceId, maxId);

            overflow = false;
            try {
                // statuses = twitter.getUserTimeline(user, paging);
                statuses = twitters.get(currentCrawl).getUserTimeline(usr, paging);
                currentCrawl++;
                if (currentCrawl == this.numCrawl)
                    currentCrawl = 0;

                /*Thread.currentThread();
                try {
                   Thread.sleep(updateFreq);
                } catch (InterruptedException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
                }*/
            } catch (TwitterException te) {
                te.printStackTrace();
                System.out.println("Failed to get timeline: " + te.getMessage());
                //System.exit(-1);
            }
            if (statuses == null) {
                break;
            }
            if (statuses.isEmpty()) {
                if (i > 16)
                    overflow = true;
                break;
            }
            for (Status status : statuses) {

                int len = status.getUserMentionEntities().length;
                /*
                 * We only need the interactions
                 */
                if (len <= 0)
                    continue;

                String record = "";
                record += status.getId();
                record += "::" + status.getInReplyToStatusId();
                record += "::" + status.getInReplyToUserId();
                record += "::" + status.getRetweetCount();
                if (status.getRetweetedStatus() != null)
                    record += "::" + status.getRetweetedStatus().getId();
                else
                    record += "::" + "-1";
                //record += "::" + status.isRetweet();
                //int len = status.getUserMentionEntities().length;
                if (len > 0) {
                    record += "::";
                    for (int l = 0; l < len; l++) {
                        UserMentionEntity ent = status.getUserMentionEntities()[l];
                        record += "," + ent.getId();
                    }
                } else {
                    record += "::" + "-1";
                }
                len = status.getURLEntities().length;
                if (len > 0) {
                    record += "::";
                    for (int l = 0; l < len; l++) {
                        URLEntity ent = status.getURLEntities()[l];
                        record += "," + ent.getURL() + "|"
                        //+ ent.getDisplayURL() + "|"
                                + ent.getExpandedURL();
                    }
                } else
                    record += "::" + "-1";
                //record += "::" + cleanText(status.getText());
                // We just ignore the text content in this crawling
                record += "::-1";
                record += "::" +
                // status.getCreatedAt();
                        (status.getCreatedAt().getTime() - start.getTime()) / 1000;

                record += "::" + getSource(status.getSource());

                // Geo Location
                if (status.getGeoLocation() != null)
                    record += "::" + status.getGeoLocation();
                else
                    record += "::" + "-1";

                //System.out.println(record);
                out.println(record);
                count++;
            }

            //count += statuses.size();

        }
        out.flush();

        totalCount += count;
        out.println("%" + usr + ", " + count + ", " + overflow);
        outUserId.println(usr + "," + count);

        //System.out.println("%" + usr + ", " + count + ", " + overflow);
        System.out.println(userIndex + "/" + userTotal + ", " + count);
        //+ ", " + (overflow ? "overflow" : "not overflow"));
        userIndex++;
        out.println("------------------------------------------");
    }
    System.out.println("Total status count is " + totalCount);
    outUserId.println("#" + totalCount);
    this.closeFile();
}

From source file:crawling.SearchTweetsHashtag.java

License:Apache License

private static void storeATweet(Status status) {
    int len = status.getUserMentionEntities().length;
    /*//from ww  w .  j  a v  a2 s  . co m
     * We only need the interactions
     */
    if (len <= 0)
        return;

    String record = "";
    record += status.getId();
    record += "::" + status.getInReplyToStatusId();
    record += "::" + status.getInReplyToUserId();
    record += "::" + status.getRetweetCount();
    if (status.getRetweetedStatus() != null)
        record += "::" + status.getRetweetedStatus().getId();
    else
        record += "::" + "-1";
    //record += "::" + status.isRetweet();
    //int len = status.getUserMentionEntities().length;
    if (len > 0) {
        record += "::";
        for (int l = 0; l < len; l++) {
            UserMentionEntity ent = status.getUserMentionEntities()[l];
            record += "," + ent.getId();
        }
    } else {
        record += "::" + "-1";
    }
    len = status.getURLEntities().length;
    if (len > 0) {
        record += "::";
        for (int l = 0; l < len; l++) {
            URLEntity ent = status.getURLEntities()[l];
            record += "," + ent.getURL() + "|"
            //+ ent.getDisplayURL() + "|"
                    + ent.getExpandedURL();
        }
    } else
        record += "::" + "-1";
    record += "::" + cleanText(status.getText());
    // We just ignore the text content in this crawling
    //record += "::-1";
    record += "::" +
    // status.getCreatedAt();
            (status.getCreatedAt().getTime() - start.getTime()) / 1000;

    record += "::" + getSource(status.getSource());

    // Geo Location
    if (status.getGeoLocation() != null)
        record += "::" + status.getGeoLocation();
    else
        record += "::" + "-1";

    record += "::" + status.getUser().getId() + "::" + status.getUser().getScreenName() + "::"
            + status.getUser().getName() + "::" + status.getCreatedAt();

    //System.out.println(record);
    out.println(record);
    out.flush();
    count++;
}

From source file:crawltweets2mongo.MonoThread.java

void getNewTweets(String keyword) {
    try {//from   www  . jav  a2 s.  c  om
        Query query = new Query(keyword);
        query.setCount(20000);
        QueryResult result;
        result = twitter.search(query);

        //System.out.println("Getting Tweets..by Key.");
        List<Status> tweets = result.getTweets();

        for (Status tweet : tweets) {

            BasicDBObject basicObj = new BasicDBObject();
            basicObj.put("user_Rname", tweet.getUser().getName());
            basicObj.put("user_name", tweet.getUser().getScreenName());
            basicObj.put("retweet_count", tweet.getRetweetCount());
            basicObj.put("tweet_followers_count", tweet.getUser().getFollowersCount());

            UserMentionEntity[] mentioned = tweet.getUserMentionEntities();
            basicObj.put("tweet_mentioned_count", mentioned.length);
            basicObj.put("tweet_ID", tweet.getId());
            basicObj.put("tweet_text", tweet.getText());
            Status temp1 = tweet.getRetweetedStatus();
            if (temp1 != null)
                basicObj.put("Re_tweet_ID", temp1.getUser().getId());
            GeoLocation loc = tweet.getGeoLocation();
            if (loc != null) {
                basicObj.put("Latitude", loc.getLatitude());
                basicObj.put("Longitude", loc.getLongitude());
            }
            basicObj.put("CreateTime", tweet.getCreatedAt());
            basicObj.put("FavoriteCount", tweet.getFavoriteCount());
            basicObj.put("user_Id", tweet.getUser().getId());

            if (tweet.getUser().getTimeZone() != null)
                basicObj.put("UsertimeZone", tweet.getUser().getTimeZone());
            if (tweet.getUser().getStatus() != null)
                basicObj.put("UserStatus", tweet.getUser().getStatus());
            //basicObj.put("tweetLocation", tweet.getPlace().getGeometryCoordinates());
            String U_Loc = tweet.getUser().getLocation();
            if (U_Loc != null)
                basicObj.put("userLocation", U_Loc);
            basicObj.put("number_of_rt", tweet.getRetweetCount());

            if (mentioned.length > 0) {
                basicObj.append("mentions", pickMentions(mentioned));
            }
            try {

                collection.insert(basicObj);
            } catch (Exception e) {
                //System.out.println("MongoDB Connection Error : " + e.getMessage());
                //                            loadMenu();
            }
        }
        collection.ensureIndex(new BasicDBObject("tweet_ID", 1), new BasicDBObject("unique", true));
    } catch (TwitterException ex) {
        java.util.logging.Logger.getLogger(MonoThread.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:crawltweets2mongo.MonoThread.java

void getNewTweets(GeoLocation myLoc, double radius) {
    try {//from w  w w  .  j  a v a 2s  .  c  om
        Query query = new Query();
        Query.Unit unit = Query.KILOMETERS; // or Query.MILES;
        query.setGeoCode(myLoc, radius, unit);
        if (radius > 200)
            query.setCount(20000);
        else
            query.setCount(20000);

        QueryResult result;
        result = this.twitter.search(query);

        //System.out.println("Getting Tweets. by Geo..");
        List<Status> tweets = result.getTweets();

        for (Status tweet : tweets) {

            BasicDBObject basicObj = new BasicDBObject();
            basicObj.put("user_Rname", tweet.getUser().getName());
            basicObj.put("user_name", tweet.getUser().getScreenName());
            basicObj.put("retweet_count", tweet.getRetweetCount());
            basicObj.put("tweet_followers_count", tweet.getUser().getFollowersCount());

            UserMentionEntity[] mentioned = tweet.getUserMentionEntities();
            basicObj.put("tweet_mentioned_count", mentioned.length);
            basicObj.put("tweet_ID", tweet.getId());
            basicObj.put("tweet_text", tweet.getText());
            Status temp1 = tweet.getRetweetedStatus();
            if (temp1 != null)
                basicObj.put("Re_tweet_ID", temp1.getUser().getId());
            GeoLocation loc = tweet.getGeoLocation();
            if (loc != null) {
                basicObj.put("Latitude", loc.getLatitude());
                basicObj.put("Longitude", loc.getLongitude());
            }
            basicObj.put("CreateTime", tweet.getCreatedAt());
            basicObj.put("FavoriteCount", tweet.getFavoriteCount());
            basicObj.put("user_Id", tweet.getUser().getId());

            if (tweet.getUser().getTimeZone() != null)
                basicObj.put("UsertimeZone", tweet.getUser().getTimeZone());
            if (tweet.getUser().getStatus() != null)
                basicObj.put("UserStatus", tweet.getUser().getStatus());
            //basicObj.put("tweetLocation", tweet.getPlace().getGeometryCoordinates());
            String U_Loc = tweet.getUser().getLocation();
            if (U_Loc != null)
                basicObj.put("userLocation", U_Loc);
            basicObj.put("number_of_rt", tweet.getRetweetCount());
            //basicObj.put("isRetweet", tweet.getPlace().getGeometryCoordinates());                
            //basicObj.put("POS", tweet.getWithheldInCountries());

            if (mentioned.length > 0) {
                basicObj.append("mentions", pickMentions(mentioned));
            }
            try {
                //items.insert(basicObj);
                collection.insert(basicObj);
            } catch (Exception e) {
                //     System.out.println("MongoDB Connection Error : " + e.getMessage());
                //                            loadMenu();
            }
        }
        collection.ensureIndex(new BasicDBObject("tweet_ID", 1), new BasicDBObject("unique", true));
    } catch (TwitterException ex) {
        java.util.logging.Logger.getLogger(MonoThread.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:DataCollections.TweetHelper.java

public Tweet_dbo convertStatusToTweet_dbo(Status s) {
    Tweet_dbo tweet = new Tweet_dbo();
    tweet.values[Tweet_dbo.map.get("tweet_id")].setValue(String.valueOf(s.getId()));
    tweet.values[Tweet_dbo.map.get("user_id")].setValue(String.valueOf(s.getUser().getId()));
    tweet.values[Tweet_dbo.map.get("user_screenname")]
            .setValue(removeEscapeCharacters(s.getUser().getScreenName()));
    if (s.getGeoLocation() != null) {
        tweet.values[Tweet_dbo.map.get("lon")].setValue(String.valueOf(s.getGeoLocation().getLongitude()));
        tweet.values[Tweet_dbo.map.get("lat")].setValue(String.valueOf(s.getGeoLocation().getLatitude()));
    }/*  w  w w  .  jav a  2  s  . co  m*/
    //tweet.values[Tweet_dbo.map.get("f_search")].setValue("true");
    tweet.values[Tweet_dbo.map.get("text")].setValue(removeEscapeCharacters(s.getText()));
    tweet.values[Tweet_dbo.map.get("hashtags")].setValue(stringifyHashtags(s.getHashtagEntities()));
    tweet.values[Tweet_dbo.map.get("mentions")].setValue(stringiyMentions(s.getUserMentionEntities()));
    tweet.values[Tweet_dbo.map.get("favouritecount")].setValue(String.valueOf(s.getFavoriteCount()));
    tweet.values[Tweet_dbo.map.get("retweetcount")].setValue(String.valueOf(s.getRetweetCount()));
    return tweet;
}

From source file:de.binfalse.jatter.processors.TwitterStatusProcessor.java

License:Open Source License

/**
 * Translate twitter status.//ww  w  . j a  v a 2 s .c o  m
 *
 * @param status
 *          the status
 * @return the string
 */
public static String translateTwitterStatus(Status status) {
    String msg = "";
    if (status.getUser() != null && status.getUser().getScreenName() != null)
        msg += "*" + status.getUser().getScreenName() + "*: ";
    if (!status.isRetweet())
        msg += JatterTools.processTwitterMessag(status.getText());
    msg += " [" + App.printDateFormat.format(status.getCreatedAt());
    if (status.getGeoLocation() != null)
        msg += " - " + status.getGeoLocation().getLatitude() + "," + status.getGeoLocation().getLongitude();
    msg += " - " + status.getId();
    msg += "]";

    if (status.isRetweet())
        msg += "\n *RT* > " + translateTwitterStatus(status.getRetweetedStatus());

    if (status.getQuotedStatus() != null)
        msg += "\n *QT* > " + translateTwitterStatus(status.getQuotedStatus());

    return msg;
}

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

License:Apache License

public static Twitter4JTweet toTweet(Status st, User user) {
    if (user == null)
        throw new IllegalArgumentException("User mustn't be null!");
    if (st == null)
        throw new IllegalArgumentException("Status mustn't be null!");

    Twitter4JTweet tw = new Twitter4JTweet(st.getId(), st.getText(), user.getScreenName());
    tw.setCreatedAt(st.getCreatedAt());//from  w w w  .  ja  v  a  2 s .  co  m
    tw.setFromUser(user.getScreenName());

    if (user.getProfileImageURL() != null)
        tw.setProfileImageUrl(user.getProfileImageURL().toString());

    tw.setSource(st.getSource());
    tw.setToUser(st.getInReplyToUserId(), st.getInReplyToScreenName());
    tw.setInReplyToStatusId(st.getInReplyToStatusId());

    if (st.getGeoLocation() != null) {
        tw.setGeoLocation(st.getGeoLocation());
        tw.setLocation(st.getGeoLocation().getLatitude() + ", " + st.getGeoLocation().getLongitude());
    } else if (st.getPlace() != null)
        tw.setLocation(st.getPlace().getCountryCode());
    else if (user.getLocation() != null)
        tw.setLocation(toStandardLocation(user.getLocation()));

    return tw;
}