Created
January 11, 2009 20:30
-
-
Save gdaniels/45793 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/perl | |
########################################### | |
# This script takes the body of a FaceBook comment notification | |
# mail, sucks out a) the commenter's name, and b) the comment | |
# itself, and sends them to a designated Twitter account, in | |
# the following form: | |
# | |
# [name] comment | |
# | |
# If the tweet length would exceed 139 characters, we shorten | |
# a few different ways - first, shorten any URLs longer than 30 | |
# characters using TinyURL. Second, remove the commenter's | |
# last name. If we're still long at that point, split it up | |
# into chunks and send multiple tweets, being careful not to | |
# split URLs. | |
# | |
# To set this up, I put this rule into my .procmailrc: | |
# | |
# :0cb | |
# * ^Subject:.*commented on your status... | |
# | /home/glen/scripts/handleComment | |
# | |
# You'll have to replace <your-username> and <your-password> | |
# below with appropriate values for Twitter. | |
########################################### | |
# Note - needs LWP and WWW:Shorten (see CPAN) | |
use IO::Handle; | |
use WWW::Shorten::TinyURL; | |
use LWP; | |
my $collecting = 0; | |
my $comment; | |
my $who; | |
my $USER = "<your-username>"; | |
my $PASS = "<your-password>"; | |
while (<>) { | |
chomp($_); | |
if ((!defined $who) && /^(.*) commented on/) { | |
$who = $1; | |
} | |
# If a single line, just extract the content. | |
if (/^\"(.*)\"$/) { | |
$comment = $1; | |
last; | |
} | |
# For multi-line, start collecting at the open- | |
# quote, and end at the close-quote. | |
if (!$collecting && /^\"(.*)/) { | |
$comment = $1; | |
$collecting = 1; | |
} elsif ($collecting && /(.*)\"$/) { | |
$comment .= " $1"; | |
$collecting = 0; | |
} elsif ($collecting) { | |
$comment .= " $_"; | |
} | |
} | |
# Total length is the length of the name plus the | |
# comment plus the brackets and space. | |
$totalLength = length($who) + length($comment) + 3; | |
# Trim down if the tweet would be too long. | |
if ($totalLength > 139) { | |
# Try shortening URLs first | |
&DealWithURLs(); | |
if ($totalLength > 139) { | |
# Still long... try losing the last name | |
if ($who =~ /^(.*) /) { | |
$who = $1; | |
$totalLength = length($who) + length($comment) + 3; | |
} | |
} | |
while ($totalLength > 139) { | |
# Still long... got to split it up. | |
$realLen = 139 - (length($who) + 3); | |
$subStr = substr $comment, 0, $realLen; | |
# Try to avoid splitting a URL... | |
if ($subStr =~ /(http:\/\/\S*)$/) { | |
$realLen -= length($1); | |
$subStr = substr $comment, 0, $realLen; | |
} | |
&tweet($subStr); | |
$comment = substr $comment, $realLen; | |
$totalLength = length($who) + length($comment) + 3; | |
} | |
} | |
# Tweet! | |
&tweet($comment); | |
# Shorten URLs (assumes only one for the moment) | |
sub DealWithURLs { | |
if ($comment =~ /(http:\/\/\S*)\s*/) { | |
if (length($1) > 30) { | |
# convert to short URLs | |
$short = makeashorterlink($1); | |
$comment =~ s/$1/$short/g; | |
} | |
} | |
$totalLength = length($who) + length($comment) + 3; | |
} | |
# Send a tweet to Twitter. | |
sub tweet { | |
my($txt) = @_; | |
$status = "[$who] $txt"; | |
my $browser = LWP::UserAgent->new; | |
$browser->credentials( | |
'twitter.com:80', | |
'Twitter API', | |
$USER => $PASS | |
); | |
my $url = "http://twitter.com/statuses/update.json"; | |
my $response = $browser->post($url, [ | |
status => $status, | |
]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment