Mon, 20 Sep 2010

Delicious URL Autoposter

After hooking up my autoposter for movies and photos, I've completed progress on a Delicious URL autoposter.

Now I'm not a big Delicious user, but I figured that I could use delicious as the clearinghouse for sharing links with my blog readers. The previous post on this blog is the first test of the script.

For anyone who runs a blosxom blog (or anything that can use or extend what I've written) here is the code... The intent is to run this via cron hourly. There are only two configuration requirements: Your delicious user id (to read your public feed) and the path to your blog entry files (wordpress/MT users can replace the file writes with db inserts and get the same thing).

#!/usr/bin/perl # Author: Khan Klatt # Released under the GNU GPL. (http://www.gnu.org/copyleft/gpl.html) # http://www.khan.org/blog/deliciouscron # an hourly delicious.com link parser for blosxom # uses the v2 (yahoo) rss feed. May need testing with old-school delicious user accounts. use DateTime::Format::Epoch; use DateTime::Format::Mail; use XML::FeedLite; # Delicious Constants my $delicious_id = 'YOUR_ID_HERE'; # Date Constants # To kickstart the first run and publish any backlog of posts, swap the following # two lines and change it back afterwards. my $global_date = time() - 36000000; # 10,000 Hours Ago my $global_date = time() - 3600; # One Hour Ago my $dt = DateTime->new( year => 1970, month => 1, day => 1 ); my $formatter = DateTime::Format::Epoch->new( epoch => $dt, unit => 'seconds', type => 'int', skip_leap_seconds => 1, start_at => 0, local_epoch => undef); # Post Constants my $path = 'PATH_TO_YOUR_BLOG_ENTRIES'; my $urlsposted = 0; # Get the feed from Delicious my $xfl = XML::FeedLite->new("http://feeds.delicious.com/v2/rss/$delicious_id"); my $delicious = $xfl->entries(); foreach my $feed (values %{$delicious}) { foreach my $item (@{$feed}) { # Convenience for item link my $link = $item->{link}[0]->{content}; # Convenience for item contents my $description = $item->{description}[0]->{content}; # Convenience for item title my $title = $item->{title}[0]->{content}; # Convenience for item date (RFC822 format) my $entry_date = $item->{pubDate}[0]->{content}; # Convenience for item date my $pub_date = DateTime::Format::Mail->parse_datetime($entry_date); # Epoch formatted date for item my $pub_date_e = $formatter->format_datetime($pub_date); # Convenience array for feed item categories my @tags = @{$item->{category}}; # Don't post stuff older than (the default of) an hour ago next if $pub_date_e <= $global_date; # Construct the Post Entry $post .= qq||; $post .= $title; $post .= qq|\n|; $post .= $description; $post .= qq||; #$post .= $pubDate; if ($tags) { $post .= "\n\nTags: "; foreach my $tag (@tags) { ; $post .= $_; } } $post =~ s/&amp;/&/g; # Fix double encoding of ampersands in HTML my $filename = $path . "/delicious" . $pub_date_e . ".txt"; umask(002); open(outfile, ">$filename") or die "Couldn't open $filename for writing."; print outfile $post; close(outfile); } } __END__

Enjoy!




Khan Klatt

Khan Klatt's photo