12 January 2005
This post may be outdated due to it was written on 2005. The links may be broken. The code may be not working anymore. Leave comments if needed.

任务描述

RSS 的好处和用途不用多说,简而言之就是方便有用。详细的请Google it.
我的任务是给我自己的Eplanet,就是现在用的发布程序增加个RSS功能。
详细的结果见:http://www.fayland.org/recent.rdf

脚本编写

任务有两个,第一个是创建,第二个是更新。都可以用XML::RSS来完成。
创建需要填写Channel,而更新时不需要。对于image, textinput暂时略过。

创建

Channel里有很多参数,如dc, syn, taxo等。很多我都不太懂。不过不懂就暂时先别加上,等懂了再加也不迟。

use XML::RSS;
my $RSS_file = "$Cfg{'htmldir'}/index.rdf";
my $rss = XML::RSS->new(version => '1.0');

print 'Create a new RSS file<br>';
$rss->channel(
    title        => "Fayland's",
    link         => 'http://www.1313s.com/f/',
    description  => 'What Fayland says',
    language     => 'zh-cn',
    copyright    => '(c)2005, all rights reserved',
    syn => {
        updatePeriod     => 'daily',
        updateFrequency  => '1',
        updateBase       => '2005-01-12T00:00+00:00',
    },
);
# get 20 data from database, u can get more by set LIMIT
my $sth = $dbh->prepare(
    qq{SELECT * FROM cms ORDER BY cms_id DESC LIMIT 0, 20}
);
$sth->execute();
my $cats = $sth->fetchall_arrayref({});
$sth->finish;

foreach my $cat (@$cats) {
    $rss->add_item(
        title       => "$cat->{'cms_title'}",
        link        => "http://www.1313s.com/f/$cat->{'cms_file'}.html",
        description => "$cat->{'cms_describe'}",
    );
    print "Add $cat->{'cms_title'}<br>";
}

$rss->save($RSS_file);
print 'all DOne!<br>';

更新

更新比创建还简单。就要先读取,然后再add_item, 再save就OK了。

use XML::RSS;
my $RSS_file = "$Cfg{'htmldir'}/index.rdf";
my $rss = XML::RSS->new(version => '1.0');

$rss->parsefile($RSS_file);

$rss->add_item(
    title       => "$tilte",
    link        => "http://www.1313s.com/f/$file.html",
    description => "$describe",
);
$rss->save($RSS_file);

参考



blog comments powered by Disqus