EDGE Now!外部DBを使いEDGE Now!のRSSを作る
スポンサードリンク
おととい作ったEDGE Now!外部DBは、EDGE Now!で参加者が紹介してくれたURLを、紹介された順番で登録していますので、それを利用すると、RSSができるのではないかと思いました。
というわけで早速スクリプト。
Filename: index.rdf
#!/usr/bin/perl
use strict;
use warnings;
use CGI;
use DBI;
use LWP::UserAgent;
use URI::Escape;
use XML::Simple;
use XML::RSS;
use utf8;
use Encode;
binmode STDOUT => ':utf8';
my $q = CGI->new();
my $result = get_data();
my $rss = rss($result);
if ($ENV{GATEWAY_INTERFACE}) {
require CGI;
print CGI::header('text/xml; charset=utf-8');
}
print $rss;
exit;
sub rss {
my $items = shift;
my $rss = XML::RSS->new;
$rss->add_module(
prefix => 'content',
uri => 'http://purl.org/rss/1.0/modules/content/',
);
$rss->channel(
title => 'EDGE Now! 今注目されているサイト',
link => 'http://edgenow.jp/',
description => 'EDGE Now! 今注目されているサイトのRSS',
language => 'ja',
dc => {
publisher => 'アドビシステムズ株式会社',
contributor => 'DTPWiki.jp',
},
syn => {
updatePeriod => 'daily',
updateFrequency => '12',
},
);
foreach my $item (@$items) {
$rss->add_item(
link => $item->{'url'},
title => _escape( decode('utf8',
$item->{'title'}
) ),
description => _escape(
decode('utf8',
"$item->{'title'} "
."$item->{url}"
) ),
dc => {
date => _date( $item->{'ctime'} ),
},
);
}
return $rss->as_string() ;
}
sub get_data {
my $db_path = 'edgenow_sites.db';
my $dbh = DBI->connect( "dbi:SQLite:dbname=$db_path",
"","", {AutoCommit => 0} );
my $sth = $dbh->prepare("SELECT * FROM urls "
."ORDER BY id DESC LIMIT 50");
my $result = $sth->execute() || die $!;
my $hash;
while (my $ref = $sth->fetchrow_hashref) {
push @$hash, $ref;
}
$dbh->commit();
return $hash;
}
sub _date {
my $date = shift;
$date =~s/\s/T/g;
$date .= '+00:00';
return $date;
}
sub _escape {
my $str =shift;
$str =~s/&/&/g;
return $str;
}
__END__
これは実際にアクセスできます。
http://labo.dtpwiki.jp/edgenow/index.rdf
だいたいSQLiteのDBの扱い方にも慣れてきましたので、次回は、URLを問い合わせると、紹介した人の人数をEDGE Now!に問い合わせして、User数として返すXML-RPCインターフェースをつけてみようと思います。
スポンサードリンク
トラックバック(0)
トラックバックURL: http://blog.dtpwiki.jp/MTOS/mt-tb.cgi/2441





![: Amazon.co.jp: プラスティック・メモリーズ 1【完全生産限定版】(イベントチケット優先販売申込券付) [Blu-ray]](/lists/_9/B00VWX66E8.jpg)
![: Amazon.co.jp: プラスティック・メモリーズ 2【完全生産限定版】[Blu-ray]](/lists/_9/B00VWX66K2.jpg)
![: Amazon.co.jp: プラスティック・メモリーズ 3【完全生産限定版】[Blu-ray]](/lists/_9/B00VWX6MV0.jpg)
![: Amazon.co.jp: プラスティック・メモリーズ 4【完全生産限定版】[Blu-ray]](/lists/_9/B00VWX66IO.jpg)
![: Amazon.co.jp: プラスティック・メモリーズ 5【完全生産限定版】[Blu-ray]](/lists/_9/B00VWX6Y0E.jpg)
![: Amazon.co.jp: プラスティック・メモリーズ 6【完全生産限定版】[Blu-ray]](/lists/_9/B00VWX69D6.jpg)


コメントする