Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env perl
- use strict;
- use warnings;
- use Net::FTP;
- use Getopt::Long;
- use File::Path 'mkpath';
- use Data::Dumper;
- # User-input/Defaults. No error checking! Dangerous...
- my $local_dir = '/tmp/FEC_data';
- my $max_connections = 5;
- my $help = 0;
- GetOptions(
- "d|directory=s" => \$local_dir,
- "m|max-connections=i" => \$max_connections,
- "h|help" => $help,
- ) or &usage;
- &usage if $help;
- my $host = 'ftp.fec.gov';
- my $remote_dir = 'FEC/electronic';
- my (@modtime, @timeouts, $count, $child);
- $count = 0;
- $SIG{INT} = "interrupt";
- mkpath ($local_dir, 1, 0775) unless -e $local_dir;
- opendir(DIR, $local_dir) or die 'Could not open dir: ' . $!;
- my @local_files = grep !/^\.?\.$/, readdir DIR;
- close DIR;
- my $ftp = Net::FTP->new($host,
- Debug => 0,
- Passive => 1)
- or die "Cannot connect to host: $@";
- $ftp->login('anonymous', 'doesnt@matter.com')
- or die "Login failed ", $ftp->message;
- $ftp->cwd($remote_dir)
- or die "Couldn't change directory ", $ftp->message;
- my @files = $ftp->ls('.')
- or die "Couldn't get directory listening ", $ftp->message;
- $ftp->close();
- undef $ftp;
- print 'Comparing files' . "\n";
- @files = grep !${{map{$_,1} @local_files}}{$_}, @files;
- #print Dumper(@files);
- my $pid;
- my @pids;
- while (my @smallchunks = splice @files, 0, int($#files/$max_connections)) {
- sleep 2;
- $pid = fork();
- if ($pid == 0) {
- my $ftp = Net::FTP->new($host,
- Debug => 0,
- Passive => 1,
- Timeouts => '300')
- or die "Cannot connect to host: $@";
- $ftp->login('anonymous', 'doesnt@matter.com')
- or die "Login failed ", $ftp->message;
- $ftp->cwd($remote_dir)
- or die "Couldn't change directory ", $ftp->message;
- $ftp->binary();
- foreach my $file (@smallchunks) {
- print "Attempting to download: $file" . "\n";
- eval {$ftp->get($file) or warn "Failed to download: " . $file . " || ", $ftp->message};
- if ($@ =~ /Timeout/) {
- push @timeouts, $file;
- }
- }
- $ftp->quit;
- exit 0;
- } else {
- push @pids, $pid;
- }
- }
- do {$child = waitpid (-1,0);} while $child > 0;
- waitpid($pid, 0) if $pid != 0;
- print "All files downloaded I hope\n";
- open(FB, ">/tmp/timeouts.txt");
- map {print FB "$_"} @timeouts;
- close FB;
- sub usage {
- print <<'METADATA';
- FEC Mass Downloader/Updater - downloads a shit ton of .fec files easily
- Usage:
- ./dl_fec.pl [options]
- options are following :
- -h, --help : display this help message
- -m, --max-connections : maximum ftp connections (default: 7)
- -d, --directory : Directory/folder to put downloaded files
- (default: /tmp/FEC_Data, must be writable)
- Example:
- ./dl_fec.pl
- ./dl_fec.pl -d /tmp/fec_temp -m 10
- ./dl_fec.pl --directory=/tmp/fec_temp --max-connections=10
- METADATA
- exit 0;
- }
- sub interrupt {
- $count++;
- die if $count > 1;
- }
Add Comment
Please, Sign In to add comment