S
Sergei Shelukhin
Hi
Here I prase opml blogroll into linkage database for my embarasingly
primitive blog engine I wrote slowly to study Perl basics.
One thing I already know is that common is an evil name for custom module. I
will hcange that in production version
I am also aware of being able to refactor query-prepare out from the loop,
running away to th uni now
#!/usr/bin/perl
use strict;
use lib qw(/usr/local/lib/perl5/site_perl/5.6.1);
use lib qw(/home/virtual/html/raven.is.rathercute.com/cgi);
use DBI;
use XML:arser;
use XML:OM;
use IO::Handle;
use common;
############################################################################
######
my $db = Common::connect();
my $io = new IO::Handle;
my $fname;
my $new = 0;
my $old = 0;
my $p = new XML:OM:arser();
print "OPML file name: ";
if (!$io->fdopen(fileno(STDIN),"r"))
{
die "error initialising i/o";
}
$fname = $io->getline;
my $opml = $p->parsefile($fname) or die "File not found";
my $nodes = $opml->getElementsByTagName("outline");
my $n = $nodes->getLength;
$db->do("ALTER TABLE Linkage ADD COLUMN SaveMe bit");
$db->do("UPDATE Linkage SET SaveMe = 0");
$db->do("UPDATE Linkage SET SaveMe = 1 WHERE Blog = 0 OR Feed = ''");
for (my $i = 0; $i < $n; $i++)
{
my $node = $nodes->item ($i);
next if ($node->hasChildNodes); #outline cats
my ($title,$url,$feed) = ($node->getAttributeNode("title")
,$node->getAttributeNode("htmlUrl"),$node->getAttributeNode("xmlUrl"));
$title = $title->getValue if $title;
$feed = $feed->getValue if $feed;
if ($url )
{
$url = $url->getValue
}
else
{
# print "URL for the \"$title\" not found, please supply the url: ";
# $url = $io->getline;
$url = '';
}
my $query = $db->prepare("SELECT * FROM Linkage WHERE Feed = ?");
$query->execute($feed);
if (my $row = $query->fetchrow_hashref())
{
$db->do("UPDATE Linkage SET SaveMe = 1 WHERE Feed = ?",undef,$feed);
++$old;
}
else
{
$db->do("INSERT INTO Linkage (Name,Url,Feed,Blog,SaveMe) VALUES
(?,?,?,1,1)",undef,$title,$url,$feed);
++$new;
}
}
$db->do("DELETE FROM Linkage WHERE SaveMe = 0");
$db->do("ALTER TABLE Linkage DROP COLUMN SaveMe");
print "$new new feeds, $old old feeds\n";
$io->close;
$db->disconnect();
Here I prase opml blogroll into linkage database for my embarasingly
primitive blog engine I wrote slowly to study Perl basics.
One thing I already know is that common is an evil name for custom module. I
will hcange that in production version
I am also aware of being able to refactor query-prepare out from the loop,
running away to th uni now
#!/usr/bin/perl
use strict;
use lib qw(/usr/local/lib/perl5/site_perl/5.6.1);
use lib qw(/home/virtual/html/raven.is.rathercute.com/cgi);
use DBI;
use XML:arser;
use XML:OM;
use IO::Handle;
use common;
############################################################################
######
my $db = Common::connect();
my $io = new IO::Handle;
my $fname;
my $new = 0;
my $old = 0;
my $p = new XML:OM:arser();
print "OPML file name: ";
if (!$io->fdopen(fileno(STDIN),"r"))
{
die "error initialising i/o";
}
$fname = $io->getline;
my $opml = $p->parsefile($fname) or die "File not found";
my $nodes = $opml->getElementsByTagName("outline");
my $n = $nodes->getLength;
$db->do("ALTER TABLE Linkage ADD COLUMN SaveMe bit");
$db->do("UPDATE Linkage SET SaveMe = 0");
$db->do("UPDATE Linkage SET SaveMe = 1 WHERE Blog = 0 OR Feed = ''");
for (my $i = 0; $i < $n; $i++)
{
my $node = $nodes->item ($i);
next if ($node->hasChildNodes); #outline cats
my ($title,$url,$feed) = ($node->getAttributeNode("title")
,$node->getAttributeNode("htmlUrl"),$node->getAttributeNode("xmlUrl"));
$title = $title->getValue if $title;
$feed = $feed->getValue if $feed;
if ($url )
{
$url = $url->getValue
}
else
{
# print "URL for the \"$title\" not found, please supply the url: ";
# $url = $io->getline;
$url = '';
}
my $query = $db->prepare("SELECT * FROM Linkage WHERE Feed = ?");
$query->execute($feed);
if (my $row = $query->fetchrow_hashref())
{
$db->do("UPDATE Linkage SET SaveMe = 1 WHERE Feed = ?",undef,$feed);
++$old;
}
else
{
$db->do("INSERT INTO Linkage (Name,Url,Feed,Blog,SaveMe) VALUES
(?,?,?,1,1)",undef,$title,$url,$feed);
++$new;
}
}
$db->do("DELETE FROM Linkage WHERE SaveMe = 0");
$db->do("ALTER TABLE Linkage DROP COLUMN SaveMe");
print "$new new feeds, $old old feeds\n";
$io->close;
$db->disconnect();