Example usage for twitter4j Status getId

List of usage examples for twitter4j Status getId

Introduction

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

Prototype

long getId();

Source Link

Document

Returns the id of the status

Usage

From source file:crawling.GetUserTimelineByFile.java

License:Apache License

public static void main(String[] args) {

    if (args.length < 1) {
        System.out.println("Usage: java twitter4j.examples.GetUserTimelineByFile UserFile");
        System.exit(-1);//from  w w  w .ja v  a 2  s .  c  om
    }
    userFile = args[0];

    try {
        FileWriter outFileWhole = new FileWriter("CrawlTweets" + ".txt", true);
        out = new PrintWriter(outFileWhole);

        // out.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    File inFile = new File(userFile);

    if (!inFile.isFile()) {
        System.out.println("Parameter is not an existing file");
        return;
    }

    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader(userFile));
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    // gets Twitter instance with default credentials
    Twitter twitter = new TwitterFactory().getInstance();
    int count = 0;
    int totalCount = 0;

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy, MM, dd");
    Date start = null;
    try {
        start = sdf.parse("2000, 1, 1");
    } catch (ParseException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    List<Status> statuses = null;
    // String user = "Porter_Anderson";
    // user = "pqtad";
    // user = "paradunaa6";
    // user = "palifarous";

    ArrayList<Long> users = new ArrayList<Long>();

    String line = null;

    //Read from the original file and write to the new
    //unless content matches data to be removed.
    try {
        while ((line = br.readLine()) != null)
            users.add(Long.parseLong(line));
        br.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    //users.add(584928891L);
    //users.add(700425265L);

    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    Date date = new Date();
    System.out.println();
    System.out.println("----------------------------------------------");

    System.out.println("Start at: " + dateFormat.format(date));

    Long sinceID =
            //218903304682471424L; // 2012/06/29
            238893053518151680L; // 2012/08/24
    Long maxID = 250409135600975873L; // 2012/09/24
    //238893053518151680L;   // 2012/08/24
    // 227659323180974080L;   // 2012/07/24

    boolean overflow = false;
    for (Long usr : users) {
        System.out.println("%" + usr);
        out.println("%" + usr);
        out.println("%" + usr);
        Paging paging = null;
        count = 0;
        for (int i = 1; i < 21; i++) {
            //paging = new Paging(i, 200);
            paging = new Paging(i, 200, sinceID, maxID);

            overflow = false;
            try {
                // statuses = twitter.getUserTimeline(user, paging);
                statuses = twitter.getUserTimeline(usr, paging);
                Thread.currentThread();
                try {
                    Thread.sleep(10500);
                } 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.isEmpty()) {
                if (i > 16)
                    overflow = true;
                break;
            }
            for (Status status : statuses) {
                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());
                record += "::" +
                // status.getCreatedAt();
                        (status.getCreatedAt().getTime() - start.getTime()) / 1000;

                record += "::" + getSource(status.getSource());
                //System.out.println(record);
                out.println(record);
            }

            count += statuses.size();
            out.flush();
        }
        totalCount += count;
        out.println("%" + usr + ", " + count + ", " + overflow);
        System.out.println("%" + usr + ", " + count + ", " + overflow);
        out.println("------------------------------------------");
    }
    System.out.println("Total status count is " + totalCount);
    out.println("#" + totalCount);
    out.close();
    date = new Date();
    System.out.println();
    System.out.println("----------------------------------------------");

    System.out.println("End at: " + dateFormat.format(date));
}

From source file:crawling.GetUserTimelineByFileDiff.java

License:Apache License

public static void main(String[] args) {

    if (args.length < 1) {
        System.out.println("Usage: java twitter4j.examples.GetUserTimelineByFile UserFile");
        System.exit(-1);/*from  ww w .  j a v a  2  s  .c  o m*/
    }
    userFile = args[0];

    try {
        FileWriter outFileWhole = new FileWriter("CrawlTweets" + ".txt", true);
        outWhole = new PrintWriter(outFileWhole);

        FileWriter outFileInter = new FileWriter("CrawlInter" + ".txt", true);
        outInter = new PrintWriter(outFileInter);
        // out.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    File inFile = new File(userFile);

    if (!inFile.isFile()) {
        System.out.println("Parameter is not an existing file");
        return;
    }

    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader(userFile));
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    // gets Twitter instance with default credentials
    Twitter twitter = new TwitterFactory().getInstance();
    int count = 0;
    int countInter = 0;
    int totalCount = 0;
    int totalCountInter = 0;

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy, MM, dd");
    Date start = null;
    try {
        start = sdf.parse("2000, 1, 1");
    } catch (ParseException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    List<Status> statuses = null;
    // String user = "Porter_Anderson";
    // user = "pqtad";
    // user = "paradunaa6";
    // user = "palifarous";
    // statuses = twitter.getUserTimeline(user);
    //String usersS = "490495670,210462629,731570426,174646182,381163846,308812526,595154838,47356666,590795627,466854690,560386951,406747522,767819150,193105498,725874775,381257304,486727765,39469575,68239634,187739283,27048882,172979072,16075589,618047540,841958365,707631710,76274554,334451278,573978436,782711790,498017142,764236789,88185343,293950091,726606902,221410697,566098451,32987449,73497385,223182159,571182929,769441794,16225570,404748449,249953519,713693096,727692930,178473099,366957548,255171527,411892240,370333418,753509292,765928471,49389074,422867487,185298515,112841821,524731824,132207254,431289858,35602356,747638244,76398937,276388822,221106768,608597994,532423674,509070257,235773945,263995811,61575477,48606725,518136068,450781132,631504150,842236698,840479664,306291201,59829276,541212815,264436593,262466303,302157661,135587748,399229428,265344815,225280446,540254332,218019401,260453139,187424025,177051847,351065900,406684867,293234971,219356963,280763734,59290083,214750688";

    //String[] splits = usersS.split(",");
    ArrayList<Long> users = new ArrayList<Long>();

    String line = null;

    //Read from the original file and write to the new
    //unless content matches data to be removed.
    try {
        while ((line = br.readLine()) != null) {

            users.add(Long.parseLong(line));
        }
        br.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    //users.add(584928891L);
    //users.add(700425265L);

    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    Date date = new Date();
    System.out.println();
    System.out.println("----------------------------------------------");

    System.out.println("Start at: " + dateFormat.format(date));

    Long sinceID =
            //218903304682471424L; // 2012/06/29
            238893053518151680L; // 2012/08/24
    Long maxID = 250409135600975873L; // 2012/09/24
    //238893053518151680L;   // 2012/08/24
    // 227659323180974080L;   // 2012/07/24

    boolean overflow = false;
    for (Long usr : users) {
        System.out.println("%" + usr);
        outWhole.println("%" + usr);
        outInter.println("%" + usr);
        Paging paging = null;
        count = 0;
        countInter = 0;
        for (int i = 1; i < 21; i++) {
            //paging = new Paging(i, 200);
            paging = new Paging(i, 200, sinceID, maxID);

            overflow = false;
            try {
                // statuses = twitter.getUserTimeline(user, paging);
                statuses = twitter.getUserTimeline(usr, paging);
                Thread.currentThread();
                try {
                    Thread.sleep(10500);
                } 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.isEmpty()) {
                if (i > 16)
                    overflow = true;
                break;
            }

            for (Status status : statuses) {
                boolean inter = false;
                //countRT += status.getRetweetCount();
                String record = "";
                String recordInter = "";
                record += status.getId();

                // For reply
                Long replyId = status.getInReplyToStatusId();
                if (replyId == -1)
                    record += "::-1::-1";
                else {
                    record += "::" + replyId;
                    record += "::" + status.getInReplyToUserId();
                    inter = true;
                }

                // For retweeting
                Long retweets = (long) status.getRetweetCount();
                record += "::" + retweets;
                if (retweets > 0) {
                    inter = true;

                    if (status.getRetweetedStatus() != null)
                        record += "::" + status.getRetweetedStatus().getId();
                    else
                        record += "::" + "-1"; // The source of retweeting
                }
                //record += "::" + status.isRetweet();
                // For mentions
                int len = status.getUserMentionEntities().length;
                if (len > 0) {
                    record += "::";
                    for (int l = 0; l < len; l++) {
                        UserMentionEntity ent = status.getUserMentionEntities()[l];
                        record += "," + ent.getId();
                    }
                    inter = true;
                } else
                    record += "::" + "-1";

                recordInter += record;

                // For URL
                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";

                // For text
                record += "::" + cleanText(status.getText());

                // For creating time
                // record += "::" + status.getCreatedAt().toString();
                Long seconds =
                        // status.getCreatedAt();
                        (status.getCreatedAt().getTime() - start.getTime()) / 1000;
                record += "::" + seconds;
                recordInter += "::" + seconds;

                // For publishing source
                record += "::" + getSource(status.getSource());
                //System.out.println(record);
                outWhole.println(record);

                if (inter) {
                    outInter.println(recordInter);
                    countInter++;
                }
            }

            count += statuses.size();
            outWhole.flush();
            outInter.flush();
        }
        totalCount += count;
        totalCountInter += countInter;
        outWhole.println("%" + usr + "," + count + "," + countInter + "," + overflow);
        outInter.println("%" + usr + "," + count + "," + countInter + "," + overflow);
        System.out.println("%" + usr + "," + count + "," + countInter + "," + overflow);
        outWhole.println("------------------------------------------");
        outInter.println("------------------------------------------");
    }
    System.out.println("Total status count is " + totalCount + "," + totalCountInter);
    outWhole.println("#" + totalCount + "," + totalCountInter);
    outInter.println("#" + totalCount + "," + totalCountInter);
    outWhole.close();
    outInter.close();
    date = new Date();
    System.out.println();
    System.out.println("----------------------------------------------");

    System.out.println("End at: " + dateFormat.format(date));
}

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;//from   www . java  2s  . c om
        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 doASearch(Twitter twitter, Query query) throws TwitterException {
    //try {/*from w ww.  j  av a  2 s. co m*/
    QueryResult result = twitter.search(query);
    List<Status> tweets = result.getTweets();
    int thisCount = 0;
    for (Status tweet : tweets) {

        // Check the tweet
        Long id = tweet.getId();

        if (discoveredTweets.containsKey(id)) {
            //System.out.println("Already found this user: " + id);
            long num = discoveredTweets.get(id);
            discoveredTweets.put(id, num + 1);
        } else {
            discoveredTweets.put(id, (long) 1);
            storeATweet(tweet);
            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 tweets 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.SearchTweetsHashtag.java

License:Apache License

private static void storeATweet(Status status) {
    int len = status.getUserMentionEntities().length;
    /*/*from  w ww.  j  av  a2  s .  c  om*/
     * 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   w  w w .j  a v  a  2 s .  c o m
        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 {/*  w  w  w .  j a v a2  s .  co m*/
        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()));
    }//from  w  w  w . j  av a 2s .c  om
    //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:DataCollections.UserTimeLineCollection.java

public void collect_InsertTimeLineOfUser(User_dbo user) throws InterruptedException {

    Paging p = new Paging();
    int count = 20;
    p.setCount(count);//w w  w  . j  a  v  a  2  s . c om
    long max_id, since_id;
    int totaltweets = 0;
    long timestamp = -1;
    int nooftweets = 0;
    if (user.values[User_dbo.map.get("max_id")].used) {
        max_id = user.values[User_dbo.map.get("max_id")].lnumber;
        p.setMaxId(max_id);
    } else {
        max_id = -1;
    }
    if (user.values[User_dbo.map.get("since_id")].used) {
        since_id = user.values[User_dbo.map.get("since_id")].lnumber;
    } else {
        since_id = -1;
    }

    ResponseList<Status> statuses = null;
    boolean available = true;
    while (available) {

        try {
            LogPrinter.printLog("Retrieving some more tweets....");
            statuses = timelinesres.getUserTimeline(user.values[User_dbo.map.get("user_id")].lnumber, p);
        } catch (Exception e) {
            e.printStackTrace();
            TwitterException te = (TwitterException) e;
            if (te.exceededRateLimitation()) {
                LogPrinter.printLog("Rate Limited Reached.. Sleeping.. for ms " + 900 * 1000);
                Thread.sleep(900 * 1000 + 500);
            } else {

                return;
            }

        }
        totaltweets += statuses.size();
        if (statuses.isEmpty()) {
            LogPrinter.printLog("All tweets are retrieved....");
            available = false;
            continue;

        }
        ListIterator li = statuses.listIterator();
        while (li.hasNext()) {
            Status s = (Status) li.next();
            if (since_id < 0 && nooftweets == 0) {
                since_id = s.getId();
            }
            Tweet_dbo tweet = tweethelper.convertStatusToTweet_dbo(s);
            Tweet_dbo[] currentweets = TweetsTable
                    .select(" tweet_id = " + tweet.values[Tweet_dbo.map.get("tweet_id")].lnumber, 0, 2);
            if (currentweets.length == 0) {
                tweet.values[Tweet_dbo.map.get("processed")].setValue("true");
                tweet.values[Tweet_dbo.map.get("f_usertimeline")].setValue("true");
                TweetsTable.insert(tweet);
                useredgecollections.extract_InsertUsers_EdgesFromTweet(s);
                nooftweets++;
                LogPrinter.printLog("Inserting a new tweet.." + nooftweets);
            } else {
                LogPrinter.printLog("This tweet already exists in the database...");
            }
        }
        if (!statuses.isEmpty()) {
            max_id = ((Status) statuses.get(statuses.size() - 1)).getId();
            p.setMaxId(max_id);
        }
        if (totaltweets > 250) {
            LogPrinter.printLog("No of tweets collected reached goal....");
            available = false;
            continue;

        }

    }
    LogPrinter.printLog(String.valueOf(max_id));
    LogPrinter.printLog(String.valueOf(since_id));
    if (since_id > 0 && max_id > 0) {
        boolean selected[] = new boolean[User_dbo.nooffields];
        selected[User_dbo.map.get("max_id")] = true;
        selected[User_dbo.map.get("since_id")] = true;
        selected[User_dbo.map.get("utimeline_processed")] = true;
        user.values[User_dbo.map.get("max_id")].setValue(String.valueOf(max_id));
        user.values[User_dbo.map.get("since_id")].setValue(String.valueOf(since_id));
        user.values[User_dbo.map.get("utimeline_processed")].setValue("true");
        UsersTable.update(user, selected, " user_id =  " + user.values[User_dbo.map.get("user_id")].lnumber);
    }

}

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

License:Open Source License

/**
 * Translate twitter status.//from w w w. ja  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;
}