lib/IMDB/Local/Download.pm untested

filter: Statementstmt 0.0% 0 / 88 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 13 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 137 SCAR 48.8
CC
11
Cov
0%
downloadListFile
56.0
init
12.0
download
12.0
CRAP 132.0
1 package IMDB::Local::Download;
2  
3 use 5.006;
4 use strict;
5 use warnings;
6  
7 =head1 NAME
8  
9 IMDB::Local::Download - Object to manage IMDB List file downloads.
10  
11 =head1 VERSION
12  
13 Version 0.01
14  
15 =cut
16  
17 our $VERSION = '1.00';
18  
19 =head1 SYNOPSIS
20  
21 Quick summary of what the module does.
22  
23 Perhaps a little code snippet.
24  
25 my $foo = new IMDB::Local::Download('listsDir' => "./imdb-data/lists");
26  
27 my $forceRefresh=1;
28  
29 $foo->download($forceRefresh);
30 ...
31  
32 =head1 EXPORT
33  
34 A list of functions that can be exported. You can delete this section
35 if you don't export anything, such as for a purely object-oriented module.
36  
37 =head1 SUBROUTINES/METHODS
38  
39 =cut
40  
41 use LWP;
42  
43 # Use Term::ProgressBar if installed.
44 use constant Have_bar => eval {
45 require Term::ProgressBar;
46 $Term::ProgressBar::VERSION >= 2;
47 };
48  
49  
50 use IMDB::Local::ListFiles;
51  
52 =head2 new
53  
54 =cut
55  
56 use Class::MethodMaker
57 [
58 scalar => ['listsDir'],
59 scalar => [{-default => 0}, 'verbose'],
60 scalar => [{-default => 'ftp://ftp.fu-berlin.de/pub/misc/movies/database'}, 'baseUrl'],
61 scalar => [{-default => 1}, 'movies'],
62 scalar => [{-default => 1}, 'directors'],
63 scalar => [{-default => 1}, 'actors'],
64 scalar => [{-default => 1}, 'actresses'],
65 scalar => [{-default => 1}, 'genres'],
66 scalar => [{-default => 1}, 'ratings'],
67 scalar => [{-default => 1}, 'keywords'],
68 scalar => [{-default => 1}, 'plot'],
69 scalar => [{-default => 1}, 'showProgressBar'],
70  
71 scalar => [{ -type => 'IMDB::Local::ListFiles'}, '_listFiles'],
72 new => [qw/ -init -hash new /] ,
73 ];
74  
75 =head2 movies(boolean)
76  
77 specify if movies list file should be included in list of files to download
78  
79 =head2 directors(boolean)
80  
81 specify if directors list file should be included in list of files to download
82  
83 =head2 actors(boolean)
84  
85 specify if actors list file should be included in list of files to download
86  
87 =head2 actresses(boolean)
88  
89 specify if actresses list file should be included in list of files to download
90  
91 =head2 genres(boolean)
92  
93 specify if genres list file should be included in list of files to download
94  
95 =head2 ratings(boolean)
96  
97 specify if ratings list file should be included in list of files to download
98  
99 =head2 keywords(boolean)
100  
101 specify if keywords list file should be included in list of files to download
102  
103 =head2 plot(boolean)
104  
105 specify if plot list file should be included in list of files to download
106  
107 =cut
108  
109 sub init
110 {
111 my ($self)=@_;
112  
113 $self->_listFiles(new IMDB::Local::ListFiles(listsDir=>$self->listsDir(),
114 movies=>$self->movies(),
115 directors=>$self->directors(),
116 actors=>$self->actors(),
117 actresses=>$self->actresses(),
118 genres=>$self->genres(),
119 ratings=>$self->ratings(),
120 keywords=>$self->keywords(),
121 plot=>$self->plot()));
122  
123  
124 if ( ! -d $self->listsDir() ) {
125 die $self->listsDir().":does not exist";
126 }
127  
128 # only leave progress bar on if its available
129 if ( !Have_bar ) {
130 $self->showProgressBar(0);
131 }
132  
133 return($self);
134 }
135  
136 =head2 listFiles
137  
138 Get an array of list files
139  
140 =cut
141  
142 sub listFiles($)
143 {
144 my ($self)=@_;
145  
146 return $self->_listFiles->listFiles();
147 }
148  
149  
150 =head2 downloadListFile
151  
152 Same as download() but when you want to control each list file.
153 If an existing list file exists it is removed prior to download starting.
154  
155 =cut
156  
157 sub downloadListFile($$$)
158 {
159 my ($self, $type, $force)=@_;
160  
161 my $filepath=$self->_listFiles->paths_index($type);
162 if ( $force && -f $filepath ) {
163 unlink($filepath);
164 }
165  
166 $self->_listFiles()->statFiles();
167  
168 my $url = $self->baseUrl()."/$type.list.gz";
169
170 #my $filepath = $self->listsDir()."/$type.gz";
171
172 #warn "$filepath: does not exist\n";
173
174 my $partial = $self->listsDir()."/$type.gz.partial";
175 if (-e $partial) {
176 unlink $partial or die "cannot unlink $partial: $!";
177 }
178
179 print "Downloading $url..\n";
180  
181 #
182 # For downloading we use LWP
183 #
184 my $ua = LWP::UserAgent->new();
185 $ua->env_proxy();
186 $ua->show_progress(($self->showProgressBar()!=0));
187
188 my $req = HTTP::Request->new(GET => $url);
189 $req->authorization_basic('anonymous', 'IMDB::Local::Download');
190
191 my $resp = $ua->request($req, $filepath);
192 my $got_size = -s $filepath;
193
194 if (defined $resp and $resp->is_success ) {
195 die if not $got_size;
196 print "<$url>\n\t-> $filepath, success\n\n";
197 }
198 else {
199 my $msg = "failed to download $url to $filepath";
200 $msg .= ", http response code: ".$resp->status_line if defined $resp;
201 warn $msg;
202 if ($got_size) {
203 warn "renaming $filepath -> $partial\n";
204 rename $filepath, $partial
205 or die "cannot rename $filepath to $partial: $!";
206 warn "You might try continuing the download of <$url> manually.\n";
207 }
208 return(0);
209 }
210 return(1);
211 }
212  
213 =head2 download
214  
215 Attempt to download all missing list files
216  
217 =cut
218  
219 sub download
220 {
221 my ($self, $force)=@_;
222
223 $self->_listFiles()->statFiles();
224  
225 for my $type ( $self->_listFiles->listFiles() ) {
226
227 # skip the ones that are already downloaded
228 my $filepath=$self->_listFiles->paths_index($type);
229 if ( -f $filepath && ! $force ) {
230 next;
231 }
232  
233 if ( $self->downloadListFile($type, $force) != 1 ) {
234 # stop short
235 return(0);
236 }
237 }
238  
239 return(1);
240 }
241  
242  
243 =head1 AUTHOR
244  
245 jerryv, C<< <jerry.veldhuis at gmail.com> >>
246  
247 =head1 BUGS
248  
249 Please report any bugs or feature requests to C<bug-imdb-local at rt.cpan.org>, or through
250 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=IMD-Local>. I will be notified, and then you'll
251 automatically be notified of progress on your bug as I make changes.
252  
253  
254  
255  
256 =head1 SUPPORT
257  
258 You can find documentation for this module with the perldoc command.
259  
260 perldoc IMDB::Local::Download
261  
262  
263 You can also look for information at:
264  
265 =over 4
266  
267 =item * RT: CPAN's request tracker (report bugs here)
268  
269 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=IMDB-Local>
270  
271 =item * AnnoCPAN: Annotated CPAN documentation
272  
273 L<http://annocpan.org/dist/IMDB-Local>
274  
275 =item * CPAN Ratings
276  
277 L<http://cpanratings.perl.org/d/IMDB-Local>
278  
279 =item * Search CPAN
280  
281 L<http://search.cpan.org/dist/IMDB-Local/>
282  
283 =back
284  
285  
286 =head1 ACKNOWLEDGEMENTS
287  
288  
289 =head1 LICENSE AND COPYRIGHT
290  
291 Copyright 2015 jerryv.
292  
293 This program is free software; you can redistribute it and/or modify it
294 under the terms of the the Artistic License (2.0). You may obtain a
295 copy of the full license at:
296  
297 L<http://www.perlfoundation.org/artistic_license_2_0>
298  
299 Any use, modification, and distribution of the Standard or Modified
300 Versions is governed by this Artistic License. By using, modifying or
301 distributing the Package, you accept this license. Do not use, modify,
302 or distribute the Package, if you do not accept this license.
303  
304 If your Modified Version has been derived from a Modified Version made
305 by someone other than you, you are nevertheless required to ensure that
306 your Modified Version complies with the requirements of this license.
307  
308 This license does not grant you the right to use any trademark, service
309 mark, tradename, or logo of the Copyright Holder.
310  
311 This license includes the non-exclusive, worldwide, free-of-charge
312 patent license to make, have made, use, offer to sell, sell, import and
313 otherwise transfer the Package with respect to any patent claims
314 licensable by the Copyright Holder that are necessarily infringed by the
315 Package. If you institute patent litigation (including a cross-claim or
316 counterclaim) against any party alleging that the Package constitutes
317 direct or contributory patent infringement, then this Artistic License
318 to you shall terminate on the date that such litigation is filed.
319  
320 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
321 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
322 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
323 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
324 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
325 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
326 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
327 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
328  
329  
330 =cut
331  
332 1; # End of IMDB::Local::Download