#!/usr/bin/perl # get the data from twitter and then pipe into festival or say # @daveisanidiot - 28th of August 2009 print "Please enter your Twitter username: "; $username = ; chomp($username); print "Please enter the password for $username: "; system("stty -echo"); $password = ; chomp($password); system("stty echo"); print "\n\nRunning twestival.p for \@$username\n"; # it should work straight away on a mac, on linux you might have to download festival (sudo apt-get install festival) # Mac - open terminal (Applications | Utilities | Terminal) # change to the directory you downloaded it to to where you have saved the file, eg "cd Downloads" will put you into your Downloads folder # to run type "perl twestival.p" # Linux - open terminal and do the same as above. # $print_out - if 1 this will print out the tweets in terminal, if 0 it will not $print_out = 1; # $sleep_time - this is the number of seconds before looking for new tweets $sleep_time = 120; # seconds $last_id = 1; while(1) { # get your timeline from twitter and put it in @xml @xml = split(/\n/, readpipe("curl -s -u $username:$password http://twitter.com/statuses/friends_timeline.xml?since_id=$last_id")); @to_talk = @empty; # run over and pull out screen_names, text and tweet ids foreach $xml (@xml) { if($xml =~ //) { $tweet = &strip($xml, "text"); } elsif($xml =~ //) { $screen_name = lc(&strip($xml, "screen_name")); push @to_talk, "$screen_name says $tweet"; } elsif($xml =~ //) { $last_id_temp = &strip($xml, "id"); if($last_id_temp > $last_id) { $last_id = $last_id_temp; } } elsif($xml =~ //) { print &strip($xml, "error"), "\n"; exit(0); } } foreach $to_talk (reverse(@to_talk)) { if($print_out == 1) { $to_print = $to_talk; } # need to remove the links and things # remove the &#xxx;, used for encoding some characters $to_talk =~ s/&#?\w+;//g; # remove links $to_talk =~ s/(https?:\/\/([-\w\.]+)+(:\d+)?(\/([\w\/_\.]*(\?\S+)?)?)?)/link/g; # remove ' as it breaks say $to_talk =~ s/'/\\'/g; # remove () as it breaks say $to_talk =~ s/[\(\)]//g; # if you want to see what its talking then uncomment this line if($print_out == 1) { print $to_print, "\n"; } if($^O eq "linux") { # you will need to install festival (sudo apt-get install festival) system("echo \"$to_talk\" | festival --tts"); } else { # this will only work on mac (maybe) system("say $to_talk"); } sleep(2); } sleep($sleep_time); } # strip the xml tags # call with &strip($line, "string"); sub strip() { $_[0] =~ s/^\s+//; return(substr($_[0], length($_[1])+2, -(length($_[1])+3))); }