[ /lj/tw/002.txt ]   PHP source

#!/usr/bin/perl # # tw # simple twitter reader/writer # # Project #123 # http://umlautllama.com/project.php?ShortName=tw # # 2008-03 Scott Lawrence http://twitter.com/yorgle # yorgle@gmail.com # http://umlautllama.com # usage: # tw displays the most recent twits, newest first # tw my content post "my content" to your twitter account # # Version Info: # # 2008-03-22 v002 Rework the tweets into an array, reverse sorting # # 2008-03-21 v001 Initial version, basic tweeting and such # # set your username and password here. # remember to chmod 700 this file! my $username = "yourUsernameHere"; my $password = "yourPasswordHere"; # the command we use to get the current timeline my $getcmd = "curl -s -u %s:%s http://twitter.com/statuses/friends_timeline.xml"; # the command we use to push a tweet to the server my $putcmd = "curl -s --basic --user \"%s:%s\" --data-ascii \"status=%s\" \"http://twitter.com/statuses/update.json\""; # we'll use these to mash up our returned data into a sortable array my $sep1 = "____Y0RGL3____"; my $sep2 = "____3LGR0Y____"; # webize # Convert special characters to be web form friendly sub webize { my ($text); $text = shift; $text =~ s/(\W)/ sprintf "%%%02lX", ord $1 /eg; return $text; } my @content, @sortedEntries; # take the XML from the server, and work it into our own sortedEntries sub parseResponse { my $line; my $c = 0; # welcome to hacksville.. population ME! my $started = 0; my $ended = 0; # remove the first two lines... $junk = shift @content; $junk = shift @content; # and parse the entire content foreach $line ( @content ) { chomp $line; # first tag on the line $op1 = index( $line, "<" ); $cl1 = index( $line, ">", $op ); if( $op1 > 0 && $cl1 > 0 ) { $firstTagOnLine = substr $line, $op1+1, ($cl1-$op1-1); } else { $firstTagOnLine = ""; } # last tag on the line $op2 = rindex( $line, "<" ); $cl2 = rindex( $line, ">" ); if( $op2 > 0 && $cl2 > 0 ) { $lastTagOnLine = substr $line, $op2+1, ($cl2-$op2-1); } else { $lastTagOnLine = ""; } # if first !+ last, text is in the middle # if first == last, # if[0] == /, text preceeds # else, text follows # otherwise, text is entire line if( $firstTagOnLine ne $lastTagOnLine ) { $text .= " " . substr $line, $cl1+1, $op2-$cl1-1; } elsif( $firstTagOnLine ne "" ) { if( "/" ne substr $firstTagOnLine, 0, 1 ) { $text .= " " . substr $line, $cl1+1; } else { $text .= " " . substr $line, 0, $op2; } } else { $text .= " " . $line; } $text =~ s/^\s+//g; $text =~ s/\s+$//g; # do stuff if( $firstTagOnLine == "status" ) { $started = 1; } if( $lastTagOnLine == "/status" ) { $ended = 1; } if( $ended ) { # reset $ended = 0; $started = 0; } # see if the last tag on the line is a close tag # if so, store aside the text content for the tag if( "/" eq substr $lastTagOnLine, 0, 1 ) { $entryHash{ substr( $lastTagOnLine, 1) } = $text; $text = ""; } # available tags: # created_at, id, text (content), source, truncated, # name, screen_name, location, description (feed) # profile_image_url, url, protected if( "/status" eq $lastTagOnLine ) { # reformat and store the record # order is not important here, it gets extracted back # out into a hash later, after sorting $entry = "order" . $sep2 . (sprintf "%03d", $c++) . $sep1 . "created_at" . $sep2 . $entryHash{ "created_at" } . $sep1 . "protected" . $sep2 . $entryHash{ "protected" } . $sep1 . "source" . $sep2 . $entryHash{ "source" } . $sep1 . "truncated" . $sep2 . $entryHash{ "truncated" } . $sep1 . "name" . $sep2 . $entryHash{ "name" } . $sep1 . "profile_image_url" . $sep2 . $entryHash{ "profile_image_url" } . $sep1 . "url" . $sep2 . $entryHash{ "url" } . $sep1 . "screen_name" . $sep2 . $entryHash{ "screen_name" } . $sep1 . "location" . $sep2 . $entryHash{ "location" } . $sep1 . "text" . $sep2 . $entryHash{ "text" } . $sep1 . $sep1 . "id" . $sep2 . $entryHash{ "id" } . $sep1; push @sortedEntries, $entry; } } } # dump out the list of items sub displayResponse { my $entry, @items, %ihash; foreach $entry ( @displayEntries ) { $ihash = {}; # first, extract out the hash. @items = split $sep1, $entry; foreach $item ( @items ) { ( $key, $value ) = split $sep2, $item; $ihash{ $key } = $value; } # now pretty print it for ourselves printf " Ord: %02d\n", $ihash{ "order" }; printf "Date: %s %s\n", $ihash{ "created_at" }, $prot; if( $ihash{ "protected" } ne "false" ){ printf "Prot: PROTECTED\n"; } printf "User: %s\n", $ihash{ "name" }; printf " Url: http://twitter.com/%s\n", $ihash{ "screen_name" }; if( "" ne $ihash{ "location" } ) { printf " Loc: %s\n", $ihash{ "location" }; } printf "Text: %s\n", $ihash{ "text" }; printf "\n"; } } # do stuff sub main { if( scalar @ARGV == 0 ) { $command = sprintf $getcmd, $username, $password; @content = `$command`; parseResponse(); @displayEntries = reverse @sortedEntries; displayResponse(); } else { $content = join ' ', @ARGV; printf "Tweeting: %s\n", webize( $content ); $command = sprintf $putcmd, $username, $password, webize( $content ); `$command`; } } &main;