Example usage for twitter4j Twitter getUserTimeline

List of usage examples for twitter4j Twitter getUserTimeline

Introduction

In this page you can find the example usage for twitter4j Twitter getUserTimeline.

Prototype

ResponseList<Status> getUserTimeline(String screenName, Paging paging) throws TwitterException;

Source Link

Document

Returns the 20 most recent statuses posted from the authenticating user.

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   www.  j  a  va 2  s  .  c o  m
    }
    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  w w  w  .ja  v  a 2s  . c  om*/
    }
    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:de.vanita5.twittnuker.loader.support.UserTimelineLoader.java

License:Open Source License

@Override
protected ResponseList<Status> getStatuses(final Twitter twitter, final Paging paging) throws TwitterException {
    if (twitter == null)
        return null;
    if (mUserId != -1)
        return twitter.getUserTimeline(mUserId, paging);
    else if (mUserScreenName != null)
        return twitter.getUserTimeline(mUserScreenName, paging);
    else/*www.  j  a  v a  2 s . com*/
        return null;
}

From source file:edu.cmu.cs.lti.discoursedb.io.twitter.converter.TwitterConverterService.java

License:Open Source License

/**
 * For each user in the mongodb dataset, import the whole timeline of that user (API limit: latest 3,200 tweets)
 * /*from  w w w . j a  va 2 s.c o m*/
 * @param users
 * @param discourseName
 * @param datasetName
 */
public void importUserTimelines(List<String> users, String discourseName, String datasetName) {
    Twitter twitter = TwitterFactory.getSingleton();

    log.info("Importing timelines for " + users.size() + " users into DiscourseDB");

    for (String screenname : users) {
        log.info("Retrieving timeline for user " + screenname);
        List<Status> tweets = new ArrayList<>();

        //There's an API limit of 3,200 tweets you can get from a timeline and 200 per request (page). 
        //This makes 16 requests with 200 tweets per page (pages 1 to 17)
        //This also works if the users has less than 3,200 tweets
        for (int i = 1; i < 17; i++) {
            try {
                tweets.addAll(twitter.getUserTimeline(screenname, new Paging(i, 200)));
            } catch (TwitterException e) {
                log.error("Error retrieving timeline for user " + screenname, e);
            }
        }

        log.info("Retrieved timeline (" + tweets.size() + " Tweets) for user " + screenname);
        log.info("Mapping tweets for user " + screenname);
        for (Status tweet : tweets) {
            log.info("Mapping tweet " + tweet.getId());
            mapTweet(discourseName, datasetName, tweet, null);
        }
    }
}

From source file:edu.proyectofinal.integradorrs.services.tweets.impl.TweetsServiceImpl.java

@Override
public Collection<Status> getUserTimeline(String user, String email) {
    TwitterCredentials tc = TwitterCredentials.getInstance();
    ConfigurationBuilder cb = tc.GetCredentials(email);

    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();
    List<Status> statuses = null;
    Paging paging = new Paging(1, 30);
    try {/*from  ww  w .j  a  v  a  2 s .c  om*/
        statuses = twitter.getUserTimeline(user, paging);
    } catch (TwitterException ex) {
        Logger.getLogger(TweetsServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
    }
    Collection<Status> result = (Collection<Status>) statuses;
    return result;
}

From source file:edu.umich.cse.pyongjoo.twittercrawl.GetUserTimeline.java

License:Apache License

/**
 * Usage: java twitter4j.examples.timeline.GetUserTimeline
 *
 * @param args String[]/*from ww w  .j  av  a 2s.c o m*/
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
    OAuthTokenReader oauth = new OAuthTokenReader("oauth_tokens.csv");

    TwitterFactory tf = new TwitterFactory(oauth.getNextConfiguration());

    // gets Twitter instance with default credentials
    Twitter twitter = tf.getInstance();

    if (args.length < 2) {
        System.err.println("Usuage: command [username] [outputfile]");
        System.exit(-1);
    }
    String filename = args[1];

    FileWriter fstream = new FileWriter(filename, true);
    BufferedWriter out = new BufferedWriter(fstream);

    String user = "";
    if (args.length >= 1) {
        user = args[0];
    }
    //      out.write("#document starts with username: " + user + "\n");

    for (int i = 1; i <= 1; i++) {
        Paging pagingOption = new Paging(i, 200);

        try {
            List<Status> statuses;

            statuses = twitter.getUserTimeline(user, pagingOption);

            System.out.println("My Custom Showing @" + user + "'s user timeline.");

            for (Status status : statuses) {
                out.write(status.toString() + '\n');
                System.out.println(status.getUser().getScreenName() + "tweets written.");
            }
        } catch (TwitterException te) {
            te.printStackTrace();
            System.out.println("Failed to get timeline: " + te.getMessage());

            // close the file
            out.close();
            //              output.close();

            System.exit(-1);
        }
    }

    // close the file
    out.close();
    //      output.close();
}

From source file:es.upm.oeg.examples.watson.servlets.TwitterAnalysisServlet.java

License:Apache License

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 *///w  w w  .  j a  v a  2s  .  c  o m
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String twitterUsername = request.getParameter("twitter_name");
    // The factory instance is re-useable and thread safe.
    Twitter twitter = new TwitterFactory().getInstance();
    env = System.getenv();
    consumer_key = env.get("TWITTER_CONSUMER_KEY");
    consumer_secret = env.get("TWITTER_CONSUMER_SECRET");
    access_token = env.get("TWITTER_ACCESS_TOKEN");
    access_key = env.get("TWITTER_ACCESS_KEY");

    AccessToken accessToken = new AccessToken(access_token, access_key);

    try {
        twitter.setOAuthConsumer(consumer_key, consumer_secret);
        twitter.setOAuthAccessToken(accessToken);

        twitter4j.User a_name = twitter.showUser(twitterUsername);
        int followerCount = a_name.getFollowersCount();
        List<Status> retweets = twitter.getUserTimeline(twitterUsername, new Paging(1, 20)); // get the first twenty tweets
        int retweetCount = 0;
        List<String> langs = new ArrayList<>();
        List<String> translated = new ArrayList<>();

        StringBuilder aggregatedTextBuilder = new StringBuilder();
        String personalityInsights = null;

        for (Status tweet : retweets) {
            String tweetText = tweet.getText();
            try {
                String lang = languageIdentification.getLang(tweetText);
                langs.add(lang);
                String englishText;
                //TODO do the same for french and portuguese
                if (LanguageIdentificationService.ES_ES.equals(lang)) {
                    englishText = machineTranslation.translate(tweetText, MachineTranslationService.ES_TO_EN);
                } else if (LanguageIdentificationService.FR_FR.equals(lang)) {
                    englishText = machineTranslation.translate(tweetText, MachineTranslationService.FR_TO_EN);
                } else if (LanguageIdentificationService.PT_BR.equals(lang)) {
                    englishText = machineTranslation.translate(tweetText, MachineTranslationService.PT_TO_EN);
                } else {
                    englishText = tweetText;
                }
                translated.add(englishText);
                aggregatedTextBuilder.append(englishText);

                personalityInsights = personalityInsightsService.analyse(aggregatedTextBuilder.toString());

            } catch (Exception e) {
                // Log something and return an error message
                logger.log(Level.SEVERE, "got error: " + e.getMessage(), e);
                request.setAttribute("error", e.getMessage());
            }

        }

        request.setAttribute("t_name", twitterUsername);
        request.setAttribute("rtweets", retweets);
        request.setAttribute("langs", langs);
        request.setAttribute("translated", translated);
        request.setAttribute("personalityInsights", personalityInsights);
        request.setAttribute("aggregatedText", aggregatedTextBuilder.toString());

        request.getRequestDispatcher("/myTweets.jsp").forward(request, response);
    } catch (TwitterException e) {

        e.printStackTrace();
        if (e.getErrorCode() == 215 || e.getErrorCode() == 32) {
            response.sendRedirect("../index.html?message=errorcode215");
        } else if (e.getErrorCode() == -1 || e.getErrorCode() == 34) {
            response.sendRedirect("../index.html?message=errorcode-1");
        } else {
            response.sendRedirect("../index.html?message=errorcode99");
        }
        //throw new ServletException("Encountered a problem fetching data from Twitter - " + e.getErrorMessage());
    }
}

From source file:jp.wda.azeda.core.daemons.impl.RetweetsCrawlerImpl.java

License:Apache License

/**
 *
 * @param twitter//from  w ww  .  j  a va 2 s  .co m
 * @param retweeter
 * @param paging
 * @return
 * @throws TwitterException
 */
private ResponseList<Status> gtUserTimeline(Twitter twitter, long retweeter, Paging paging)
        throws TwitterException {
    if (gtUserTimelineTimer == 0) {
        gtUserTimelineTimer = System.currentTimeMillis();
    }
    long waiting = 15 * 60 * 1000L - (System.currentTimeMillis() - gtUserTimelineTimer);
    if (++gtUserTimelineCounter >= 180) {
        if (waiting > 0) {
            log.info("?????gtUserTimeline" + (waiting / 1000)
                    + "?????");
            try {
                sleep(waiting);
            } catch (InterruptedException exx) {
                log.error("", exx);
            }
        }
        gtUserTimelineTimer = System.currentTimeMillis();
        gtUserTimelineCounter = 0;
    }

    try {
        return twitter.getUserTimeline(retweeter, paging);
    } catch (TwitterException ex) {
        log.error("gtUserTimeline?twitter?????15?????", ex);
        try {
            sleep(15 * 60 * 1000L);
        } catch (InterruptedException exx) {
            log.error("", exx);
        }
        return twitter.getUserTimeline(retweeter, paging);
    }
}

From source file:org.getlantern.firetweet.loader.support.UserTimelineLoader.java

License:Open Source License

@NonNull
@Override/*from  w  w  w . ja  v a  2s  . co m*/
protected ResponseList<Status> getStatuses(@NonNull final Twitter twitter, final Paging paging)
        throws TwitterException {
    if (twitter == null)
        return null;
    if (mUserId != -1)
        return twitter.getUserTimeline(mUserId, paging);
    else if (mUserScreenName != null)
        return twitter.getUserTimeline(mUserScreenName, paging);
    else
        return null;
}

From source file:org.getlantern.firetweet.util.TwitterWrapper.java

License:Open Source License

@NonNull
public static User showUserAlternative(final Twitter twitter, final long id, final String screenName)
        throws TwitterException {
    final String searchScreenName;
    if (screenName != null) {
        searchScreenName = screenName;//from   w w  w . j  ava  2  s .c o  m
    } else if (id != -1) {
        searchScreenName = twitter.showFriendship(twitter.getId(), id).getTargetUserScreenName();
    } else
        throw new IllegalArgumentException();
    final Paging paging = new Paging();
    paging.count(1);
    if (id != -1) {
        final ResponseList<Status> timeline = twitter.getUserTimeline(id, paging);
        for (final Status status : timeline) {
            final User user = status.getUser();
            if (user.getId() == id)
                return user;
        }
    } else {
        final ResponseList<Status> timeline = twitter.getUserTimeline(screenName, paging);
        for (final Status status : timeline) {
            final User user = status.getUser();
            if (searchScreenName.equalsIgnoreCase(user.getScreenName()))
                return user;
        }
    }
    for (final User user : twitter.searchUsers(searchScreenName, 1)) {
        if (user.getId() == id || searchScreenName.equalsIgnoreCase(user.getScreenName()))
            return user;
    }
    throw new TwitterException("can't find user");
}