--- /dev/null
+#!/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;
use base 'Mubot4FB::Common';
+use Mubot4FB::Atom;
+use POSIX 'strftime';
use Net::PubSubHubbub::Publisher;
use JSON::XS;
use Mubot4FB::DB;
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;
}
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};
}
}