| 1 |
package Plagger::Plugin::CustomFeed::FileList; |
|---|
| 2 |
use strict; |
|---|
| 3 |
use base qw( Plagger::Plugin ); |
|---|
| 4 |
|
|---|
| 5 |
use Encode; |
|---|
| 6 |
use Plagger::UserAgent; |
|---|
| 7 |
use Plagger::Date; |
|---|
| 8 |
use Digest::MD5 qw(md5_hex); |
|---|
| 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_utf8($res->content); |
|---|
| 40 |
|
|---|
| 41 |
my $feed = Plagger::Feed->new; |
|---|
| 42 |
$feed->title('filelist') unless $feed->title; |
|---|
| 43 |
$feed->link($url); |
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
my @files = split /\n/, $content; |
|---|
| 48 |
@files = map {s!/$!!; $_} |
|---|
| 49 |
grep {! m!\.?\./$!} |
|---|
| 50 |
grep {m!^[-d][-rwx]+!} @files; |
|---|
| 51 |
for my $file (@files) { |
|---|
| 52 |
my @infos = split /\s+/, $file; |
|---|
| 53 |
next if ($self->conf->{dir_only} && $infos[0] !~ m!^d!); |
|---|
| 54 |
|
|---|
| 55 |
my $title = $infos[9]; |
|---|
| 56 |
next if ($title =~ /^\.\.?$/); |
|---|
| 57 |
my $author = $infos[2]; |
|---|
| 58 |
my $date = join ' ', $infos[5], $infos[6], $infos[7], $infos[8]; |
|---|
| 59 |
|
|---|
| 60 |
$context->log(debug => $date); |
|---|
| 61 |
|
|---|
| 62 |
my $entry = Plagger::Entry->new; |
|---|
| 63 |
$entry->title($title); |
|---|
| 64 |
|
|---|
| 65 |
$entry->author($author); |
|---|
| 66 |
$entry->date(Plagger::Date->strptime('%m %d %T %Y', $date)); |
|---|
| 67 |
$entry->id(md5_hex(encode_utf8($file))); |
|---|
| 68 |
|
|---|
| 69 |
$feed->add_entry($entry); |
|---|
| 70 |
} |
|---|
| 71 |
|
|---|
| 72 |
$context->update->add($feed); |
|---|
| 73 |
|
|---|
| 74 |
return 1; |
|---|
| 75 |
} |
|---|
| 76 |
|
|---|
| 77 |
1; |
|---|
| 78 |
|
|---|