[cnuth feed Olivier Schwander **20070827102451 Intégration dans cnuth Modularisation ] { move ./feed2mb.pl ./cnuth-feed.pl hunk ./XML/Feed/MB.pm 1 -Package XML:Feed:MB +package XML::Feed::MB; hunk ./XML/Feed/MB.pm 6 -require Encode; +use Encode; hunk ./XML/Feed/MB.pm 29 + shift; hunk ./XML/Feed/MB.pm 32 + my $dohtml = shift; + my $delete = shift; + my $verbose = shift; + hunk ./XML/Feed/MB.pm 39 + my $count = 0; + hunk ./XML/Feed/MB.pm 46 +# building uri + if ($url =~ m!^http://!) { + $url = URI->new($url); + } + hunk ./XML/Feed/MB.pm 52 - my $feed = XML::Feed->parse(URI->new($url)) + my $feed = XML::Feed->parse($url) hunk ./XML/Feed/MB.pm 55 - print "Feed: ", $feed->title, "\n"; + print "Feed: ", $feed->title, "\n" if $verbose; hunk ./XML/Feed/MB.pm 66 - print "fetching ", $entry->title, "..."; + print "fetching «", $entry->title, "» \033[s..." if $verbose; hunk ./XML/Feed/MB.pm 81 - $html_body = Encode::decode_utf8($html_body); +# $html_body = Encode::decode_utf8($html_body); hunk ./XML/Feed/MB.pm 83 + hunk ./XML/Feed/MB.pm 94 - $text_body =~ s/^(\n)*//s; # deletion of the empty lines on the beginning + $text_body =~ s/^(\n)*//s; # empty lines deletion at the beginning hunk ./XML/Feed/MB.pm 107 - 'From' => $from, - 'To' => $to, - 'Subject' => encode_mimewords(decode_entities($entry->title)), - 'Date' => format_date($entry->issued->epoch), - 'X-Id' => $entry->id, + 'From' => $from, + 'To' => $to, +# 'Subject' => encode_mimewords(decode_entities($entry->title)), + 'Subject' => encode_mimewords($entry->title), + 'Date' => format_date($entry->issued->epoch), + 'X-Id' => $entry->id, hunk ./XML/Feed/MB.pm 118 - embed => 1, + embed => $dohtml, hunk ./XML/Feed/MB.pm 126 - print "done\n"; + $count++; + print " \033[u \n" if $verbose; hunk ./XML/Feed/MB.pm 129 + + return $count; hunk ./cnuth-feed.pl 1 -#! /usr/bin/perl +#! /usr/bin/perl -w hunk ./cnuth-feed.pl 3 -use utf8; - -require Encode; - -use XML::Feed; - -use Email::LocalDelivery; -use Email::Simple; -use Email::Simple::Creator; -use Email::MIME::CreateHTML; -use Email::Address; -use Email::Date; -use Email::Folder; - -use MIME::EncWords "encode_mimewords"; - -use HTML::Encoding "encoding_from_xml_declaration"; -use HTML::Entities; -use HTML::TreeBuilder; -use HTML::FormatText; -my $formatter = HTML::FormatText->new(leftmargin => 0, rightmargin => 72); - -binmode STDOUT, ":utf8"; hunk ./cnuth-feed.pl 4 +use Getopt::Long; +my $delete = 0; +my $html = 0; +my $verbose = 0; +my $configfile = "~/.feed2mbrc"; +GetOptions("delete|d!" => \$delete, + "html|h!" => \$html, + "verbose|v" => \$verbose, + "config|c=s" => \$configfile, + ); hunk ./cnuth-feed.pl 15 -my $mb = shift; -#my $url = 'http://feeds.feedburner.com/friggeri'; -#my $mb = "./rss"; - -# searching already downloaded entries -my $folder = Email::Folder->new($mb); -my %ids; -map { $ids{$_->header("X-Id")}++ } $folder->messages; - -# feed parsing -my $feed = XML::Feed->parse(URI->new($url)) - or die XML::Feed->errstr; - -print "Feed: ", $feed->title, "\n"; - -my $enc = encoding_from_xml_declaration($feed->as_xml); -if ($enc eq "") { - $enc = "ISO-8859-15"; -} - -# fetching each entries -for my $entry ($feed->entries) { - next if (defined($ids{$entry->id}) && $ids{$entry->id} > 0); - -print "fetching ", $entry->title, "..."; - -# author - my @addresses = Email::Address->parse($entry->author); - if (@addresses == 0) { - @addresses = (Email::Address->new($entry->author => - 'invalid@localhost')); - } - my $from = "@addresses"; - - my $to = Email::Address->new($feed->title => 'invalid@localhost'); - -# html body - my $html_body = $entry->content->body; - if ($enc eq "UTF-8") { - $html_body = Encode::decode_utf8($html_body); - } - my $html_sig = "
\n--
\n" . - "link . "\">" . - $entry->link . "" . "
\n" . - $feed->title . "
\n" . - $feed->description . "
\n"; - my $html = $html_body . $html_sig; - -# text body - my $text_tree = HTML::TreeBuilder->new_from_content($html_body); - my $text_body = $formatter->format($text_tree); - $text_body =~ s/^(\n)*//s; # deletion of the empty lines on the beginning - my $text_sig = "\n-- \n" . - $entry->link . "\n" . - $feed->title . "\n" . - $feed->description . "\n"; - my $text = $text_body . $text_sig; +my $mb = shift; hunk ./cnuth-feed.pl 17 -# title - my $title = $entry->title; +use Config::INI::Simple; +my $conf = new Config::INI::Simple; +$conf->read($configfile); hunk ./cnuth-feed.pl 21 -# building of the message - my $email = Email::MIME->create_html( - header => [ - 'From' => $from, - 'To' => $to, - 'Subject' => encode_mimewords(decode_entities($entry->title)), - 'Date' => format_date($entry->issued->epoch), - 'X-Id' => $entry->id, - ], - body => $html, - body_attributes => {charset => $enc}, - text_body => $text, - text_body_attributes => {charset => $enc}, - embed => 1, - base => $feed->link, - ); +use XML::Feed::MB; hunk ./cnuth-feed.pl 23 -# sending of the message - my @delivered_to = Email::LocalDelivery->deliver($email->as_string, - ($mb)); +print $html, $delete, $verbose, "\n"; +print $configfile, "\n"; hunk ./cnuth-feed.pl 26 -print "done\n"; +if (defined($url) && defined($mb)) { + my $count = XML::Feed::MB->fetch($url, $mb, $html, $delete, $verbose); + print $count, "\n"; + exit 0; }