views:

232

answers:

2

The Twitter API has the friends_count and followers_count available as cached values for the users/show or account/verify_credentials method. As far as I can tell, the only way to determine the number of lists a user is a member of is to make a call to GET list memberships and paginate through using the cursor to count the total number of lists a person is a member of. This is sub-optimal; ideally lists_count would be available on users/show.

Is there an easier way to determine the raw number of lists a user is a member of using the Twitter API? What did I miss?

+2  A: 

Forget the API :), its almost as easy to hit the HTML site directly. Just grab the xhtml, load it into your favourite Xml parser and use an xpath query to pull out the data you want.

var client = new HttpClient();
client.DefaultHeaders.Authorization = Credential.CreateBasic("username", "password");

var response = client.Get("http://www.twitter.com/{username}/lists/memberships");
var doc = new XmlDocument();
doc.Load(response.Content.ReadAsXmlReader( new XmlReaderSettings() {ProhibitDtd = false}));

var nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("xh", "http://www.w3.org/1999/xhtml");
var xpathToCount = "//xh:li[@id='on_lists_tab']/xh:a[1]/xh:span";
var count = doc.SelectSingleNode(xpathToCount,nsmgr).InnerText;
Darrel Miller
Brittle. Relying on the username is brittle. Having dealt with several other twitter apps, people do change their usernames. Accessing data via user_ids works better. Relying on the twitter page layout is brittle; the xpath query is brittle. It'd be safer to use `//*[@id="lists_count"]`. Lastly, authentication isn't necessary for this, but we use OAuth (not user/password) anyway.
Ryan McGeary
...but, unfortunately, this might be the only direct way for now. Still deserving of an upvote.
Ryan McGeary
From what I understand, relying on format of the twitter API responses is brittle in the first place :-). I didn't see the element with the "lists_count" id. Definitely sounds like a better choice.
Darrel Miller
A: 

Update: Twitter added listed_count to the user payload.


It looks like adding lists_count to the user payload is on the todo list.

We've got this on our todo list. It requires fairly extensive asynchronous fragment invalidation so it's not as simple as just adding the count into the payload. We've got it on the list though.

Meanwhile, Darrel's suggestion is the only direct approach, but succinctness of a language and its libraries do matter:

require 'rubygems'
require 'nokogiri'
require 'open-uri'

username = "twitterapi"
page = Nokogiri::HTML(open("http://twitter.com/#{username}"))
page.at_css("#lists_count").text.gsub(/\D/, "").to_i          # => 1299
Ryan McGeary