Created
February 12, 2011 19:09
-
-
Save karussell/824008 to your computer and use it in GitHub Desktop.
search twitter via maxId
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// we don't want results with an id larger than maxId | |
long maxId = lastId; | |
// we don't want results with an id smaller than sinceId | |
long sinceId = lastId; | |
int hitsPerPage = 100; | |
int maxPages = 1; | |
boolean breakPaging = false; | |
for (int page = 0; page < maxPages; page++) { | |
Query query = new Query(term); | |
// RECENT or POPULAR | |
query.setResultType(Query.MIXED); | |
// avoid that more recent results disturb our paging! | |
if (page > 0) | |
query.setMaxId(maxId); | |
query.setPage(page + 1); | |
query.setRpp(hitsPerPage); | |
QueryResult res = twitter.search(query); | |
// res.getTweets() is NOT sorted against id or something | |
for (Tweet twe : res.getTweets()) { | |
// determine maxId in the first page | |
if (page == 0 && maxId < twe.getId()) | |
maxId = twe.getId(); | |
if (twe.getId() < sinceId) | |
breakPaging = true; | |
else { | |
String userName = twe.getFromUser().toLowerCase(); | |
SolrUser user = userMap.get(userName); | |
if (user == null) { | |
user = new SolrUser(userName).init(twe); | |
userMap.put(userName, user); | |
} | |
result.add(new SolrTweet(twe, user)); | |
} | |
} | |
// sinceId could force us to leave earlier than defined by maxPages | |
if (breakPaging || res.getTweets().size() < hitsPerPage) | |
break; | |
} | |
return maxId; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment