#!/usr/bin/perl use strict; use LWP::Simple; use XML::RSS; use DBI; use CGI; print "Content-type: text/html; charset=utf-8\n\n"; # database open my $dbh = &db_open(); sub db_open() { my $d = 'DBI:mysql:db0feedmix;host=localhost'; my $u = 'feedmix'; my $p = 'b499fd792d3cc0c67c53'; my $dbh = DBI->connect($d, $u, $p); return $dbh; } # set utf-8 &db_set_char($dbh, 'UTF8'); sub db_set_char { my $dbh = shift; my $char = shift; my $sth = $dbh->prepare("SET CHARACTER SET $char"); $sth->execute; $sth->finish; } print <<__HTML;
RSSフィード
__HTML my $q = new CGI; if ($q->param('mode') eq 'regist') { my $sql = "select id from feed where feed_url = ?"; my $sth = $dbh->prepare($sql); $sth->execute($q->param('feed_url')); my $num = $sth->fetchrow_array; $sth->finish; if ($num > 0) { print 'そのフィードは既に登録済みです。'; } else { Rist_feed($q->param('feed_url')); print 'フィードの登録が完了しました。'; } } my $sql = "select * from feed"; my $ary_ref = $dbh->selectall_arrayref($sql); foreach my $row (@{$ary_ref}) { print '
  • '.$row->[2].'
  • '; } sub regist_feed { # URL of RSS feed my $feed_url = shift; # get the rss feed my $input = get($feed_url); my $rss = new XML::RSS; $rss->parse($input); # feed title my $feedtitle = $rss->channel->{'title'}; my $feed_link = $rss->channel->{'link'}; # datetime my ($sec, $min, $hour, $mday, $mon, $year) = localtime(time); $year += 1900; $mon++; $sec = ($sec < 10) ? '0'.$sec : $sec; $min = ($min < 10) ? '0'.$min : $min; $hour = ($hour < 10) ? '0'.$hour : $hour; $mday = ($mday < 10) ? '0'.$mday : $mday; $mon = ($mon < 10) ? '0'.$mon : $mon; my $datetime = "$year-$mon-$mday $hour:$min:$sec"; # feed insert my $sql = "insert into feed (feed_url, feed_title, feed_link, create_time, update_time) values (?, ?, ?, ?, ?)"; my $sth = $dbh->prepare($sql); $sth->execute($feed_url, $feedtitle, $feed_link, $datetime, $datetime); $sth->finish; } print <<__HTML; __HTML &db_close($dbh); sub db_close() { my $dbh = shift; $dbh->disconnect(); }