| 1 |
package Plagger::Plugin::CustomFeed::JoyHack; |
|---|
| 2 |
use strict; |
|---|
| 3 |
use base qw( Plagger::Plugin ); |
|---|
| 4 |
|
|---|
| 5 |
use Encode; |
|---|
| 6 |
use Plagger::UserAgent; |
|---|
| 7 |
use Plagger::Util qw( decode_content extract_title ); |
|---|
| 8 |
use Plagger::Date; |
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
sub register { |
|---|
| 12 |
my($self, $context) = @_; |
|---|
| 13 |
$context->register_hook( |
|---|
| 14 |
$self, |
|---|
| 15 |
'customfeed.handle' => \&handle, |
|---|
| 16 |
); |
|---|
| 17 |
} |
|---|
| 18 |
|
|---|
| 19 |
sub handle { |
|---|
| 20 |
my($self, $context, $args) = @_; |
|---|
| 21 |
|
|---|
| 22 |
return $self->aggregate($context, $args); |
|---|
| 23 |
} |
|---|
| 24 |
|
|---|
| 25 |
sub aggregate { |
|---|
| 26 |
my($self, $context, $args) = @_; |
|---|
| 27 |
|
|---|
| 28 |
my $url = $args->{feed}->url; |
|---|
| 29 |
$context->log(info => "GET $url"); |
|---|
| 30 |
|
|---|
| 31 |
my $agent = Plagger::UserAgent->new; |
|---|
| 32 |
my $res = $agent->fetch($url, $self); |
|---|
| 33 |
|
|---|
| 34 |
if ($res->http_response->is_error) { |
|---|
| 35 |
$context->log(error => "GET $url failed: " . $res->status_line); |
|---|
| 36 |
return; |
|---|
| 37 |
} |
|---|
| 38 |
|
|---|
| 39 |
my $content = decode_content($res); |
|---|
| 40 |
|
|---|
| 41 |
my $feed = Plagger::Feed->new; |
|---|
| 42 |
$feed->title('JoyHack'); |
|---|
| 43 |
$feed->link($url); |
|---|
| 44 |
my $host = $self->conf->{host} || 'localhost'; |
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
my @lines = split /\n/, $content; |
|---|
| 48 |
for my $line (@lines) { |
|---|
| 49 |
my ($title, $singer, $works, $author, $date ,$id) = |
|---|
| 50 |
$line =~ /([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)/; |
|---|
| 51 |
|
|---|
| 52 |
my $link = "http://$host/cgi-bin/hack.cgi?m=1&p=$id"; |
|---|
| 53 |
my $summary = join ' ', ($singer, $works); |
|---|
| 54 |
|
|---|
| 55 |
my $entry = Plagger::Entry->new; |
|---|
| 56 |
$entry->title($title); |
|---|
| 57 |
$entry->link($link); |
|---|
| 58 |
$entry->author($author); |
|---|
| 59 |
$entry->date(Plagger::Date->strptime('%y/%m/%d', $date)); |
|---|
| 60 |
$entry->id($id); |
|---|
| 61 |
$entry->body($summary); |
|---|
| 62 |
push @{$entry->tags}, $_ for ($singer, $works, $author); |
|---|
| 63 |
|
|---|
| 64 |
$feed->add_entry($entry); |
|---|
| 65 |
$context->log(debug => "$date $title ($link)"); |
|---|
| 66 |
} |
|---|
| 67 |
|
|---|
| 68 |
$context->update->add($feed); |
|---|
| 69 |
|
|---|
| 70 |
return 1; |
|---|
| 71 |
} |
|---|
| 72 |
|
|---|
| 73 |
1; |
|---|
| 74 |
|
|---|