Guest User

Untitled

a guest
Jun 24th, 2018
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. #!/usr/bin/env perl
  2.  
  3. use strict;
  4. use warnings;
  5. use Net::FTP;
  6. use Getopt::Long;
  7. use File::Path 'mkpath';
  8. use Data::Dumper;
  9.  
  10. # User-input/Defaults. No error checking! Dangerous...
  11. my $local_dir = '/tmp/FEC_data';
  12. my $max_connections = 5;
  13. my $help = 0;
  14.  
  15. GetOptions(
  16. "d|directory=s" => \$local_dir,
  17. "m|max-connections=i" => \$max_connections,
  18. "h|help" => $help,
  19. ) or &usage;
  20.  
  21. &usage if $help;
  22.  
  23. my $host = 'ftp.fec.gov';
  24. my $remote_dir = 'FEC/electronic';
  25. my (@modtime, @timeouts, $count, $child);
  26.  
  27. $count = 0;
  28. $SIG{INT} = "interrupt";
  29.  
  30. mkpath ($local_dir, 1, 0775) unless -e $local_dir;
  31.  
  32. opendir(DIR, $local_dir) or die 'Could not open dir: ' . $!;
  33. my @local_files = grep !/^\.?\.$/, readdir DIR;
  34. close DIR;
  35.  
  36. my $ftp = Net::FTP->new($host,
  37. Debug => 0,
  38. Passive => 1)
  39. or die "Cannot connect to host: $@";
  40. $ftp->login('anonymous', 'doesnt@matter.com')
  41. or die "Login failed ", $ftp->message;
  42. $ftp->cwd($remote_dir)
  43. or die "Couldn't change directory ", $ftp->message;
  44. my @files = $ftp->ls('.')
  45. or die "Couldn't get directory listening ", $ftp->message;
  46. $ftp->close();
  47. undef $ftp;
  48.  
  49.  
  50. print 'Comparing files' . "\n";
  51. @files = grep !${{map{$_,1} @local_files}}{$_}, @files;
  52.  
  53. #print Dumper(@files);
  54.  
  55. my $pid;
  56. my @pids;
  57.  
  58. while (my @smallchunks = splice @files, 0, int($#files/$max_connections)) {
  59. sleep 2;
  60. $pid = fork();
  61. if ($pid == 0) {
  62. my $ftp = Net::FTP->new($host,
  63. Debug => 0,
  64. Passive => 1,
  65. Timeouts => '300')
  66. or die "Cannot connect to host: $@";
  67. $ftp->login('anonymous', 'doesnt@matter.com')
  68. or die "Login failed ", $ftp->message;
  69. $ftp->cwd($remote_dir)
  70. or die "Couldn't change directory ", $ftp->message;
  71. $ftp->binary();
  72. foreach my $file (@smallchunks) {
  73. print "Attempting to download: $file" . "\n";
  74. eval {$ftp->get($file) or warn "Failed to download: " . $file . " || ", $ftp->message};
  75. if ($@ =~ /Timeout/) {
  76. push @timeouts, $file;
  77. }
  78. }
  79.  
  80. $ftp->quit;
  81. exit 0;
  82. } else {
  83. push @pids, $pid;
  84. }
  85. }
  86. do {$child = waitpid (-1,0);} while $child > 0;
  87.  
  88. waitpid($pid, 0) if $pid != 0;
  89. print "All files downloaded I hope\n";
  90.  
  91. open(FB, ">/tmp/timeouts.txt");
  92. map {print FB "$_"} @timeouts;
  93. close FB;
  94.  
  95. sub usage {
  96.  
  97. print <<'METADATA';
  98.  
  99. FEC Mass Downloader/Updater - downloads a shit ton of .fec files easily
  100.  
  101. Usage:
  102. ./dl_fec.pl [options]
  103.  
  104. options are following :
  105. -h, --help : display this help message
  106. -m, --max-connections : maximum ftp connections (default: 7)
  107. -d, --directory : Directory/folder to put downloaded files
  108. (default: /tmp/FEC_Data, must be writable)
  109.  
  110. Example:
  111. ./dl_fec.pl
  112. ./dl_fec.pl -d /tmp/fec_temp -m 10
  113. ./dl_fec.pl --directory=/tmp/fec_temp --max-connections=10
  114.  
  115. METADATA
  116. exit 0;
  117. }
  118.  
  119. sub interrupt {
  120. $count++;
  121. die if $count > 1;
  122. }
Add Comment
Please, Sign In to add comment