A terrible LiveJournal sync script
I wanted a really quick and dirty hack to auto-post to LiveJournal from a RSS feed. After the cut is the vile thing. Plug in your RSS feed URL and LJ account details and shove it in your crontab.
Note that it is really quick and dirty, most definitely not an example of good Perl code, and I suggest you create a scratch LJ account to test it against first.
#!/usr/bin/env perl
use warnings;
use strict;
my $FEED_URL = 'http://tripe.cabal.org.uk/atom.xml';
my($LJ_USER, $LJ_PASS) = qw( pndc ******** );
=for later
This is a very dumb and simple Perl script which will take a RSS feed (in
this case, my Improvable Tripe blog which uses Movable Type) and then
publish summaries to LiveJournal.
Wordpress has the Livepress plugin to do this syncing, so you may wish to
consider using that if you're on Wordpress.
If this randomly fails, it could be because you have a future-dated post. LJ
returns a blank error to the API, but posting on the website gives "Error
updating journal: Incorrect time value: You have an entry which was posted
at 2027-09-26 23:12, but you're trying to post an entry before this. Please
check the date and time of both entries. If the other entry is set in the
future on purpose, edit that entry to use the "Date Out of Order" option.
Otherwise, use the "Date Out of Order" option for this entry instead." So do
what it tells you.
=cut
use LWP::Simple;
use XML::Simple ':strict';
use YAML qw/ Dump DumpFile LoadFile /;
use LJ::Simple;
use HTML::Entities;
use POSIX;
use Date::Parse;
use Data::Dumper; # for debugging
my $STASH_PATH = glob("~/.syncjournal_${LJ_USER}");
#$LJ::Simple::debug = 1;
our %stash;
sub loadconf {
my $stash = eval { LoadFile($STASH_PATH) };
unless($stash) {
warn "$0: No/corrupt stash at $STASH_PATH: Will recreate";
$stash = {};
}
%stash = %$stash;
}
sub saveconf {
DumpFile($STASH_PATH, \%stash);
}
sub getfeed($) {
my $feed_url = shift;
my $feed_text = get($feed_url)
or die "Couldn't fetch $feed_url";
my $xs = new XML::Simple
ForceArray => [qw( entry link category )],
KeyAttr => {
link => 'rel',
category => 'term',
}
or die "Can't create an XML::Simple";
my $data = $xs->XMLin($feed_text);
return $data;
}
# pull in the on-disk configuration
loadconf;
# Create any missing fields and set to defaults
$stash{posted} ||= {};
# Speculatively resave the configuration, just to see if it can be saved
saveconf;
my $feed = getfeed($FEED_URL);
my $blog_title = $feed->{title};
$blog_title = encode_entities($blog_title);
my $blog_url = $feed->{link}{alternate}{href};
my @entries = @{ $feed->{entry} };
# sort into earliest post first; this way we post to LJ in date order and
# the newest will appear first.
@entries = sort { $a->{published} cmp $b->{published} } @entries;
foreach my $entry (@entries) {
my $url = $entry->{link}{alternate}{href};
my $id = $entry->{id};
my $title = $entry->{title};
my $summary = $entry->{summary};
my $ctime = $entry->{published};
my $mtime = $entry->{updated};
my @tags = keys %{ $entry->{category} };
# skip this entry if it has already been posted
next if $stash{posted}{$id};
$_ = encode_entities($_)
foreach($title, $summary, @tags);
$_ = strftime('%a, %d %b %Y %H:%M:%S', strptime $_)
foreach($ctime, $mtime);
my %results;
my $entry = <<"EOT";
<p>A new post was made to <a href="$blog_url">$blog_title</a> on $ctime. Here is the summary:</p>
<blockquote>$summary</blockquote>
<p>Read the rest of <a href="$url">$title</a>...</p>
<p align="right">[ <a href="$url">Permalink</a> | <a href="$url#comments">Comments</a> ]</p>
EOT
my %quickpost = (
user => $LJ_USER,
pass => $LJ_PASS,
subject => $title,
results => \%results,
html => 1,
entry => $entry,
# tags => \@tags,
);
#die Dumper \%quickpost;
LJ::Simple::QuickPost(%quickpost);
if($results{ok}) {
$stash{posted}{$id} = 1;
saveconf;
print "Successfully posted $title: new URL $results{url}\n";
#last;
} else {
# Post failed - don't bother attempting to make any more posts
die "Failed to post to LJ: \"$LJ::Simple::error\"\n".
Dumper(\%quickpost);
}
}
0 TrackBacks
Listed below are links to blogs that reference this entry: A terrible LiveJournal sync script.
TrackBack URL for this entry: http://tripe.cabal.org.uk/mt/mt-tb.cgi/20
I wish this worked for me. I am using ActiveState Perl under windows.
I end up with the error:
link element has no 'rel' key attribute at C:\bat\rss2livejournal.pl line 72
line 72 is: my $data = $xs->XMLin($feed_text);
Any idea?