root/trunk/plagger/lib/Plagger/Plugin/Filter/FetchEnclosure.pm

Revision 1400 (checked in by miyagawa, 14 years ago)

Notify::Audio: Added a dirty hack to get seconds. Will make it a separate plugin later.

Line 
1 package Plagger::Plugin::Filter::FetchEnclosure;
2 use strict;
3 use base qw( Plagger::Plugin );
4
5 use Cwd;
6 use File::Spec;
7 use File::Path;
8
9 sub register {
10     my($self, $context) = @_;
11     $context->register_hook(
12         $self,
13         'update.entry.fixup' => \&filter,
14     );
15 }
16
17 sub init {
18     my $self = shift;
19     $self->SUPER::init(@_);
20
21     defined $self->conf->{dir} or Plagger->context->error("config 'dir' is not set.");
22     # XXX make it Plagger::Util function
23     if ($self->conf->{dir} =~ /^[a-zA-Z]/ && $self->conf->{dir} !~ /:/) {
24         $self->conf->{dir} = File::Spec->catfile( Cwd::cwd, $self->conf->{dir} );
25     }
26    
27     unless (-e $self->conf->{dir} && -d _) {
28         Plagger->context->log(warn => $self->conf->{dir} . " does not exist. Creating");
29         mkpath $self->conf->{dir};
30     }
31 }
32
33 sub filter {
34     my($self, $context, $args) = @_;
35
36     my $ua = Plagger::UserAgent->new;
37     for my $enclosure ($args->{entry}->enclosures) {
38         my $feed_dir = File::Spec->catfile($self->conf->{dir}, $args->{feed}->id_safe);
39         unless (-e $feed_dir && -d _) {
40             $context->log(info => "mkdir $feed_dir");
41             mkdir $feed_dir, 0777;
42         }
43
44         my $filename = $enclosure->filename;
45         Encode::_utf8_off($filename);
46         my $path = File::Spec->catfile($feed_dir, $filename);
47         $context->log(info => "fetch " . $enclosure->url . " to " . $path);
48
49         my $request = HTTP::Request->new(GET => $enclosure->url);
50         if ($self->conf->{fake_referer}) {
51             $context->log(debug => "Sending Referer: " . $args->{entry}->permalink);
52             $request->header('Referer' => $args->{entry}->permalink);
53         }
54
55         my $res = $ua->mirror($request, $path);
56         $enclosure->local_path($path); # set to be used in later plugins
57
58         # Fix length if it's broken
59         if ($res->header('Content-Length')) {
60             $enclosure->length( $res->header('Content-Length') );
61         }
62     }
63 }
64
65 1;
66
67 __END__
68
69 =head1 NAME
70
71 Plagger::Plugin::Filter::FetchEnclosure - Fetch enclosure(s) in entry
72
73 =head1 SYNOPSIS
74
75   - module: Filter::FetchEnclosure
76     config:
77       dir: /path/to/files
78
79 =head1 DESCRIPTION
80
81 This plugin downloads enclosure files set for each entry.
82
83 =head1 CONFIG
84
85 =over 4
86
87 =item dir
88
89 Directory to store downloaded enclosures. Required.
90
91 =item fake_referer
92
93 Flag to set I<Referer> HTTP header when downloading enclosures. Some
94 sites would deny downloading images without Referer header. Defaults
95 to 0.
96
97 =back
98
99 =head1 TODO
100
101 =over 4
102
103 =item Support asynchronous download using POE
104
105 =back
106
107 =head1 AUTHOR
108
109 Tatsuhiko Miyagawa
110
111 =head1 SEE ALSO
112
113 L<Plagger>
114
115 =cut
116
Note: See TracBrowser for help on using the browser.