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:br.ufba.dcc.tagcloud.util.TwitterProvider.java

public void getTweets(Twitter twitter, String user, int max) throws TwitterException {
    List<Status> tweets = new ArrayList<Status>();
    for (int i = 1; i < 20; i++) {
        tweets.addAll(twitter.getUserTimeline(user, new Paging(i, max)));
    }//from   w  w w.j av a  2 s.  c om
    for (Status tweet : tweets) {
        this.texts.add(tweet.getText());
    }
}

From source file:com.appspot.socialinquirer.server.service.impl.UserServiceImpl.java

License:Apache License

@Override
public void aggregateQuotes() {
    EntityManager entityManager = createEntityManager();
    try {/*from www.j a v  a  2  s.  com*/
        DataAccessObject dao = getDao(entityManager);
        Version version = getCurrentVersion();
        Twitter twitter = getTwitterClient();
        List<Status> homeTimeline = twitter.getUserTimeline(ApplicationConstants.TWITTER_SCREEN_NAME,
                new Paging(version.getLastHomeTweetId()));
        long lastTweetId = 0L;
        for (Status status : homeTimeline) {
            if (status.getId() > lastTweetId) {
                lastTweetId = status.getId();
            }
            if (!status.isRetweet() && status.getInReplyToStatusId() == -1) {
                Quote quote = new Quote();
                quote.setPublishedDate(status.getCreatedAt());
                quote.setText(new Text(status.getText()));
                quote.setTweetId(status.getId());
                dao.saveOrUpdate(quote);
            }
        }
        if (lastTweetId > version.getLastHomeTweetId()) {
            version.setLastHomeTweetId(lastTweetId);
            dao.saveOrUpdate(version);
        }
    } catch (Exception e) {
        throw new ServiceException(e);
    } finally {
        closeEntityManager(entityManager);
    }
}

From source file:com.daiv.android.twitter.ui.profile_viewer.fragments.ProfileFragment.java

License:Apache License

public void getTimeline(final User user, final AsyncListView listView) {
    spinner.setVisibility(View.VISIBLE);
    canRefresh = false;/* w  w  w. jav a  2  s. co m*/

    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Twitter twitter = Utils.getTwitter(context, settings);

                List<twitter4j.Status> statuses = twitter.getUserTimeline(user.getId(), timelinePaging);
                timelinePaging.setPage(timelinePaging.getPage() + 1);

                for (twitter4j.Status s : statuses) {
                    timelineStatuses.add(s);
                }

                if (statuses.size() > 17) {
                    hasMore = true;
                } else {
                    hasMore = false;
                }

                ((Activity) context).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (timelineAdapter != null) {
                            timelineAdapter.notifyDataSetChanged();
                        } else {
                            timelineAdapter = new TimelineArrayAdapter(context, timelineStatuses, screenName);
                            listView.setItemManager(builder.build());
                            listView.setAdapter(timelineAdapter);
                        }

                        if (settings.roundContactImages) {
                            ImageUtils.loadSizedCircleImage(context, profilePicture,
                                    thisUser.getOriginalProfileImageURL(), mCache, 96);
                        } else {
                            ImageUtils.loadImage(context, profilePicture, thisUser.getOriginalProfileImageURL(),
                                    mCache);
                        }

                        String url = user.getProfileBannerURL();
                        ImageUtils.loadImage(context, background, url, mCache);

                        spinner.setVisibility(View.GONE);
                        canRefresh = true;
                    }
                });

            } catch (Exception e) {
                e.printStackTrace();
                ((Activity) context).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            if (user != null && user.isProtected()) {
                                Toast.makeText(context, getResources().getString(R.string.protected_account),
                                        Toast.LENGTH_SHORT).show();
                                if (settings.roundContactImages) {
                                    ImageUtils.loadSizedCircleImage(context, profilePicture,
                                            thisUser.getOriginalProfileImageURL(), mCache, 96);
                                } else {
                                    ImageUtils.loadImage(context, profilePicture,
                                            user.getOriginalProfileImageURL(), mCache);
                                }

                                String url = user.getProfileBannerURL();
                                ImageUtils.loadImage(context, background, url, mCache);
                            } else {
                                Toast.makeText(context,
                                        getResources().getString(R.string.error_loading_timeline),
                                        Toast.LENGTH_SHORT).show();
                            }
                            spinner.setVisibility(View.GONE);
                        } catch (Exception e) {
                            // not attached
                        }
                        canRefresh = false;
                        hasMore = false;
                    }
                });
            } catch (OutOfMemoryError x) {
                x.printStackTrace();
                ((Activity) context).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Toast.makeText(context,
                                    context.getResources().getString(R.string.error_loading_timeline),
                                    Toast.LENGTH_SHORT).show();
                            spinner.setVisibility(View.GONE);
                        } catch (Exception e) {
                            // not attached
                        }

                        canRefresh = false;
                        hasMore = false;
                    }
                });
            }
        }
    }).start();
}

From source file:com.daiv.android.twitter.ui.profile_viewer.fragments.sub_fragments.ProfilePicturesFragment.java

License:Apache License

public void doSearch() {
    spinner.setVisibility(View.VISIBLE);

    new Thread(new Runnable() {
        @Override/*from   w w w. ja  va 2 s  .c  o  m*/
        public void run() {
            try {
                Twitter twitter = Utils.getTwitter(context, settings);

                ResponseList<Status> result;
                try {
                    result = twitter.getUserTimeline(screenName, paging);
                } catch (OutOfMemoryError e) {
                    return;
                }

                tweets.clear();

                for (twitter4j.Status status : result) {
                    tweets.add(status);
                }

                hasMore = result.size() > 17;

                for (Status s : tweets) {
                    String[] links = TweetLinkUtils.getLinksInStatus(s);
                    if (!links[1].equals("")) {
                        pics.add(links[1]);
                        tweetsWithPics.add(s);
                    }
                }

                ((Activity) context).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        Display display = ((Activity) context).getWindowManager().getDefaultDisplay();
                        Point size = new Point();
                        display.getSize(size);
                        int width = size.x;

                        int numColumns;

                        int currentOrientation = getResources().getConfiguration().orientation;
                        if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
                            numColumns = 5;
                        } else {
                            numColumns = 3;
                        }

                        adapter = new PicturesGridAdapter(context, pics, tweetsWithPics, width / numColumns);
                        listView.setAdapter(adapter);
                        listView.setVisibility(View.VISIBLE);

                        spinner.setVisibility(View.GONE);
                        canRefresh = true;

                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
                ((Activity) context).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        spinner.setVisibility(View.GONE);
                        canRefresh = false;
                    }
                });

            }

        }
    }).start();
}

From source file:com.daiv.android.twitter.ui.profile_viewer.fragments.sub_fragments.ProfilePicturesFragment.java

License:Apache License

public void getMore() {
    canRefresh = false;//www  .  ja  va 2 s.com
    spinner.setVisibility(View.VISIBLE);

    if (destroyed) {
        hasMore = false;
        return;
    }

    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Twitter twitter = Utils.getTwitter(context, settings);

                paging.setPage(paging.getPage() + 1);

                ResponseList<Status> result = twitter.getUserTimeline(screenName, paging);

                tweets.clear();

                for (Status status : result) {
                    tweets.add(status);
                }

                hasMore = result.size() > 17;

                boolean update = false;

                for (Status s : tweets) {
                    String[] links = TweetLinkUtils.getLinksInStatus(s);
                    if (!links[1].equals("")) {
                        pics.add(links[1]);
                        tweetsWithPics.add(s);
                        update = true;
                    }
                }

                if (update) {
                    ((Activity) context).runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            adapter.notifyDataSetChanged();
                            canRefresh = true;

                            spinner.setVisibility(View.GONE);
                        }
                    });
                } else {
                    canRefresh = true;
                }

                try {
                    Thread.sleep(250);
                } catch (Exception e) {

                }
            } catch (Exception e) {
                e.printStackTrace();
                ((Activity) context).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        canRefresh = false;
                        hasMore = false;

                        try {
                            adapter.notifyDataSetChanged();
                        } catch (Exception e) {

                        }

                        spinner.setVisibility(View.GONE);
                    }
                });

            }

        }
    }).start();
}

From source file:com.eventattend.portal.bl.TwitterBL.java

License:Open Source License

public List tweets(Twitter twitter, String screenName, TwitterDTO twitterDTO) throws BaseAppException {
    List tweetList = new ArrayList();

    List<Status> statuses = null;

    Paging paging = new Paging(1, 10);

    try {/*from w ww  .j av a 2s .c  o  m*/
        statuses = twitter.getUserTimeline(screenName, paging);
    } catch (TwitterException e) {
        processTwitterException(e);
    }
    int i = 1;
    if (!statuses.isEmpty()) {

        for (Status status : statuses) {
            if (i <= 10) {
                User user = status.getUser();
                twitterDTO = new TwitterDTO();
                //   
                //   if(userId==status.getUser().getId()){
                if (status.getId() != 0) {
                    System.out.println(i + "TweetId=> " + status.getId());
                    twitterDTO.setTweetId(String.valueOf(status.getId()));
                }
                if (user.getProfileImageURL() != null) {
                    twitterDTO.setUserImg(user.getProfileImageURL().toString());
                }
                if (user.getScreenName() != null) {
                    twitterDTO.setUserScreeName("http://twitter.com/" + user.getScreenName());
                }
                if (user.getName() != null) {
                    twitterDTO.setUserName(user.getName());
                }
                if (status.getText() != null) {
                    twitterDTO.setTweet(status.getText());
                }
                System.out.println(status.getId() + " : " + status.getCreatedAt() + " >> " + status.getText());
                tweetList.add(twitterDTO);
                i++;
            } else {
                break;
            }

        }

    }

    return tweetList;
}

From source file:com.klinker.android.twitter.activities.profile_viewer.fragments.sub_fragments.ProfilePicturesFragment.java

License:Apache License

public void doSearch() {
    spinner.setVisibility(View.VISIBLE);

    new Thread(new Runnable() {
        @Override//from  w w  w .j ava  2 s .c om
        public void run() {
            try {
                Twitter twitter = Utils.getTwitter(context, settings);

                ResponseList<Status> result;
                try {
                    result = twitter.getUserTimeline(screenName, paging);
                } catch (OutOfMemoryError e) {
                    return;
                }

                tweets.clear();

                for (twitter4j.Status status : result) {
                    tweets.add(status);
                }

                if (result.size() > 17) {
                    hasMore = true;
                } else {
                    hasMore = false;
                }

                for (Status s : tweets) {
                    String[] links = TweetLinkUtils.getLinksInStatus(s);
                    if (!links[1].equals("")) {
                        pics.add(links[1]);
                        tweetsWithPics.add(s);
                    }
                }

                ((Activity) context).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        Display display = ((Activity) context).getWindowManager().getDefaultDisplay();
                        Point size = new Point();
                        display.getSize(size);
                        int width = size.x;

                        int numColumns;

                        int currentOrientation = getResources().getConfiguration().orientation;
                        if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
                            numColumns = 5;
                        } else {
                            numColumns = 3;
                        }

                        adapter = new PicturesGridAdapter(context, pics, tweetsWithPics, width / numColumns);
                        listView.setAdapter(adapter);
                        listView.setVisibility(View.VISIBLE);

                        spinner.setVisibility(View.GONE);
                        canRefresh = true;

                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
                ((Activity) context).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        spinner.setVisibility(View.GONE);
                        canRefresh = false;
                    }
                });

            }

        }
    }).start();
}

From source file:com.klinker.android.twitter.activities.profile_viewer.fragments.sub_fragments.ProfilePicturesFragment.java

License:Apache License

public void getMore() {
    canRefresh = false;/*from   w w  w .jav  a 2 s. c  om*/
    spinner.setVisibility(View.VISIBLE);

    if (destroyed) {
        hasMore = false;
        return;
    }

    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Twitter twitter = Utils.getTwitter(context, settings);

                paging.setPage(paging.getPage() + 1);

                ResponseList<Status> result = twitter.getUserTimeline(screenName, paging);

                tweets.clear();

                for (Status status : result) {
                    tweets.add(status);
                }

                if (result.size() > 17) {
                    hasMore = true;
                } else {
                    hasMore = false;
                }

                boolean update = false;

                for (Status s : tweets) {
                    String[] links = TweetLinkUtils.getLinksInStatus(s);
                    if (!links[1].equals("")) {
                        pics.add(links[1]);
                        tweetsWithPics.add(s);
                        update = true;
                    }
                }

                if (update) {
                    ((Activity) context).runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            adapter.notifyDataSetChanged();
                            canRefresh = true;

                            spinner.setVisibility(View.GONE);
                        }
                    });
                } else {
                    canRefresh = true;
                }

                try {
                    Thread.sleep(250);
                } catch (Exception e) {

                }
            } catch (Exception e) {
                e.printStackTrace();
                ((Activity) context).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        canRefresh = false;
                        hasMore = false;

                        try {
                            adapter.notifyDataSetChanged();
                        } catch (Exception e) {

                        }

                        spinner.setVisibility(View.GONE);
                    }
                });

            }

        }
    }).start();
}

From source file:com.klinker.android.twitter.ui.profile_viewer.fragments.sub_fragments.ProfilePicturesFragment.java

License:Apache License

public void doSearch() {
    spinner.setVisibility(View.VISIBLE);

    new Thread(new Runnable() {
        @Override/*from   ww  w  .ja v a 2s.  co  m*/
        public void run() {
            try {
                Twitter twitter = Utils.getTwitter(context, settings);

                ResponseList<Status> result;
                try {
                    result = twitter.getUserTimeline(screenName, paging);
                } catch (OutOfMemoryError e) {
                    return;
                }

                tweets.clear();

                for (twitter4j.Status status : result) {
                    tweets.add(status);
                }

                if (result.size() > 17) {
                    hasMore = true;
                } else {
                    hasMore = false;
                }

                for (Status s : tweets) {
                    String[] links = TweetLinkUtils.getLinksInStatus(s);
                    if (!links[1].equals("")) {
                        pics.add(links[1]);
                        tweetsWithPics.add(s);
                    }
                }

                ((Activity) context).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter = new PicturesArrayAdapter(context, pics, tweetsWithPics);
                        listView.setAdapter(adapter);
                        listView.setVisibility(View.VISIBLE);

                        spinner.setVisibility(View.GONE);
                        canRefresh = true;

                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
                ((Activity) context).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        spinner.setVisibility(View.GONE);
                        canRefresh = false;
                    }
                });

            }

        }
    }).start();
}

From source file:com.temenos.interaction.example.mashup.twitter.Twitter4JConsumer.java

License:Open Source License

/**
 * @param otherUser//from  ww w . j a v a2  s  .  c o m
 * @return
 */
public Collection<Tweet> requestTweetsByUser(String otherUser) {
    List<Tweet> tweets = new ArrayList<Tweet>();
    try {
        // The factory instance is re-useable and thread safe.
        Twitter twitter = new TwitterFactory().getInstance();
        AccessToken accessToken = loadAccessToken(1);
        twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
        twitter.setOAuthAccessToken(accessToken);
        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("Fetching latest 100 tweets for [" + otherUser + "]");
        }
        // First param of Paging() is the page number, second is the number per page (this is capped around 200 I think.
        Paging paging = new Paging(1, 100);
        List<Status> statuses = twitter.getUserTimeline(otherUser, paging);
        for (Status status : statuses) {
            tweets.add(new Tweet(otherUser, status.getText(),
                    (status.getGeoLocation() != null
                            ? status.getGeoLocation().getLatitude() + ","
                                    + status.getGeoLocation().getLongitude()
                            : "")));
            if (LOGGER.isInfoEnabled()) {
                LOGGER.info(
                        status.getUser().getName() + "(" + status.getGeoLocation() + "):" + status.getText());
            }
        }
    } catch (Exception e) {
        LOGGER.error("Error on requestTweetsByUser", e);
        throw new TwitterMashupException(e);
    }
    return tweets;
}