< Back
Page 4 of 4
Listings
Listing 16-3 fetch script
01: #!/usr/bin/perl -w 02: # fetch 03: use strict; 04: use File::Basename; 05: use DBI; 06: use LWP::Simple qw(mirror); 07: my $RDF_DIR = './rdf'; 08: my $dbh = DBI->connect("dbi:mysql:book", "user", "password"); 09: my $sth = $dbh->prepare(qq{select URL from rdf}); 10: $sth->execute or die $DBI::errstr; 11: while (my $url = $sth->fetchrow) { 12: my $name = basename($url); 13: mirror($url, "$RDF_DIR/$name"); 14: } 15: $dbh->disconnect;
Listing 16-4 index.cgi script
01: #!/usr/bin/perl -wT 02: # index.cgi 03: use strict; 04: use CGI qw(:standard end_ul end_table); 05: use CGI::Carp qw(fatalsToBrowser); 06: use File::Basename; 07: use DBI; 08: use XML::RSS; 09: my $RDF_DIR = './rdf'; 10: my $dbh = DBI->connect("dbi:mysql:book", 'user','password') or print $DBI::errstr; 11: my $sth = $dbh->prepare(qq{select URL from rdf where Selected = 1}); 12: $sth->execute; 13: print header, 14: start_html("My Home Page"), 15: h2("My Favorite Sites"); 16: print start_table({cellpadding=>0, cellspacing=>0, border=> 0, width => '100%'}), 17: td; 18: my $count = 1; 19: my @html = ('</TD><TD>', '</TD><TR><TD>'); 20: while (my $url = basename($sth->fetchrow)) { 21: my $rss = new XML::RSS; 22: eval {$rss->parsefile("$RDF_DIR/$url")}; 23: warn "$url will not parse $@" and next if $@; 24: my $last_mod = scalar localtime((stat("$RDF_DIR/$url"))[9]); 25: print start_table({cellpadding=>0, cellspacing=>2, border=> 5, width=>'75%'}), 26: td({valign=>'CENTER', bgcolor => '#C0C0C0'}); 27: $rss->{image}{url} 28: ? print img({src=>$rss->{image}{url}}) 29: : print strong($rss->{channel}{title}); 30: print ul; 31: for (@{$rss->{items}}) { 32: print li(a({href=>$_->{link}}, $_->{title})); 33: } 34: print end_ul; 35: if ($rss->{textinput}{link}) { 36: print $rss->{textinput}{description}, start_form(-method => 'GET', 37: -action => $rss->{textinput}{link}), 38: textfield(-name => $rss->{textinput}{name}), 39: end_form; 40: } 41: print qq(Last Updated $last_mod<BR>), 42: end_table; 43: print $html[$count^=1]; 44: } 45: print end_table, 46: end_html;
Listing 16-5 admin.cgi script
01: #!/usr/bin/perl -w 02: # admin.cgi 03: use strict; 04: use CGI qw(:standard); 05: use CGI::Carp qw(fatalsToBrowser); 06: use DBI; 07: my $dbh = DBI->connect("dbi:mysql:book", "user", "password"); 08: param('Submit') ? add_new() : show_form(); 09: $dbh->disconnect; 10: sub show_form { 11: my $sth = $dbh->prepare(qq{select * from rdf}); 12: $sth->execute; 13: print header, 14: start_html("My Home Page Options"), 15: h2("Choose My Favorite Sites"); 16: print start_form(-method => 'POST', -action => 'admin.cgi'); 17: while (my $data = $sth->fetchrow_hashref) { 18: my $checked = $data->{Selected} ? "CHECKED" : ""; 19: print checkbox(-name => 'Selected', 20: -checked => $checked, 21: -value => $data->{Name}, 22: -label => $data->{Name}, 23: ), 24: p; 25: } 26: print h2("Add new channel") , p, 27: "RDFs URL: ", textfield(-name => 'URL', -size => 50), p 28: "Name of channel: ", textfield(-name => 'Name', - size => 50), p 29: "Display on Home Page: ", 30: checkbox(-name => 'new-Selected', -label => ''), p, 31: submit(-name => 'Submit', -value => 'Make Changes'), 32: end_form, end_html; 33: } 34: sub add_new { 35: my $qry_select = qq(update rdf set Selected = 1 where ); 36: my $qry_deselect = qq(update rdf set Selected = 0 where ); 37: my @selected = param('Selected'); 38: $qry_select .= qq(Name = '$_' or ) for @selected; 39: $qry_deselect .= qq(Name <> '$_' and ) for @selected; 40: $qry_deselect =~ s! and $!!; 41: $qry_select =~ s! or $!!; 42: my $sth = $dbh->prepare($qry_select); 43: $sth->execute or print $DBI::errstr; 44: $sth = $dbh->prepare($qry_deselect); 45: $sth->execute or print $DBI::errstr; 46: if (param('URL') && param('Name')) { 47: my $url = param('URL'); 48: my $name = param('Name'); 49: my $display = param('new-Selected') ? 1 : 0; 50: $sth = $dbh->prepare(qq{insert into rdf (URL, NAME, SELECTED) values ('$url', '$name', $display)}); 51: $sth->execute or print $DBI::errstr; 52: } 53: show_form; 54: }
Listing 16-6 make_rss script
01: #!/usr/bin/perl -wT 02: # make_rss 03: use strict; 04: use XML::RSS; 05: my $FILE = 'news.txt'; 06: my $RDF_DIR = './rdf'; 07: my $rdf = new XML::RSS; 08: $rdf->channel(title => 'My News', 09: link => 'http://news.me.com', 10: language => 'en', 11: description => 'My news, for you!', 12: copyright => 'Copyright 2000++, Me', 13: pubDate => scalar localtime(time), 14: lastBuildDate => scalar localtime(time), 15: managingEditor => 'me@me.com', 16: webMaster => 'me@me.com' 17: ); 18: $rdf->image(title => 'My News', 19: url => 'http://news.me.com/my_news.gif', 20: link => 'http://news.me.com', 21: height => 30, 22: width => 119 23: ); 24: open(FILE, $FILE) || die "Can't open $FILE ($!)"; 25: while (<FILE>) { 26: my ($url, $desc) = split /\|/; 27: $rdf->add_item(title => $desc, 28: link => $url 29: ); 30: } 31: $rdf->textinput(title => 'Search My News', 32: description => 'Search the Archives', 33: name => 'text', 34: link => 'http://news.me.com/search.cgi' 35: ); 36: $rdf->save("$RDF_DIR/my_news.rdf");
< Back
Page 4 of 4