root/bloglines2email/trunk/bloglines2email

Revision 1649 (checked in by miyagawa, 15 years ago)

Added 'Post to del.icio.us' link (Thanks to sekimura-san)
It uses raw utf-8 HTML bytes which is invalid, but it works with Gmail

  • Property svn:executable set to *
Line 
1 #!/usr/local/bin/perl -w
2 use strict;
3 use DateTime;
4 use DateTime::Format::Mail;
5 use Encode;
6 use FindBin;
7 use File::Spec;
8 use Getopt::Long;
9 use MIME::Lite;
10 use Template;
11 use WebService::Bloglines;
12 use YAML;
13
14 our $VERSION = '0.01';
15
16 GetOptions(\our %opt, "test", "verbose", "conf=s");
17
18 my $conf = $opt{conf} || File::Spec->catfile($FindBin::Bin, "bloglines2email.conf");
19 my $cfg  = YAML::LoadFile($conf);
20
21 my $bws = WebService::Bloglines->new(
22     username => $cfg->{username},
23     password => $cfg->{password},
24 );
25
26 my $mark_read = $opt{test} ? 0 : 1;
27 my @updates = $bws->getitems(0, $mark_read);
28 debug(scalar(@updates) . " feeds updated.");
29 for my $update (@updates) {
30     send_email($cfg, $update);
31 }
32
33 sub debug {
34     my $msg = "@_";
35     chomp($msg);
36     print STDERR encode('utf-8', "$msg\n") if $opt{verbose};
37 }
38
39 sub send_email {
40     my($cfg, $update) = @_;
41     my $feed  = $update->feed;
42     my @items = $update->items;
43     if ($cfg->{'group-items'}) {
44         send_email_feed($cfg, $feed, \@items);
45     }
46     else {
47         for my $item (@items) {
48             send_email_item($cfg, $feed, $item);
49         }
50     }
51 }
52
53 sub send_email_feed {
54     my($cfg, $feed, $items) = @_;
55     my $subject = $feed->{title} || '(no-title)';
56     my $body = join '<hr />', map format_body($feed, $_, $cfg), @$items;
57     do_send_mail($cfg, $feed, $subject, $body);
58 }
59
60 sub send_email_item {
61     my($cfg, $feed, $item) = @_;
62     my $subject = $item->{title} || '(no-title)';
63     my $body    = format_body($feed, $item, $cfg);
64     do_send_email($cfg, $feed, $subject, $body);
65 }
66
67 sub do_send_mail {
68     my($cfg, $feed, $subject, $body) = @_;
69     debug("Sending $subject to $cfg->{mailto}");
70     my $feed_title = $feed->{title};
71        $feed_title =~ tr/,//d;
72     my $msg = MIME::Lite->new(
73         Date => get_rfc2822_date($cfg),
74         From => encode('MIME-Header', qq("$feed_title" <$cfg->{mailfrom}>)),
75         To   => $cfg->{mailto},
76         Subject => encode('MIME-Header', $subject),
77         Type => 'multipart/related',
78     );
79     $msg->attach(
80         Type => 'text/html; charset=utf-8',
81         Data => encode("utf-8", $body),
82     );
83     $msg->send("smtp", "localhost", Timeout => 60)
84 }
85
86 sub get_rfc2822_date {
87     my $cfg = shift;
88     my $dt  = @_
89         ? DateTime::Format::Mail->parse_datetime($_[0])
90         : DateTime->now;
91     my $tz = $cfg->{'date-timezone'} || 'local';
92     $dt->set_time_zone($tz);
93     DateTime::Format::Mail->format_datetime($dt);
94 }
95
96 sub format_body {
97     my($feed, $item, $cfg) = @_;
98     my $template = get_template();
99     my $tt = Template->new;
100     $tt->process(\$template, {
101         feed => $feed,
102         item => $item,
103         cfg  => $cfg,
104         get_rfc2822_date => sub { get_rfc2822_date($cfg, @_) },
105     }, \my $out);
106     $out;
107 }
108
109 sub get_template {
110     return <<'HTML';
111 <div>
112 <div>
113 [% IF feed.image %]<a href="[% feed.image.link %]"><img style="border:0" align="right" src="[% feed.image.url | html %]" alt="[% feed.image.title | html %]" /></a>[% END %]
114 [% var = 'group-items'; IF cfg.$var %]<strong style="font-weight:bold;font-size:1.2em">[% item.title %]</strong><br />[% END %]
115 [% SET link = item.link || item.guid -%]
116 Link: <a href="[% link | html %]">[% link | html %]</a><br />
117 [% IF item.dc.creator %]by [% item.dc.creator | html %][% END %][% IF item.dc.subject %] on [% item.dc.subject %][% END %]</div>
118 [% IF item.description -%]
119 [% IF item.description.match('(?i)^<p[ >]') %][% item.description %][% ELSE %]<div style="padding: 1em 0">[% item.description %]</div>[% END %]
120 [% ELSE %]<br />[% END %]
121 <div style="font-size:0.8em">[% IF item.pubDate %]Posted on [% get_rfc2822_date(item.pubDate) %][% END %] | <a href="[% link | html %]">permalink</a> | <a href="[% feed.link | html %]">[% feed.title | html %]</a>[% var = 'delicious-username'; IF cfg.$var %] | <a href="http://del.icio.us/[% cfg.$var %]?v=3&amp;url=[% item.link | html %]&amp;title=[% item.title | html %]">Post to del.icio.us</a>[% END %]<br clear="all" /></div>
122 </div>
123 HTML
124 }
125
126 =head1 NAME
127
128 bloglines2email - Send Bloglines unread items as HTML mail
129
130 =head1 AUTHOR
131
132 Tatsuhiko Miyagawa E<lt>miyagawa@bulknews.netE<gt>
133
134 This script is free software and licensed under the same terms as Perl
135 (Artistic/GPL).
136
137 =cut
Note: See TracBrowser for help on using the browser.