| 1 |
#!/usr/bin/perl |
|---|
| 2 |
use strict; |
|---|
| 3 |
use warnings; |
|---|
| 4 |
use Encode; |
|---|
| 5 |
use FindBin; |
|---|
| 6 |
use Getopt::Long; |
|---|
| 7 |
use List::Util qw(first); |
|---|
| 8 |
|
|---|
| 9 |
use lib File::Spec->catdir($FindBin::Bin, '..', 'lib'); |
|---|
| 10 |
use Plagger; |
|---|
| 11 |
use XML::Atom::Stream; |
|---|
| 12 |
|
|---|
| 13 |
my $path = "$FindBin::Bin/../config.yaml"; |
|---|
| 14 |
GetOptions("--config=s", \$path); |
|---|
| 15 |
Getopt::Long::Configure("bundling"); # allows -c |
|---|
| 16 |
|
|---|
| 17 |
my $context = Plagger->new(config => $path); |
|---|
| 18 |
$context->run_hook('plugin.init'); |
|---|
| 19 |
|
|---|
| 20 |
$XML::Atom::ForceUnicode = 1; |
|---|
| 21 |
|
|---|
| 22 |
my $url = shift || "http://updates.sixapart.com/atom-stream.xml"; |
|---|
| 23 |
|
|---|
| 24 |
my $client = XML::Atom::Stream->new( |
|---|
| 25 |
callback => \&callback, |
|---|
| 26 |
reconnect => 1, |
|---|
| 27 |
debug => 1, |
|---|
| 28 |
); |
|---|
| 29 |
$client->connect($url); |
|---|
| 30 |
|
|---|
| 31 |
$context->run_hook('plugin.finalize'); |
|---|
| 32 |
|
|---|
| 33 |
sub callback { |
|---|
| 34 |
my $atom = shift; |
|---|
| 35 |
|
|---|
| 36 |
# TODO: make convenience method to convert XML::Atom::Feed to Plagger::Feed |
|---|
| 37 |
my $feed = Plagger::Feed->new; |
|---|
| 38 |
$feed->title($atom->title); |
|---|
| 39 |
|
|---|
| 40 |
my $link = first { !defined $_->rel || $_->rel eq 'alternate' } $atom->link; |
|---|
| 41 |
$feed->link($link->href) if $link; |
|---|
| 42 |
$feed->description($atom->tagline); |
|---|
| 43 |
$feed->language($atom->language); |
|---|
| 44 |
$feed->author($atom->author->name) if $atom->author; |
|---|
| 45 |
$feed->updated($atom->modified); |
|---|
| 46 |
$feed->source_xml($atom->as_xml); |
|---|
| 47 |
$feed->id($atom->id); |
|---|
| 48 |
|
|---|
| 49 |
for my $e ($atom->entries) { |
|---|
| 50 |
my $entry = Plagger::Entry->new; |
|---|
| 51 |
$entry->title($e->title); |
|---|
| 52 |
$entry->author($e->author->name) if $e->author; |
|---|
| 53 |
for my $cat ($e->categories) { |
|---|
| 54 |
$entry->add_tag($cat->label || $cat->term); |
|---|
| 55 |
} |
|---|
| 56 |
$entry->date($e->published || $e->updated); |
|---|
| 57 |
|
|---|
| 58 |
my $link = first { !defined $_->rel || $_->rel eq 'alternate' } $e->link; |
|---|
| 59 |
$entry->link($link->href) if $link; |
|---|
| 60 |
$entry->feed_link($feed->link); |
|---|
| 61 |
$entry->id($e->id); |
|---|
| 62 |
|
|---|
| 63 |
my $body = $e->content ? $e->content->body : |
|---|
| 64 |
$e->summary ? $e->summary->body : undef; |
|---|
| 65 |
$entry->body($body) if $body; |
|---|
| 66 |
|
|---|
| 67 |
$feed->add_entry($entry); |
|---|
| 68 |
} |
|---|
| 69 |
|
|---|
| 70 |
$context->update->add($feed); |
|---|
| 71 |
$context->do_run_with_feeds; |
|---|
| 72 |
|
|---|
| 73 |
$context->clear_session; |
|---|
| 74 |
} |
|---|