#!/usr/bin/perl -w # twitter2at.pl - by dual # # Processes XML tweets into at jobs ################################### use strict; use Switch; use XML::Simple; use Date::Calc qw(Add_Delta_YMDHMS); # User defined variables my $USER = "user"; my $PASS = "pass"; my $USER_ID = "1234567"; my $TIMEZONE = '-7'; # Declare my @ids; my @date_orig; my $xml = shift or die "Need XML: $!"; # Open XML open my $fh, '<', "$xml" or die "Can't open statuses.xml: $!"; # Create array of tweet IDs while (<$fh>) { chomp; push(@ids, $1) if ($_ =~ /(\d+)<\/id>/ && $_ !~ /$USER_ID/); } close $fh; # Create new XML object my $statuses = XMLin($xml); # Process each ID foreach my $id (@ids) { my $mon_temp; # Grab date chomp(my $date_orig = $statuses->{status}->{$id}->{created_at}); # Parse date $date_orig =~ s/ \+0000 / /; # Thu Apr 02 02:12:15 2009 $date_orig =~ /\w{3} (\w{3}) (\d\d) (\d\d):(\d\d):(\d\d) (\d{4})/; my ($mon_orig, $day_orig, $hr_orig, $min_orig, $sec_orig, $yr_orig) = ($1, $2, $3, $4, $5, $6); switch ($mon_orig) { case /Jan/ { $mon_temp = 1 } case /Feb/ { $mon_temp = 2 } case /Mar/ { $mon_temp = 3 } case /Apr/ { $mon_temp = 4 } case /May/ { $mon_temp = 5 } case /Jun/ { $mon_temp = 6 } case /Jul/ { $mon_temp = 7 } case /Aug/ { $mon_temp = 8 } case /Sep/ { $mon_temp = 9 } case /Oct/ { $mon_temp = 10 } case /Nov/ { $mon_temp = 11 } else { $mon_temp = 12 } } # Crude handling of Daylight Stupid Time if ($mon_orig !~ /Nov|Dec|Jan|Feb/) { $TIMEZONE = '-8'; } # Calculate date my ($yr_new,$mon_new,$day_new, $hr_new,$min_new,$sec_new) = Add_Delta_YMDHMS($yr_orig,$mon_temp,$day_orig, $hr_orig,$min_orig,$sec_orig, +2,0,0, $TIMEZONE,0,0); my $mon_at = sprintf("%02d", $mon_new); my $day_at = sprintf("%02d", $day_new); my $hr_at = sprintf("%02d", $hr_new); my $min_at = sprintf("%02d", $min_new); my $date_new = "$hr_at:$min_at $yr_new-$mon_at-$day_at"; # Grab tweet and output at command my $text = $statuses->{status}->{$id}->{text}; $text =~ s/-|:|"|&|\\|\(|\)|\?//g; print "echo \"curl -u $USER:$PASS -d status=\\\"#2yrsago $text\\\" https://twitter.com/statuses/update.xml\" | at $date_new\n"; } __END__ Twitter Two Years Ago - by dual April 6, 2009 Twitter was born March 21, 2006. I got on Twitter March 28, 2007. Inspired by these anniversaries and Tweetbook [1], I thought it interesting to use Twitter to revisit my life at a medium distance, two years later. Not too soon to be recent and not so far as to be nostalgic. Quite simple, I decided to use Perl and at along with the Twitter REST API. The most difficult part was dealing with special characters, thanks to Date::Calc and estimating Daylight Saving Time. First I grabbed my tweets. There were fifty pages for the two years of '07 to '09. $ for i in $(seq 1 99); do curl -u user:pass http://twitter.com/statuses/user_timeline.xml?page=$i > tweet_$i; sleep 1; done Then I dumped a page to see what XML::Simple was dealing. #!/usr/bin/perl -w use strict; use XML::Simple; use Data::Dumper; my $xml = shift or die; my $tweets = XMLin($xml); print Dumper($tweets); Then it was writing the meat, twitter2at.pl, running it on the raw data and letting at do its thing. $ for i in raw/*; do perl twitter2at.pl $i >> temp; done && tac temp > at.sh $ bash ./at.sh I don't know what seeing myself two years ago will be like. Probably more interesting down the road as I get farther away from the project. We'll see. [1] http://booktwo.org/notebook/vanity-press-plus-the-tweetbook/