开发者

Twitter List Membership Count Per User

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?


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;


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 was 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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜