Azuhmier

get_request.pl

Apr 21st, 2020 (edited)
935
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.44 KB | None | 0 0
  1. #!/usr/bin/perl;
  2. use warnings;
  3. use strict;
  4. use LWP::Simple;
  5. use LWP::UserAgent;
  6. use utf8;
  7. no warnings 'utf8';
  8.  
  9. # Set up LWP object
  10. my $ua = LWP::UserAgent->new(timeout => 10);
  11. $ua->agent('Mozilla/5.0');
  12.  
  13. # Start timer
  14. my $start = time;
  15.  
  16. # Get urls from 'url_only.txt'
  17. my @url;
  18. my $fname_urls = glob('~/hmofa/pastebin/code_examples/goto_files/url_only.txt');
  19. open my $fh, '<' , $fname_urls ;
  20.   while (my $line = <$fh>) {
  21.     $line =~ m/https:\/\/pastebin.com\/[^\s]+/;
  22.     push @url, $&;
  23.   }
  24. close $fh;
  25.  
  26. # Check if $dirname is available
  27. my $dirname = 'archive';
  28. my $DestFolder = glob('~/hmofa/pastebin/Pastebin_Archives/' . $dirname);
  29. my $FileNumber = 2;
  30.  
  31. if (-d $DestFolder) {
  32.   while (-d $DestFolder . '_' . $FileNumber ) {
  33.     $FileNumber++;
  34.   }
  35.   $DestFolder = $DestFolder . '_' . $FileNumber;
  36. }
  37.  
  38. print $DestFolder;
  39. mkdir $DestFolder;
  40.  
  41. # Fetch and Store html files
  42. my @fh; # will store the filehandles.
  43. my @html;
  44. my $i=0;
  45.  
  46. for (@url) {
  47.     my $response = $ua->get($_);
  48.     push @html, $response->decoded_content;
  49.     my $start = time;
  50.  
  51.     open $fh[$i], '>', $DestFolder . "/file-$i" or die $!;
  52.       if ($html[$i]) {
  53.       print {$fh[$i]} $html[$i];
  54.       }
  55.       else {
  56.         my $DeadString = $_ . "\n" . "DEAD_LINK";
  57.       print {$fh[$i]} $DeadString;
  58.       }
  59.     close $fh[$i];
  60.  
  61.     $i++;
  62.     while ( (time - $start) < 2){}
  63. }
  64.  
  65. my $duration = time - $start; print "\nExecution Time: $duration s\n";
  66.  
  67.  
  68.  
  69.  
Add Comment
Please, Sign In to add comment