[or-cvs] r21430: {} perl script to post bridges to twitter (projects/misc)
Runa Sandvik
runa.sandvik at gmail.com
Mon Jan 18 17:33:59 UTC 2010
Author: runa
Date: 2010-01-18 17:33:59 +0000 (Mon, 18 Jan 2010)
New Revision: 21430
Added:
projects/misc/twitter.pl
Log:
perl script to post bridges to twitter
Added: projects/misc/twitter.pl
===================================================================
--- projects/misc/twitter.pl (rev 0)
+++ projects/misc/twitter.pl 2010-01-18 17:33:59 UTC (rev 21430)
@@ -0,0 +1,103 @@
+#!/usr/bin/perl
+#
+# Author: Runa Sandvik, <runa.sandvik at gmail.com>
+#
+# This is Free Software (GPLv3)
+# http://www.gnu.org/licenses/gpl-3.0.txt
+#
+# The script will take a text file as input, pick three lines and post
+# the lines to Twitter. The script will also write the same three lines
+# to a second file so that we can keep track of the lines we have
+# already tweeted.
+#
+
+use strict;
+use warnings;
+use Net::Twitter;
+
+# The file with the lines we wish to tweet
+my $tweet = "filename1";
+
+# The file with the lines we have tweeted
+my $tweeted = "filename2";
+
+# Twitter credentials
+my $user = "username";
+my $password = "password";
+
+# Connect to Twitter
+my $twitter = Net::Twitter->new(
+ traits => [qw/API::REST/],
+ username => $user,
+ password => $password,
+ ssl => 1
+);
+
+# Verify the credentials
+my $result = $twitter->verify_credentials();
+if (!$result) {
+ die ("Could not log in. Wrong username/password?");
+}
+
+# Open the first file for reading
+my $r_tweet;
+my @c_tweet;
+open $r_tweet, "<", $tweet or die ("Could not open $tweet for reading");
+ at c_tweet = <$r_tweet>;
+close $r_tweet;
+
+# Open the second file for reading
+my $r_tweeted;
+my @c_tweeted;
+open $r_tweeted, "<", $tweeted or die ("Could not open $tweeted for reading");
+ at c_tweeted = <$r_tweeted>;
+close $r_tweeted;
+
+# Figure out the diff between the two files
+my $item;
+my $count;
+my @diff = ( );
+my %count = ( );
+
+foreach $item (@c_tweet, @c_tweeted) {
+ $count{$item}++;
+}
+
+foreach $item (keys %count) {
+ if ($count{$item} != 2) {
+ push @diff, $item;
+ }
+}
+
+# Pick three lines
+my @lines;
+my $i = 0;
+while ($i < 3) {
+ if($diff[$i]) {
+ chomp($diff[$i]);
+ push @lines, $diff[$i];
+ $i++;
+ }
+ else {
+ die("Nothing more to tweet");
+ }
+}
+
+# Cut the newlines
+my $line1 = $lines[0];
+my $line2 = $lines[1];
+my $line3 = $lines[2];
+
+# Post to Twitter
+eval { $twitter->update("$line1 $line2 $line3") };
+if ($@) {
+ die("Update failed because: $@\n");
+}
+
+# Open $tweeted for writing
+my $write;
+open $write, ">>", $tweeted or die("Could not open $tweeted for writing");
+print $write "$line1\n";
+print $write "$line2\n";
+print $write "$line3\n";
+close $write;
More information about the tor-commits
mailing list