/** * Use cursors to fetch upto jtwit.maxResults TODO More controlled paging?? * * @param url API method to call * @param screenName * @param userId * @return twitter-id numbers for friends/followers of screenName or userId Is affected by {@link * #maxResults} */ private List<Number> getUserIDs(String url, String screenName, Long userId) { Long cursor = -1L; List<Number> ids = new ArrayList<Number>(); if (screenName != null && userId != null) throw new IllegalArgumentException( "cannot use both screen_name and user_id when fetching user_ids"); Map<String, String> vars = InternalUtils.asMap("screen_name", screenName, "user_id", userId); while (cursor != 0 && !jtwit.enoughResults(ids)) { vars.put("cursor", String.valueOf(cursor)); String json = http.getPage(url, vars, http.canAuthenticate()); try { // it seems Twitter will occasionally return a raw array JSONArray jarr; if (json.charAt(0) == '[') { jarr = new JSONArray(json); cursor = 0L; } else { JSONObject jobj = new JSONObject(json); jarr = (JSONArray) jobj.get("ids"); cursor = new Long(jobj.getString("next_cursor")); } for (int i = 0; i < jarr.length(); i++) { ids.add(jarr.getLong(i)); } if (jarr.length() == 0) { // No more break; } } catch (JSONException e) { throw new TwitterException.Parsing(json, e); } } return ids; }
/** * Low-level method for fetching e.g. your friends * * @param url * @param screenName e.g. your screen-name * @return */ private List<User> getUsers(String url, String screenName) { Map<String, String> vars = InternalUtils.asMap("screen_name", screenName); List<User> users = new ArrayList<User>(); Long cursor = -1L; while (cursor != 0 && !jtwit.enoughResults(users)) { vars.put("cursor", cursor.toString()); JSONObject jobj; try { jobj = new JSONObject(http.getPage(url, vars, http.canAuthenticate())); users.addAll(User.getUsers(jobj.getString("users"))); cursor = new Long(jobj.getString("next_cursor")); } catch (JSONException e) { throw new TwitterException.Parsing(null, e); } } return users; }