summaryrefslogtreecommitdiffstats
path: root/bin/update_twitter
diff options
context:
space:
mode:
Diffstat (limited to 'bin/update_twitter')
-rw-r--r--bin/update_twitter46
1 files changed, 46 insertions, 0 deletions
diff --git a/bin/update_twitter b/bin/update_twitter
new file mode 100644
index 0000000..c5eb888
--- /dev/null
+++ b/bin/update_twitter
@@ -0,0 +1,46 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+# PODNAME: update_twitter
+
+use JSON;
+use Linkulator::Twitter;
+use Path::Class;
+use Try::Tiny;
+
+my $state_file = 'state.json';
+if (!-e $state_file) {
+ file($state_file)->openw->print(encode_json({last_tweeted => 0}));
+}
+
+my $conf = decode_json(file($state_file)->slurp);
+
+my $lt = Linkulator::Twitter->new(
+ exists $conf->{token} && exists $conf->{token_secret}
+ ? (twitter_access_token => $conf->{token},
+ twitter_access_token_secret => $conf->{token_secret})
+ : (),
+);
+
+if (my $url = $lt->authenticate_twitter) {
+ print "Authorize this app at $url and enter the pin here: ";
+
+ my $pin = <STDIN>;
+ chomp $pin;
+
+ my ($token, $secret) = $lt->authenticate_twitter($pin);
+ $conf->{token} = $token;
+ $conf->{token_secret} = $secret;
+}
+
+try {
+ $lt->update;
+ for my $link (sort { $a->id <=> $b->id } $lt->links) {
+ next if $link->id <= $conf->{last_tweeted};
+ $lt->tweet($link);
+ $conf->{last_tweeted} = $link->id;
+ }
+}
+finally {
+ file($state_file)->openw->print(encode_json($conf));
+};