As is so often the case..

Sheer has a bit of not-very-well-written code to share. Because nothing immediately jumped out at me for fetching LJ, here is a perl script that will fetch the latest LJ entry from the RSS feed, compare it with a database. You can also find this data in Sheer’s newly created code directory, where he will share all open source code snippits in the future: http://www.sheer.us/code

#!/usr/bin/perl

use DBI;
use Data::Dumper;
use LWP::Simple;
use XML::Simple;

my $dbh = DBI->connect(“dbi:mysql:db=obfuscated”,”because i”,”dont want you accessing it”);

fetchdata();

# yes, this could be a *lot* better

sub fetchdata()
{
my $xml = XML::Simple->new();
my $webpage = ““;
$webpage = get(“http://sheer-panic.livejournal.com/data/rss”);

#print $webpage;

$debug = 0;

if(! defined $webpage) {
# die “sheer sucks”;
return 0;
}

my $doc = $xml->XMLin($webpage);

# return($doc->{GRPVOL}{PRV});

$item = $doc->{channel}{item}[0];

# print Dumper($item);
# print Dumper($doc);

$title = $item->{‘title’};
$titleq = $dbh->quote($title);
$link = $item->{‘link’};
$linkq = $dbh->quote($link);
$guid = $item->{‘guid’}{‘content’};
$guidq = $dbh->quote($guid);
$text = $item->{‘description’};
$textq = $dbh->quote($text);
$date = $item->{‘pubDate’};
$dateq = $dbh->quote($date);

$sth = $dbh->prepare(“SELECT lj_seq FROM lj WHERE link = ‘$link'”);
$sth->execute();

($seq) = $sth->fetchrow_array();

if($seq) {
print “we got it” if ($debug);
return;
} else {
print “Title: [$title]\nlink: [$link]\nGuid:[$guid]\ntext:[$text]\ndate:[$date]\n” if ($debug);

$query = “INSERT INTO lj VALUES (NULL,$linkq,$textq,$titleq,$dateq,$guidq,NOW())”;
print “query: $query\n” if ($debug);
$dbh->do($query);

}

}

#–
#– Table structure for table ‘lj’
#–

#CREATE TABLE lj (
# lj_seq int(11) NOT NULL auto_increment,
# link text,
# description text,
# title text,
# `date` varchar(255) default NULL,
# guid text,
# t timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
# PRIMARY KEY (lj_seq)
#) ENGINE=MyISAM DEFAULT CHARSET=latin1;
<

Leave a Reply