root/plagger/trunk/my-plugins/CustomFeed-FTP/FTP.pm

Revision 1 (checked in by yohei, 2 years ago)

Add my Plagger plugins

Line 
1 package Plagger::Plugin::CustomFeed::FTP;
2 use strict;
3 use base qw( Plagger::Plugin );
4
5 use Plagger::Date;
6 use Net::FTP;
7
8 sub register {
9     my($self, $context) = @_;
10     $context->register_hook(
11         $self,
12         'subscription.load' => \&load,
13     ); 
14 }   
15
16 sub load {
17     my($self, $context) = @_;
18    
19     my $feed = Plagger::Feed->new;
20        $feed->aggregator(sub { $self->aggregate(@_) });
21     $context->subscription->add($feed);
22 }
23
24 sub aggregate {
25     my($self, $context, $args) = @_;
26
27     my $host = $self->conf->{host};
28     my $ftp = Net::FTP->new($host, Timeout => 300);
29
30     unless ($ftp->login($self->conf->{username}, $self->conf->{password})) {
31         $context->log(error => "Login to $host failed.");
32         return;
33     }
34
35     my $root = $self->conf->{root} || '.';
36     my $file_infos = [];
37
38     $ftp->cwd($root);
39     $self->load_file_info($context, $file_infos, $ftp, '');
40    
41     $ftp->quit;
42
43     my $base_uri = "ftp://" . $self->conf->{username} . '@' ."$host/$root";
44
45     my $feed = Plagger::Feed->new;
46     $feed->title("$host/$root");
47     $feed->link($base_uri);
48
49     for my $file_info (@$file_infos) {
50         next if ($self->conf->{dir_only} && !$file_info->{is_dir});
51
52         my $entry = Plagger::Entry->new;
53
54         $entry->title($file_info->{name});
55         $entry->id($file_info->{path});
56         $entry->link($base_uri . $file_info->{path});
57         $entry->date( Plagger::Date->from_epoch($file_info->{mdtm}));
58         $entry->body($file_info->{path});
59
60         $feed->add_entry($entry);
61     }
62
63     $context->update->add($feed);
64 }
65
66 sub load_file_info {
67     my($self, $context, $list, $ftp, $dir) = @_;
68
69     $context->log(debug => "Listing files under $dir");
70     my @files = $ftp->ls();
71     unless (@files) {
72         $context->log(error => "Can't list $dir .");
73         return 0;
74     }
75
76     my $dir_mdtm = 0;
77     for my $file (@files) {
78         $context->log(debug => "Processing $file.");
79         next if ($self->conf->{skip_hidden} && $file =~ /^\./);
80         my $file_info = {
81             name   => $file,
82             path   => File::Spec->catfile($dir, $file),
83             mdtm   => 0,
84             id_dir => 0,
85         };
86         if ($ftp->cwd($file)) {
87             $file_info->{is_dir} = 1;
88             $file_info->{path}  .= '/';
89             my $next_dir = File::Spec->catdir($dir, $file);
90             $file_info->{mdtm} = $self->load_file_info($context, $list, $ftp, $next_dir);
91             $ftp->cdup();
92         } else {
93             $file_info->{mdtm} = $ftp->mdtm($file) || 0;
94             $dir_mdtm = $file_info->{mdtm} if $file_info->{mdtm} > $dir_mdtm;
95         }
96
97         push @$list, $file_info;
98     }
99     return $dir_mdtm;
100 }
101
102 1;
103
Note: See TracBrowser for help on using the browser.