#!/usr/bin/perl -w # a daily del.icio.us link parser for Wordpress # this perl script uses Net::Delicious to access a user's del.icio.us # account, filters the links by the current GMT date, and posts an # entry containing each link, the link's extended description, and # the link's tags # set $wp_timezone under time variables and the user-configurable # variables below before using # NOTE: this script is designed to be run from crontab once a day, # at approximately midnight GMT time # Author: Stephen Le (http://stephen.evilcoder.com) use strict; use DateTime; use DateTime::TimeZone; use Net::Delicious; use DBI; # time variables my $wp_timezone = "US/Pacific"; # the timezone you have set in Wordpress my $time_local = DateTime->now; my $time_wp = $time_local->clone()->set_time_zone($wp_timezone); my $time_gmt = $time_local->clone()->set_time_zone('GMT'); my $time_wp_day = $time_wp->strftime("%G-%m-%d"); my $time_gmt_day = $time_gmt->strftime("%G-%m-%d"); ############################### # user-configurable variables # ############################### my $del_username = ""; # your del.icio.us username my $del_password = ""; # your del.icio.us password my $wp_userid = "1"; # wordpress userid to post as my $post_title = "Links for " # title of post . $time_wp_day; my $allowcomments = "open"; # allow comments? (open|closed) my $allowpings = "open"; # allow pings? (open|closed) my $post_name = "daily-links"; # post slug my $category_id = "1"; # category for post # you can get the following information from wp-config.php my $db_host = "localhost"; # wordpress database server my $db_user = ""; # wordpress database user my $db_pass = ""; # wordpress database password my $db_name = ""; # wordpress database my $prefix = "wp_"; # wordpress database prefix ################################### # end user-configurable variables # ################################### # initialize Net::Delicious objects and get posts my $del = Net::Delicious->new( { user => $del_username, pswd => $del_password } ); die "Unable to connect to del.icio.us.\n" unless $del; my @posts = $del->posts( { dt => $time_gmt_day } ); # initialize database my $dbh = DBI->connect( "DBI:mysql:$db_name:$db_host", $db_user, $db_pass ) or die "Couldn't connect to database: " . DBI->errstr; my $tb_posts = $prefix . "_posts"; my $tb_post2cat = $prefix . "_post2cat"; my $sth = $dbh->prepare( "INSERT INTO $tb_posts (post_author, post_date, post_date_gmt, post_content, post_title, post_status, comment_status, ping_status, post_name, post_modified, post_modified_gmt) VALUES (?,?,?,?,?,?,?,?,?,?,?)" ) or die "Couldn't prepare statement: " . dbh->errstr; my $gth = $dbh->prepare("SELECT ID FROM $tb_posts ORDER BY ID DESC LIMIT 1") or die "Couldn't prepare statement: " . dbh->errstr; my $lth = $dbh->prepare( "INSERT INTO $tb_post2cat (post_id,category_id) VALUES (?,$category_id)") or die "Couldn't prepare statement: " . dbh->errstr; my $html; # generate the HTML foreach (@posts) { my ( $href, $description, $tags, $extended ) = ( $_->href, $_->description, $_->tags, $_->extended ); my $line = "
\n" . "$description"; if ($extended) { $line .= "\n$extended"; } if ($tags) { $line .= "\n(tags:"; foreach ( ( split /\s+/, $tags ) ) { $line .= " $_"; } $line .= ")"; } $line .= "
\n"; # close HTML tag $html .= $line; } # post the entry to the database if ($html) { # we need the date in "YYYY-MM-DD HH:MM:SS" format my $now = $time_wp->strftime("%G-%m-%d %H-%M-%S"); my $now_gmt = $time_gmt->strftime("%G-%m-%d %H-%M-%S"); # put the post into the WP database $sth->execute( $wp_userid, $now, $now_gmt, $html, $post_title, 'publish', $allowcomments, $allowpings, $post_name, $now, $now_gmt ); $sth->finish; # get the post ID $gth->execute(); my $post_id = $gth->fetchrow_array(); $gth->finish; # assign the category to the post $lth->execute($post_id); $lth->finish; } $dbh->disconnect;