Wednesday, January 16, 2013

simple RSS fetcher in Perl


寂寞考,念不下書寫點程式 Orz

把一年前 SA 作業寫過的 RSS feed fetcher 拿出來重刻一次,主要用到這兩個 CPAN modules:

https://metacpan.org/module/XML::Feed
https://metacpan.org/module/WWW::Shorten::TinyURL

可以看得出這一年來 Perl 功力的成長(?) 總之現在寫的比較乾淨了 :P

#!/usr/bin/perl -CDS

use 5.014;

use XML::Feed;
use WWW::Shorten::TinyURL;

# max articles number each feed
use constant THRESHOLD => 5;

# RSS feed urls
my @urls = (
    "http://feeds2.feedburner.com/solidot",
    "http://www.36kr.com/feed",
    "http://pansci.tw/feed",
    "http://blog.gslin.org/feed",
    "https://www.linux.com/rss/feeds.php",
    "http://www.linuxplanet.org/blogs/?feed=rss2",
    "http://perlsphere.net/atom.xml",
    "http://planet.linux.org.tw/atom.xml",
    "http://security-sh3ll.blogspot.com/feeds/posts/default",
    "http://feeds.feedburner.com/TheGeekStuff",
    "http://coolshell.cn/feed",
);

# force to print
$|++;
my $done = 0;
my @data = ();
for my $url (@urls) {
    say "fetching ..." . (sprintf "%2d", int ($done/@urls * 100)) . "%";

    # get RSS Feed via URI
    my $feed = XML::Feed->parse(URI->new($url)) or die XML::Feed->errstr;

    my $count = 0;
    for my $entry ($feed->entries) {
        push @data, [$feed->title, $entry->title, $entry->link];
        $count++;
        last if ($count >= THRESHOLD);
    }
    $done++;
}

say "done!";

for (@data) {
    say "$_->[0] | $_->[1]\n$_->[2]";
    my $short =  makeashorterlink($_->[2]);
    say $short;
    say "============================================";
}

No comments:

Post a Comment