The RSS Blog
RSS, OPML and the XML platform.
 
Copyright 2012 World Readable
The RSS Blog
Thu, 06 Oct 2011 16:25:13 GMT
Playing with Twitter API, Part Deux

Continuing to play with the twitter API. See Part Un.

Last couple days, I made a new C# program to find ppl I'm following on Twitter that didn't follow me back. I, of course, unfollowed them.

I had to cache my friends XML, since the 150 call per hour rate limit, made it impossible to run once. The code follows. Hope you like and re-use.

        static string userid = "137692493";
        static void FindNoFollows()
        {
            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/friends/ids.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(
                        string.Format("//id[text()='{0}']", userid));
                    if (dt != null)
                    {
                        continue;
                    }

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

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

            }
        }
Mon, 03 Oct 2011 08:18:53 GMT
Playing with Twitter API

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.

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/

Top Articles
  1. Unblock MySpace
  2. MySpace
  3. FaceParty, the British MySpace
  4. del.icio.us and sex.com
  5. Blocking Facebook and MySpace
  1. Review of RSS Readers
  2. MySpace Layouts
  3. RSS Stock Ticker
  4. RSS Gets an Enema
  5. Google Reader rejects del.icio.us