List of usage examples for twitter4j Paging setCount
public void setCount(int count)
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); 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);/*from ww w . jav a 2 s . c o m*/ } 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.vanita5.twittnuker.loader.support.Twitter4JActivitiesLoader.java
License:Open Source License
@Override public final List<ParcelableActivity> loadInBackground() { if (mAccountIds == null) return Collections.emptyList(); final File serializationFile = getSerializationFile(); if (mIsFirstLoad && mUseCache && serializationFile != null) { final List<ParcelableActivity> cached = getCachedData(serializationFile); if (cached != null) { Collections.sort(cached); return new CopyOnWriteArrayList<ParcelableActivity>(cached); }//from w ww . j av a 2s . c o m } final SharedPreferences prefs = mContext.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE); final int loadItemLimit = prefs.getInt(KEY_LOAD_ITEM_LIMIT, DEFAULT_LOAD_ITEM_LIMIT); final List<ParcelableActivity> result = new ArrayList<ParcelableActivity>(); for (final long accountId : mAccountIds) { final List<Activity> activities; try { final Paging paging = new Paging(); paging.setCount(Math.min(100, loadItemLimit)); activities = getActivities(getTwitter(accountId), paging); } catch (final TwitterException e) { e.printStackTrace(); final List<ParcelableActivity> cached = getCachedData(serializationFile); if (cached == null) return Collections.emptyList(); return new CopyOnWriteArrayList<ParcelableActivity>(cached); } if (activities == null) return new CopyOnWriteArrayList<ParcelableActivity>(mData); for (final Activity activity : activities) { result.add(new ParcelableActivity(activity, accountId)); } } Collections.sort(result); saveCachedData(serializationFile, result); return new CopyOnWriteArrayList<ParcelableActivity>(result); }
From source file:de.vanita5.twittnuker.loader.support.Twitter4JStatusesLoader.java
License:Open Source License
@SuppressWarnings("unchecked") @Override//from ww w . ja va2 s . co m public final List<ParcelableStatus> loadInBackground() { final File serializationFile = getSerializationFile(); final List<ParcelableStatus> data = getData(); if (isFirstLoad() && getTabPosition() >= 0 && serializationFile != null) { final List<ParcelableStatus> cached = getCachedData(serializationFile); if (cached != null) { data.addAll(cached); Collections.sort(data); return new CopyOnWriteArrayList<>(data); } } final List<Status> statuses; final boolean truncated; final Context context = getContext(); final SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE); final int loadItemLimit = prefs.getInt(KEY_LOAD_ITEM_LIMIT, DEFAULT_LOAD_ITEM_LIMIT); try { final Paging paging = new Paging(); paging.setCount(loadItemLimit); if (mMaxId > 0) { paging.setMaxId(mMaxId); } if (mSinceId > 0) { paging.setSinceId(mSinceId - 1); } statuses = new ArrayList<>(); truncated = truncateStatuses(getStatuses(getTwitter(), paging), statuses, mSinceId); } catch (final TwitterException e) { // mHandler.post(new ShowErrorRunnable(e)); e.printStackTrace(); return new CopyOnWriteArrayList<>(data); } final long minStatusId = statuses.isEmpty() ? -1 : Collections.min(statuses).getId(); final boolean insertGap = minStatusId > 0 && statuses.size() > 1 && !data.isEmpty() && !truncated; mHandler.post(CacheUsersStatusesTask.getRunnable(context, new StatusListResponse(mAccountId, statuses))); for (final Status status : statuses) { final long id = status.getId(); final boolean deleted = deleteStatus(data, id); data.add(new ParcelableStatus(status, mAccountId, minStatusId == id && insertGap && !deleted)); } Collections.sort(data); final ParcelableStatus[] array = data.toArray(new ParcelableStatus[data.size()]); for (int i = 0, size = array.length; i < size; i++) { final ParcelableStatus status = array[i]; if (shouldFilterStatus(mDatabase, status) && !status.is_gap && i != size - 1) { deleteStatus(data, status.id); } } saveCachedData(serializationFile, data); return new CopyOnWriteArrayList<>(data); }
From source file:it.greenvulcano.gvesb.social.twitter.directcall.TwitterOperationGetUserTimeline.java
License:Open Source License
@Override public void execute(SocialAdapterAccount account) throws SocialAdapterException { try {/*from w w w. java 2 s.co m*/ Twitter twitter = (Twitter) account.getProxyObject(); Paging paging = new Paging(); if ((sinceId != null) && !"".equals(sinceId)) { paging.setSinceId(Long.parseLong(sinceId)); } if ((maxId != null) && !"".equals(maxId)) { paging.setMaxId(Long.parseLong(maxId)); } if ((count != null) && !"".equals(count)) { paging.setCount(Integer.parseInt(count)); } if ((userId == null) || "".equals(userId)) { dataUser = twitter.getScreenName(); dataUserId = String.valueOf(twitter.getId()); statusList = twitter.getUserTimeline(paging); } else { User user = null; try { long id = Long.parseLong(userId); user = twitter.showUser(id); statusList = twitter.getUserTimeline(id, paging); } catch (NumberFormatException exc) { user = twitter.showUser(userId); statusList = twitter.getUserTimeline(userId, paging); } dataUser = user.getScreenName(); dataUserId = String.valueOf(user.getId()); } } catch (NumberFormatException exc) { logger.error("Call to TwitterOperationGetUserTimeline failed. Check userId[" + userId + "], sinceId[" + sinceId + "], maxId[" + maxId + "] and count[" + count + "] format.", exc); throw new SocialAdapterException("Call to TwitterOperationGetUserTimeline failed. Check followingId[" + userId + "], sinceId[" + sinceId + "], maxId[" + maxId + "] and count[" + count + "] format.", exc); } catch (TwitterException exc) { logger.error("Call to TwitterOperationGetUserTimeline followingId[" + userId + "], sinceId[" + sinceId + "], maxId[" + maxId + "] and count[" + count + "] failed.", exc); throw new SocialAdapterException("Call to TwitterOperationGetUserTimeline followingId[" + userId + "], sinceId[" + sinceId + "], maxId[" + maxId + "] and count[" + count + "] failed.", exc); } }
From source file:jp.wda.azeda.core.daemons.impl.RetweetsCrawlerImpl.java
License:Apache License
/** * * @param twitter/* w ww . j a v a 2 s . c o m*/ * @param retweetTo * @param retweeter * @param page * @return * @throws TwitterException */ private List<Status> loadRetweetersTweets(Twitter twitter, long retweetTo, long retweeter, int page) throws TwitterException { if (!alive) { return null; } if (page > timelinePages) { log.info(retweetTo + " retweet by " + retweeter + " ? " + (timelinePages * 100) + "????????????"); insertRetweeter(twitter.getId(), retweetTo, retweeter); return null; } Paging paging = new Paging(retweetTo); paging.setPage(page); paging.setCount(100); ResponseList<Status> userTimeline = gtUserTimeline(twitter, retweeter, paging); if (userTimeline.size() == 0) { return null; } List<Status> result = new ArrayList<Status>(); for (Status s : userTimeline) { result.add(s); if (s.isRetweet()) { if (s.getRetweetedStatus().getId() != retweetTo) { continue; } log.debug("found!!"); return result; } } return loadRetweetersTweets(twitter, retweetTo, retweeter, page + 1); }
From source file:org.apache.nutch.protocol.http.api.HttpBase.java
License:Apache License
private HttpResponse getTwitterResponse(String url) throws MalformedURLException, TwitterException, UnsupportedEncodingException, CharacterCodingException { url = URLDecoder.decode(url, "UTF-8"); Map<String, String> map = getQueryParams(url); String consumerKey = map.get("consumer_key"); String consumerSecret = map.get("consumer_secret"); String accessToken = map.get("oauth_key"); String accessTokenSecret = map.get("oauth_secret"); Twitter twitter = getTwitterInstance(consumerKey, consumerSecret, accessToken, accessTokenSecret); Paging paging = new Paging(); paging.setCount(10); twitter4j.internal.http.HttpResponse twitterResponse; String result = null;// w w w.j a v a2 s .c o m if (!(url.indexOf("statuses/show.json") >= 0)) { String max_id = map.get("max_id"); if (null != max_id) paging.setMaxId(Long.parseLong(max_id)); ResponseList<Status> homeTimeline = twitter.getUserTimeline(paging); twitterResponse = homeTimeline.getResponse(); result = twitterResponse.asJSONArray().toString(); } else { String id = map.get("id"); Long statusId = Long.parseLong(id); Status status = twitter.showStatus(statusId); twitterResponse = status.getResponse(); result = twitterResponse.asJSONObject().toString(); } u = new URL(twitterResponse.getRequestURL()); code = twitterResponse.getStatusCode(); ByteBuffer buffer = toByteBUffer(result); contents = buffer.array(); contentType = "text/javascript"; headers = new SpellCheckedMetadata(); Map<String, List<String>> responseHeaders = twitterResponse.getResponseHeaderFields(); for (String key : responseHeaders.keySet()) { List<String> values = responseHeaders.get(key); for (String value : values) { if (null == key) continue; if (null == value) continue; headers.add(key, value); } } return null; }
From source file:org.getlantern.firetweet.loader.support.Twitter4JActivitiesLoader.java
License:Open Source License
@SuppressWarnings("unchecked") @Override//w w w. j a va 2s. c o m public final List<ParcelableActivity> loadInBackground() { final File serializationFile = getSerializationFile(); final List<ParcelableActivity> data = getData(); if (isFirstLoad() && getTabPosition() >= 0 && serializationFile != null) { final List<ParcelableActivity> cached = getCachedData(serializationFile); if (cached != null) { data.addAll(cached); if (mComparator != null) { Collections.sort(data, mComparator); } else { Collections.sort(data); } return new CopyOnWriteArrayList<>(data); } } final List<Activity> activities; final boolean truncated; final Context context = getContext(); final SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE); final int loadItemLimit = prefs.getInt(KEY_LOAD_ITEM_LIMIT, DEFAULT_LOAD_ITEM_LIMIT); try { final Paging paging = new Paging(); paging.setCount(loadItemLimit); if (mMaxId > 0) { paging.setMaxId(mMaxId); } if (mSinceId > 0) { paging.setSinceId(mSinceId); } activities = new ArrayList<>(); truncated = truncateActivities(getActivities(getTwitter(), paging), activities, mSinceId); } catch (final TwitterException e) { // mHandler.post(new ShowErrorRunnable(e)); e.printStackTrace(); return new CopyOnWriteArrayList<>(data); } final Pair<Long, Long> minId; if (activities.isEmpty()) { minId = new Pair<>(-1L, -1L); } else { final Activity minActivity = Collections.min(activities); minId = new Pair<>(minActivity.getMinPosition(), minActivity.getMaxPosition()); } final boolean insertGap = minId.first > 0 && minId.second > 0 && activities.size() > 1 && !data.isEmpty() && !truncated; // mHandler.post(CacheUsersStatusesTask.getRunnable(context, new StatusListResponse(mAccountIds, activities))); for (final Activity activity : activities) { final long min = activity.getMinPosition(), max = activity.getMaxPosition(); final boolean deleted = deleteStatus(data, max); final boolean isGap = minId.first == min && minId.second == max && insertGap && !deleted; data.add(new ParcelableActivity(activity, mAccountIds, isGap)); } // final ParcelableActivity[] array = data.toArray(new ParcelableActivity[data.size()]); // for (int i = 0, size = array.length; i < size; i++) { // final ParcelableActivity status = array[i]; // if (shouldFilterActivity(mDatabase, status) && !status.is_gap && i != size - 1) { // deleteStatus(data, status.max_position); // } // } if (mComparator != null) { Collections.sort(data, mComparator); } else { Collections.sort(data); } saveCachedData(serializationFile, data); return new CopyOnWriteArrayList<>(data); }
From source file:org.getlantern.firetweet.loader.support.TwitterAPIStatusesLoader.java
License:Open Source License
@SuppressWarnings("unchecked") @Override/*from ww w . ja v a 2 s . c o m*/ public final List<ParcelableStatus> loadInBackground() { final File serializationFile = getSerializationFile(); final List<ParcelableStatus> data = getData(); if (isFirstLoad() && getTabPosition() >= 0 && serializationFile != null) { final List<ParcelableStatus> cached = getCachedData(serializationFile); if (cached != null) { data.addAll(cached); if (mComparator != null) { Collections.sort(data, mComparator); } else { Collections.sort(data); } return new CopyOnWriteArrayList<>(data); } } if (!isFromUser()) return data; final List<Status> statuses; final boolean truncated; final Context context = getContext(); final SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE); final int loadItemLimit = prefs.getInt(KEY_LOAD_ITEM_LIMIT, DEFAULT_LOAD_ITEM_LIMIT); try { final Paging paging = new Paging(); paging.setCount(loadItemLimit); if (mMaxId > 0) { paging.setMaxId(mMaxId); } if (mSinceId > 0) { paging.setSinceId(mSinceId - 1); } statuses = new ArrayList<>(); final Twitter twitter = getTwitter(); if (twitter == null) { throw new TwitterException("Account is null"); } truncated = truncateStatuses(getStatuses(twitter, paging), statuses, mSinceId); } catch (final TwitterException e) { // mHandler.post(new ShowErrorRunnable(e)); Log.w(LOGTAG, e); return new CopyOnWriteArrayList<>(data); } final long minStatusId = statuses.isEmpty() ? -1 : Collections.min(statuses).getId(); final boolean insertGap = minStatusId > 0 && statuses.size() > 1 && !data.isEmpty() && !truncated; for (final Status status : statuses) { final long id = status.getId(); final boolean deleted = deleteStatus(data, id); data.add(new ParcelableStatus(status, mAccountId, minStatusId == id && insertGap && !deleted)); } final ParcelableStatus[] array = data.toArray(new ParcelableStatus[data.size()]); for (int i = 0, size = array.length; i < size; i++) { final ParcelableStatus status = array[i]; if (shouldFilterStatus(mDatabase, status) && !status.is_gap && i != size - 1) { deleteStatus(data, status.id); } } if (mComparator != null) { Collections.sort(data, mComparator); } else { Collections.sort(data); } saveCachedData(serializationFile, data); return new CopyOnWriteArrayList<>(data); }
From source file:org.junjun.twitter.GetUserTimeline.java
License:Apache License
/** * Usage: java twitter4j.examples.timeline.GetUserTimeline * * @param args String[]// w w w . j a v a 2 s. c om */ public static void main(String[] args) { // gets Twitter instance with default credentials Twitter twitter = new TwitterFactory().getInstance(); try { List<Status> statuses; String user; Paging paging = new Paging(1); paging.setCount(100); if (args.length == 1) { user = args[0]; /* statuses = twitter.getUserTimeline(Long.parseLong(user),paging);*/ statuses = twitter.getUserTimeline(Long.parseLong(user)); } else { user = twitter.verifyCredentials().getScreenName(); statuses = twitter.getUserTimeline(); } System.out.println("Showing @" + user + "'s user timeline."); int i = 0; for (Status status : statuses) { System.out.println(i++ + "@" + status.getUser().getName() + " - " + status + " "); } System.out.println(); } catch (TwitterException te) { te.printStackTrace(); System.out.println("Failed to get timeline: " + te.getMessage()); System.exit(-1); } }
From source file:org.mariotaku.twidere.loader.support.Twitter4JStatusesLoader.java
License:Open Source License
@SuppressWarnings("unchecked") @Override// w ww. ja va2s . c o m public final List<ParcelableStatus> loadInBackground() { final File serializationFile = getSerializationFile(); final List<ParcelableStatus> data = getData(); if (isFirstLoad() && getTabPosition() >= 0 && serializationFile != null) { final List<ParcelableStatus> cached = getCachedData(serializationFile); if (cached != null) { data.addAll(cached); if (mComparator != null) { Collections.sort(data, mComparator); } else { Collections.sort(data); } return new CopyOnWriteArrayList<>(data); } } if (!isFromUser()) return data; final List<Status> statuses; final boolean truncated; final Context context = getContext(); final SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE); final int loadItemLimit = prefs.getInt(KEY_LOAD_ITEM_LIMIT, DEFAULT_LOAD_ITEM_LIMIT); try { final Paging paging = new Paging(); paging.setCount(loadItemLimit); if (mMaxId > 0) { paging.setMaxId(mMaxId); } if (mSinceId > 0) { paging.setSinceId(mSinceId - 1); } statuses = new ArrayList<>(); final Twitter twitter = getTwitter(); if (twitter == null) { throw new TwitterException("Account is null"); } truncated = truncateStatuses(getStatuses(twitter, paging), statuses, mSinceId); } catch (final TwitterException e) { // mHandler.post(new ShowErrorRunnable(e)); Log.w(LOGTAG, e); return new CopyOnWriteArrayList<>(data); } final long minStatusId = statuses.isEmpty() ? -1 : Collections.min(statuses).getId(); final boolean insertGap = minStatusId > 0 && statuses.size() > 1 && !data.isEmpty() && !truncated; mHandler.post(CacheUsersStatusesTask.getRunnable(context, new StatusListResponse(mAccountId, statuses))); for (final Status status : statuses) { final long id = status.getId(); final boolean deleted = deleteStatus(data, id); data.add(new ParcelableStatus(status, mAccountId, minStatusId == id && insertGap && !deleted)); } final ParcelableStatus[] array = data.toArray(new ParcelableStatus[data.size()]); for (int i = 0, size = array.length; i < size; i++) { final ParcelableStatus status = array[i]; if (shouldFilterStatus(mDatabase, status) && !status.is_gap && i != size - 1) { deleteStatus(data, status.id); } } if (mComparator != null) { Collections.sort(data, mComparator); } else { Collections.sort(data); } saveCachedData(serializationFile, data); return new CopyOnWriteArrayList<>(data); }