diff --git a/Repositories/otago_eprints3/otago_eprints/cfg/cfg.d/twitter.pl b/Repositories/otago_eprints3/otago_eprints/cfg/cfg.d/twitter.pl index cdc7d14..5a9c58a 100755 --- a/Repositories/otago_eprints3/otago_eprints/cfg/cfg.d/twitter.pl +++ b/Repositories/otago_eprints3/otago_eprints/cfg/cfg.d/twitter.pl @@ -1,16 +1,51 @@ $c->{eprint_status_change} = sub { - my( $eprint, $old_status, $new_status ) = @_; - - if( $new_status eq "archive" ) - { - my $twitter_username="OtagoSoBEprints"; - my $twitter_password="mmpcfjXIm1jY0cU"; - my $cmd1 = "curl -s 'http://is.gd/api.php?longurl=".$eprint->get_url."'"; - my $shorturl = `$cmd1`; - my $title = $eprint->get_value( "title" ); - $title =~ s/'/\\'/g; - my $cmd2 = "curl --basic -s --user $twitter_username:$twitter_password --data status='$title - $shorturl' http://twitter.com/statuses/update.xml"; - `$cmd2 &`; - } - }; + my( $eprint, $old_status, $new_status ) = @_; + + if( $new_status eq "archive" ) + { + my $twitter_username="OtagoSoBEprints"; + my $twitter_password="mmpcfjXIm1jY0cU"; + + # NJS 2009-12-07: Rewrote completely to check for existence + # of curl, wget, etc. Also now truncates the title if the + # tweet would otherwise be longer than 140 characters. + + my $isgdCmd = $twitterCmd = ''; + my $shorturl = ''; + + my $title = $eprint->get_value( "title" ); + $title =~ s/'/\\'/g; + if ( length( $title ) > 118 ) + { + $title = substr( $title, 0, 118 ) . '�'; + } + + SWITCH: foreach ( 'curl', 'wget' ) # add others here and below + { + # Try to call the command, if it's not in the path then $? + # will be -1 and $! will be 'No such file or directory'. + # If nothing works, just fail silently. + system( $_ ); + if ( $? != -1 ) + { + if ( /curl/ ) + { + $isgdCmd = "curl --silent 'http://is.gd/api.php?longurl=" . $eprint->get_url . "'"; + $shorturl = `$isgdCmd`; + $twitterCmd = "curl --silent --basic --user $twitter_username:$twitter_password --data status='$title - $shorturl' http://twitter.com/statuses/update.xml"; + `$twitterCmd &`; + last SWITCH; + } + if ( /wget/ ) + { + $isgdCmd = "wget --quiet --output-document=- 'http://is.gd/api.php?longurl=" . $eprint->get_url . "'"; + $shorturl = `$isgdCmd`; + $twitterCmd = "wget --quiet --http-user=$twitter_username --http-password=$twitter_password --post-data='status=$title - $shorturl' http://twitter.com/statuses/update.xml"; + `$twitterCmd &`; + last SWITCH; + } + } + } + } +};