Get a Twitter Account’s Registration Date via Snow Leopard Service

Creating services in Snow Leopard is all the rage lately. So is Twitter. This tutorial brings both of them together and should serve as a fine example of just how slick and useful services in Snow Leopard can be.

Step 1: Write a script to grab a twitter account’s registration date

I recently became curious about how long some of the people I follow have been on Twitter. Using this info from Twitter’s API documentation, I made the following curl command to grab info on my own account:

curl -s http://twitter.com/users/show.xml?screen_name=pmbuko

This returns a bunch of info in XML format:

<?xml version="1.0" encoding="UTF-8"?>
<user>
  <id>14845946</id>
  <name>Peter Bukowinski</name>
  <screen_name>pmbuko</screen_name>
  <location>Virginia</location>
  <description>IT geek, father of two, California ex-patriate, frequent flier</description>
  <profile_image_url>http://a3.twimg.com/profile_images/368893865/wtfisthat_normal.jpg</profile_image_url>
  <url>http://yourmacguy.wordpress.com</url>
  <protected>false</protected>
  <followers_count>80</followers_count>
  <profile_background_color>E9DCBD</profile_background_color>
  <profile_text_color>323232</profile_text_color>
  <profile_link_color>FF5500</profile_link_color>
  <profile_sidebar_fill_color>0055FF</profile_sidebar_fill_color>
  <profile_sidebar_border_color>0000FF</profile_sidebar_border_color>
  <friends_count>78</friends_count>
  <created_at>Tue May 20 14:45:16 +0000 2008</created_at>
  <favourites_count>11</favourites_count>
  <utc_offset>-18000</utc_offset>
  <time_zone>Eastern Time (US &amp; Canada)</time_zone>
  <profile_background_image_url>http://a3.twimg.com/profile_background_images/2548407/background3.jpg</profile_background_image_url>
  <profile_background_tile>true</profile_background_tile>
  <statuses_count>1361</statuses_count>
  <notifications>false</notifications>
  <verified>false</verified>
  <following>false</following>
  <status>
    <created_at>Sat Sep 12 01:38:58 +0000 2009</created_at>
    <id>3924831726</id>
    <text>@hotdogsladies That's the best description I've heard yet. You must be between Durant and Dwight.</text>
    <source>&lt;a href=&quot;http://www.atebits.com/&quot; rel=&quot;nofollow&quot;&gt;Tweetie&lt;/a&gt;</source>
    <truncated>false</truncated>
    <in_reply_to_status_id>3924654818</in_reply_to_status_id>
    <in_reply_to_user_id>749863</in_reply_to_user_id>
    <favorited>false</favorited>
    <in_reply_to_screen_name>hotdogsladies</in_reply_to_screen_name>
  </status>
</user>

The line that contains the info I want is the first <created_at> line, showing that I registered on May 20, 2008 — a relative late-comer, I know…. If you’ve visited this blog before, you’re likely aware of my love for awk. I’ll pipe curl’s output to awk to isolate the data I want:

curl -s http://twitter.com/users/show.xml?screen_name=pmbuko | \
awk '/^  <created_at>/{sub("</created_at>","");print $2" "$3", "$6}'

This outputs “May 20, 2008″ — great! Now let’s build a service with Automator that will allow us to right-click on any Twitter account name and have a dialog pop up with that account’s registration date.

Step 2: Build a service with Automator

Open up Automator.app, found in your Applications folder. You’ll be presented with some workflow template choices. Double-click the Service item.

SLS1

Note that the Service template has an input section already at the top.

SLS2

Leave the options set to defaults. Now type “shell” into the search box in the upper left area of the window and then drag the Run Shell Script action into the workflow area.

SLS3

Leave the Shell option set to /bin/bash, but change the Pass input option to as arguments. Replace the placeholder data in the script area with the following:

curl -s http://twitter.com/users/show.xml?screen_name=$1 | \
awk '/^  <created_at>/{sub("</created_at>","");print $2" "$3", "$6}'

Note that I used “$1” instead of an account name. In bash, the $1 variable in a script contains the first argument passed to the script. In this case, we’re passing whatever text we selected or right-clicked on to the script. Now we need a way to display the text on screen. AppleScript is perfect for this, so type “script” into the search box and then drag the Run AppleScript action to the bottom of the workflow area.

SLS4.1

Now, replace the contents of the script area with this:

on run {regDate}
    tell application "Finder"
        activate
	display dialog "Twitter account registered on " & regdate & "." buttons{"OK"}
    end tell
end run

The first line imports the info from the previous step to a variable named regDate. Then we’re telling the Finder to display a dialog with the text “Twitter account registered on [regDate].” Simple, right? Now click the hammer icon in the Applescript action to compile the script and check for errors. You’ll see the text change font and colors if all went well.

SLS5.1

Step 3: Save and test the service

You’re done building the service, so save it. I named mine Get Twit Age, but feel free to name yours however you want. Snow Leopard’s Automator will automatically save it in the correct location for use. In order to test this service, you need to be running a Cocoa application1 that has selectable text. Since we’re talking about Twitter, here, may I suggest Safari? Open the page of a Twitter user’s account (http://twitter.com/username) that you’d like to know the age of. Right-click on their account name and then select Get Twit Age (or whatever you called your service) near the bottom of the contextual menu. In a few moments, you should see a dialog box containing the account’s registration date.

SLS6.1

And there you have it. If you happen to right-click on some text that doesn’t correspond to a twitter account, you won’t get an error but the dialog will simply display “Twitter account registered on .”

Hopefully, this tutorial gave you an idea of how easy it is to combine snippets of code to create a useful service. Now get out there and automate!


1. John Gruber points out in his article about iTunes 9 and Cocoa that while all apps can access system-wide services via the Services item in the Application menu, only Cocoa-based apps can access contextual services.

About these ads

About pmbuko
mac/linux geek

2 Responses to Get a Twitter Account’s Registration Date via Snow Leopard Service

  1. ANDY says:

    6/27/11

    FROM:
    PECD123@HOTMAIL.COM

    TO:
    http://yourmacguy.wordpress.com/2009/09/12/twitter-reg-date-via-sl-services/

    HI!

    help!

    curl -s http://twitter.com/users/show.xml?screen_name=pmbuko

    didn’t work for me, at some unknown date seem like someone opened an account on my name…

    the account has never been used,

    name details look ‘fishy’ to me…

    can not reset password, emails do not match…

    getting the date i could check my dairy for clues…
    ========
    http://search.babylon.com/?babsrc=SP_ss&mntrId=28262dd0000000000000001143c47a11&tlver=1.4.19.19&instlRef=sst&ss=1&affID=18025&q=curl+-s+http%3A%2F%2Ftwitter.com%2Fusers%2Fshow.xml%3Fscreen_name%3Dpmbuko

    Web Results

    Your search – curl -s http://twitter.com/users/show.xml?screen_name=pmbuko – did not match any documents.

    Suggestions:

    Make sure all words are spelled correctly.
    Try different keywords.
    Try more general keywords.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 481 other followers

%d bloggers like this: