This blog is subject the DISCLAIMER below.

Monday, December 07, 2009

How to Get User Friends\Followers on Twitter

Twitter became very popular social network, all use it for different purposes i.e to expand your network, to market for your products, to be up-to-date with specific technology, or to know what people talking about is.
Developers are users but on Twitter and Facebook they are not regular users, Twitter for example gives you some APIs to develop you own application and extend Twitter with features, it's time for everybody to extend without waiting for creators to do.
I was developing Twitter application two days ago, and I faced a problem to get all user's friends\followers, I used some APIs but unfortunately, they got first 100 friends\followers!! I knew that you can browse user friends\followers for user called "RamyMahrous" like that http://twitter.com/statuses/friends/ramymahrous.xml?
http://twitter.com/statuses/followers/ramymahrous.xml?
But it gets just first 100 friends\followers I browsed some sites but no information, unless I went to Twitter API Wiki and I knew about XML cursor they use every cursor has limited 100 friends\followes means... if "RamyMahrous" has 204 friends so it will be iterated through 3 cursors.
Let's code...
string FriendsXMLFileURL = "http://twitter.com/statuses/friends/{0}.xml?cursor={1}";
string FollowersXMLFileURL = "http://twitter.com/statuses/followers/{0}.xml?cursor={1}";
List < users_list > listUserList = new List < users_list > ();
int index=0;
XmlSerializer Serializer = new XmlSerializer(typeof(users_list));
List < users_list > listUserList = new List < users_list > ();
List < users_listusersuser > users = new List < users_listusersuser > ();
users_list usersList = new users_list();
int index = 0;
string nextCursor = "-1";
while (nextCursor != "0")
{
usersList = (users_list)Serializer.Deserialize(
XmlReader.Create(string.Format(FollowersXMLFileURL, "RamyMahrous", nextCursor)));
listUserList.Add(usersList);
nextCursor = usersList.next_cursor;
}
//user_list class generated based on the xml structure you can save the xml file and generate it
//using XSD please take alook on this post by Shady
http://fci-h.blogspot.com/2009/12/how-to-create-c-class-from-xml-file-via.html
foreach (users_list userlist in listUserList)
{
users.InsertRange(index, userlist.users[0].user);
index += 100;
}
//so now you've list of users you can iterate and do anything with them!
//take a look this brings user friends\followers without providing user password!
Your comments are welcome :)

2 comments:

Nam said...

why is xml url error?

Ramy Mahrous said...

What's the error?!