Cookie Notice

As far as I know, and as far as I remember, nothing in this page does anything with Cookies.

2008/12/27

A pass at coding for Twitter and, kinda, Blogspot

So, you write something in your blog, and you want to tell somebody, right?

Me, too. And here, I've automated the process. This script, blogspot2jabber.pl, reads your Atom feed, looks to see if the newest post is new, and then sends a tweet to your Twitter feed.

I use AnyDBM_File to save the most recent, even though I don't nearly tap the awesome power of it, because opening it as a hash is a lot easier than reading then writing a file.

XML::Atom was the tough one to install. CPAN never did finish it, so I used apt-get to install it instead. The laugh is that XML is anything like human-readable.

And Net::Twitter just could not be easier.


#!/usr/bin/perl
# ========= ========= ========= ========= ========= =========
# blogspot2twitter.pl
#
# AUTHOR - Dave Jacoby
#
# Copyright (c) 2008. David Jacoby.
#
# This program is free software; you can redistribute it
# and/or modify it under the same terms as Perl itself.
#
# See http://www.perl.com/perl/misc/Artistic.html
#
# ---------

use 5.010 ;
use strict ;
use warnings ;
use AnyDBM_File ;
use Carp ;
use Net::Twitter ;
use XML::Atom::Client ;
use XML::Atom::Entry ;
use XML::Atom::Feed ;
use subs qw( get_entry send_to_twitter ) ;

dbmopen my %dbm, '/path/to/.database.dbm', 0600 or croak $! ;
if ( !defined $dbm{ 'current' } ) { $dbm{ 'current' } = '' ; }
say 'C ' . $dbm{ 'current' } ;

my $url = 'your_blogspot_Atom_ feed' ;
# I think that it'll autodetect if you just put the URL
my $entry = get_entry $url ;
my $text = 'New post to my blog - ' . $entry ;

if ( $entry ne $dbm{ 'current' } ) {
send_to_twitter $text ;
$dbm{ 'current' } = $entry ;
}

exit ;

# ===================================================================
sub get_entry {
my $url = shift ;
my $tweet_link ;
my $client = XML::Atom::Client->new ;
my $feed = $client->getFeed( $url ) ;
my @entries = $feed->entries ;

for my $link ( $entries[ 0 ]->link ) {
if ( $link->type eq 'text/html' ) {
$tweet_link = $link->href ;

#we should be picking it up 2nd pass
}
}
return $tweet_link ;
}

# -------------------------------------------------------------------

# ===================================================================
sub send_to_twitter {
my $user = 'username' ;
my $pass = 'password' ;
my $status = shift ;
my $tweet = Net::Twitter->new( username => $user,
password => $pass, ) ;
if ( $tweet->update( $status ) ) { say 'OK' ; }
else { say 'FAIL' ; }
}
# -------------------------------------------------------------------


I'd like to get the URL and subject in, but that can't be guaranteed to be under 140 characters.