Wednesday, May 13, 2009

instrumenting twitter with perl

Yesterday decided that I finally want to use twitter. Too much noise around, however it look like a nice thing that doesn't take much time. And useful for tiny announces.

What a concidence, lately I got addicted to music by Glen Hansard and Marketa Irglova from Once movie. Twitter is perfect thing to say what you're listing to right now. Why not?

    sudo cpan Net::Twitter
    vi ~/bin/tw_song
    # 10 minutes of typing
    :wq

One slight problem is how to get the current song from iTunes.

    # googling
    # AppleScript editor

    if appIsRunning("iTunes") then
            tell application "iTunes"
                    artist of current track & " - " & name of current track
            end tell
    end if

    on appIsRunning(appName)
            tell application "System Events" to (name of processes) contains appName
    end appIsRunning

That's it. From command line you can use it the following way:

    osascript /Users/ruz/Library/Scripts/say_current_itunes_song.scpt

Here is final perl script:

    #!perl

    use strict;
    use warnings;

    my $song = `osascript /Users/ruz/Library/Scripts/say_current_itunes_song.scpt`;
    return unless $song;
    $song =~ s/^\s*//;
    $song =~ s/\s*$//;

    my $status = "Listening $song";
    print $status, "\n";

    require Net::Twitter;
    my $twit = Net::Twitter->new({username=>"myuser", password => "mypass" });
    $twit->update({status => $status});

No comments:

Post a Comment