The RSS Blog

News and commentary from the RSS and OPML community.

Yesterday, I did something fun and played with the twitter API. I was trying to reduce my twitter followings and want to purge all followings with no activity in the last month. With 900+ followings, that would be too much to hand manually, so automation to the rescue.

This required the use of 2 API calls.

https://api.twitter.com/1/friends/ids.xml?screen_name={0}


https://api.twitter.com/1/users/lookup.xml?user_id={0}

One to load the list of my friends and the 2nd to turn that list of ids into user profiles. You can then check the creation date of the users status to get his last activity date.

Because of the REST XML API style, this was trivial to use. Not to mention the documentation on the website was better than adequate. I did find a few problems using the API.

  • Complete lack of use of XML attributes. Even when attributes are called for, they use XML elements instead. Not the 1st time I've seen this usage style. Not a biggy either.
  • Date times don't use the standard XML date-time format. In fact, I don't recognize the format at all. Neither did the C# Convert.ToDateTime function.
  • Usage rate of 150 calls an hour was completely inadequate. I needed almost 1000 calls to accomplish my simple task.

Code follows

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = string.Format("https://api.twitter.com/1/friends/ids.xml?screen_name={0}", "talkSportscom");
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            if (!System.IO.File.Exists("ids.xml"))
            {
                doc.Load(s);
            }
            else
            {
                doc.Load("ids.xml");
            }

            foreach (System.Xml.XmlElement e in doc.SelectNodes("//id"))
            {
                try
                {
                    if (e.HasAttribute("done"))
                    {
                        continue;
                    }
                    e.SetAttribute("done", "true");
                    doc.Save("ids.xml");

                    // need to sleep 30 seconds between calls to avoid 150 rate limit per hour
                    //System.Threading.Thread.Sleep(30 * 1000);

                    s = string.Format("https://api.twitter.com/1/users/lookup.xml?user_id={0}", e.InnerText);
                    System.Xml.XmlDocument d = new System.Xml.XmlDocument();
                    d.Load(s);

                    System.Console.WriteLine(d.OuterXml);

                    System.Xml.XmlElement dt = (System.Xml.XmlElement)d.SelectSingleNode("//status/created_at");
                    if (dt != null)
                    {
                        string dt2 = dt.InnerText;
                        dt2 = dt2.Substring(4, 6);
                        System.DateTime dateTime = Convert.ToDateTime(dt2);
                        if (System.DateTime.UtcNow.AddMonths(-1) < dateTime)
                        {
                            continue;
                        }
                    }

                    dt = (System.Xml.XmlElement)d.SelectSingleNode("//screen_name");

                    System.Diagnostics.Process.Start(string.Format("http://twitter.com/#!/{0}", dt.InnerText));
                }
                catch
                {
                    break;
                }

            }
        }
    }
}

Fun and task accomplished. Thanks twitter. I got rid of a few dozen twitter followings.

https://dev.twitter.com/

Reader Comments Subscribe
Toronto News are full of an Apple store discount on black Friday. But it is not so big to talk about...
welcome to (www.MallsJerseys.com) We are specialized in high quanlity jerseys we are wholesale NFL MLB NBA NHL Women Youth Jerseys $17 a pcs , free shipping any questions or needs are welcome ,we will endeavor to do best service for you. thanks in advance.
Type "339":