OSDN Git Service

FeedWorker Atom handling update to use Mubot4FB::Atom based on XML::Atom
authorISHIKAWA Mutsumi <ishikawa@hanzubon.jp>
Sat, 3 Nov 2012 04:56:16 +0000 (13:56 +0900)
committerISHIKAWA Mutsumi <ishikawa@hanzubon.jp>
Sat, 3 Nov 2012 04:56:16 +0000 (13:56 +0900)
lib/Mubot4FB/Atom.pm [new file with mode: 0644]
lib/Mubot4FB/FeedWorker.pm

diff --git a/lib/Mubot4FB/Atom.pm b/lib/Mubot4FB/Atom.pm
new file mode 100644 (file)
index 0000000..a801a45
--- /dev/null
@@ -0,0 +1,88 @@
+#!/usr/bin/perl
+#
+# Copyright (c) 2012 ISHIKAWA Mutsumi <ishikawa@hanzubon.jp>
+# This program is covered by the GNU General Public License 2
+#
+package Mubot4FB::Atom;
+use strict;
+use utf8;
+
+use XML::Atom::Feed;
+use XML::Atom::Entry;
+use XML::Atom::Link;
+use POSIX qw/strftime/;
+
+sub new {
+       my ($proto, $cfg) = @_;
+       my $class = ref $proto || $proto;
+       my $self = {cfg => $cfg, feed => XML::Atom::Feed->new( Version => '1.0' )};
+       bless $self, $class;
+
+       $self->init();
+       return $self;
+}
+
+sub init {
+       my ($me) = @_;
+
+       $me->{feed}->title($me->{cfg}->{atom}->{title} ? $me->{cfg}->{atom}->{title} : 'unknown');
+       $me->{feed}->id($me->{cfg}->{atom}->{id_base});
+       $me->{feed}->updated(strftime("%Y-%m-%dT%H:%M:%SZ", gmtime));
+       $me->{feed}->author($me->{cfg}->{atom}->{author} ? $me->{cfg}->{atom}->{author} : 'unknown');
+
+       my $html_link = XML::Atom::Link->new;
+       $html_link->type('text/html');
+       $html_link->rel('alternate');
+       $html_link->href($me->{cfg}->{fb_page_url});
+       $me->{feed}->add_link($html_link);
+
+       my $self_link = XML::Atom::Link->new;
+       $self_link->type('application/atom+xml');
+       $self_link->rel('self');
+       $self_link->href($me->{cfg}->{atom}->{url});
+       $me->{feed}->add_link($self_link);
+
+       my $hub_link = XML::Atom::Link->new;
+       $hub_link->rel('hub');
+       $hub_link->href($me->{cfg}->{hub});
+       $me->{feed}->add_link($hub_link);
+
+       return $me->{feed};
+}
+
+sub add_entry {
+       my ($me, $data) = @_;
+
+       my $entry = XML::Atom::Entry->new;
+       foreach my $i ('id', 'title', 'content', 'published', 'summary', 'updated', 'auther', 'issued') {
+               $entry->$i($data->{$i}) if (defined $data->{$i});
+       }
+       if (defined $data->{link}) {
+               my $links = ref $data->{link} eq 'ARRAY' ? $data->{link} : [$data->{link}];
+               foreach my $l (@$links) {
+                       my $link = XML::Atom::Link->new;
+                       $link->href($l);
+                       $entry->add_link($link);
+               }
+       }
+
+       $me->{feed}->add_entry($entry);
+       return $me->{feed};
+}
+
+sub as_xml {
+       my ($me) = @_;
+       return $me->{feed}->as_xml;
+}
+
+sub print {
+       my ($me, $fh) = @_;
+
+       if (defined $fh) {
+           print $fh $me->{feed}->as_xml;
+       } else {
+           print $me->{feed}->as_xml;
+       }
+}
+
+1;
index e9f3ee2..bf3d71e 100644 (file)
@@ -9,6 +9,8 @@ use utf8;
 
 use base 'Mubot4FB::Common';
 
+use Mubot4FB::Atom;
+use POSIX 'strftime';
 use Net::PubSubHubbub::Publisher;
 use JSON::XS;
 use Mubot4FB::DB;
@@ -22,6 +24,7 @@ use Data::Dumper;
 sub init {
        my ($me) = @_;
        $me->{db} = Mubot4FB::DB->new($me->{cfg});
+       $me->{pub} = defined $me->{cfg}->{hub} ? Net::PubSubHubbub::Publisher->new(hub => $me->{cfg}->{hub}) : undef;
        return 1;
 }
 
@@ -30,20 +33,36 @@ sub work {
 
        my $data = $me->{db}->bulkget_latest();
        my $tmpdir = defined $me->{cfg}->{tmpdir} ? $me->{cfg}->{tmpdir} : '/tmp';
-       warn Dumper($data) if defined $me->{cfg}->{debug};
-       if (defined $me->{cfg}->{atom_path}) {
-               if (defined $me->{cfg}->{hub}) {
-                       my $pub = Net::PubSubHubbub::Publisher->new(hub => $me->{cfg}->{hub});
-                       $pub->publish_update($me->{cfg}->{atom_url});
+
+       if (defined $me->{cfg}->{atom}->{path}) {
+               my ($fh, $tmp_file) = tempfile("tmpatomXXXXXX", DIR => $tmpdir);
+
+               my $feed = Mubot4FB::Atom->new($me->{cfg});
+
+               for my $e (@$data) {
+                       $feed->add_entry({
+                               title => $e->{submitter}.'曰く、'.$e->{prefix}.' '.$e->{comment},
+                               link => $me->post_uri($e->{fb_post_id}),
+                               id => $me->{cfg}->{atom}->{id_base}.'.'.$e->{fb_post_id},
+                               author => $e->{submitter},
+                               published => strftime("%Y-%m-%dT%H:%M:%SZ", gmtime($e->{post_time})),
+                               updated => strftime("%Y-%m-%dT%H:%M:%SZ", gmtime($e->{post_time})),
+                                        }
+                           );
                }
+               $feed->print($fh);
+               close($fh);
+               move $tmp_file, $me->{cfg}->{atom}->{path} || warn 'can not rename from ' . $tmp_file . ' to ' . $me->{cfg}->{atom}->{path};
+               $me->{pub}->publish_update($me->{cfg}->{atom}->{url}) if (defined $me->{pub});
        }
 
-       if (defined $me->{cfg}->{json_path}) {
+       if (defined $me->{cfg}->{json}->{path}) {
                my $out = {data_version => DATA_VERSION, timestamp => time, data => $data};
                my ($fh, $tmp_file) = tempfile("tmpjsonXXXXXX", DIR => $tmpdir);
+               warn $tmp_file if defined $me->{cfg}->{debug};
                print $fh encode_json($out);
                close($fh);
-               move $tmp_file, $me->{cfg}->{json_path} || warn 'can not rename from ' . $tmp_file . ' to ' . $me->{cfg}->{json_path};
+               move $tmp_file, $me->{cfg}->{json}->{path} || warn 'can not rename from ' . $tmp_file . ' to ' . $me->{cfg}->{json}->{path};
        }
 }