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

#!/usr/bin/perl # tw # simple twitter reader/writer # # 2008-03-21 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-21 v001 Initial version, basic tweeting and such # # set your username and password here. # remember to chmod 700 this file! $username = "YourTwitterNameHere"; $password = "YourPasswordHere"; # the command we use to $getcmd = "curl -s -u %s:%s http://twitter.com/statuses/friends_timeline.xml"; $putcmd = "curl -s --basic --user \"%s:%s\" --data-ascii \"status=%s\" \"http://twitter.com/statuses/update.json\""; # 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; sub parseResponse { my $line; #$content =~ s/},{/__SEP__/g; #$content =~ s/^\[\{//g; ##$content =~ s/\}\]$//g; #@entries = split "__SEP__", $content; # welcome to hacksville.. population ME! my $started = 0; my $ended = 0; $junk = shift @content; $junk = shift @content; foreach $line ( @content ) { chomp $line; # figure out tags on the lines... $op1 = index( $line, "<" ); $cl1 = index( $line, ">", $op ); if( $op1 > 0 && $cl1 > 0 ) { $firstTagOnLine = substr $line, $op1+1, ($cl1-$op1-1); } else { $firstTagOnLine = ""; } $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; } if( "/" eq substr $lastTagOnLine, 0, 1 ) { $entryHash{ substr( $lastTagOnLine, 1) } = $text; $text = ""; } # available tags: # created_at, id, text (content), source, truncated, id, # name, screen_name, location, description (feed) # profile_image_url, url, protected if( "/status" eq $lastTagOnLine ) { # dump record printf "\n"; if( $entryHash{ "protected" } eq "false" ){ $prot = ""; } else { $prot = " *Private*"; } printf "Date: %s %s\n", $entryHash{ "created_at" }, $prot; printf "User: %s\n", $entryHash{ "name" }; printf " Url: http://twitter.com/%s\n", $entryHash{ "screen_name" }; if( "" ne $entryHash{ "location" } ) { printf " Loc: %s\n", $entryHash{ "location" }; } printf "Text: %s\n", $entryHash{ "text" }; } } } sub main { if( scalar @ARGV == 0 ) { $command = sprintf $getcmd, $username, $password; @content = `$command`; parseResponse(); } else { $content = join ' ', @ARGV; printf "Tweeting: %s\n", webize( $content ); $command = sprintf $putcmd, $username, $password, webize( $content ); `$command`; } } &main;