简体   繁体   中英

Where does PERL LWP::Simple getstore save the image?

I am trying to use perl getstore to get a list of image from URL after reading a text file containing the file names, I created the code and able to run it successfully but I do not know where is the file saved, i checked the disk size it and shows that every time i run the code the hard disk free space decrease, so i assume there are file saved but I can't find it. So where does perl getstore save file and what is the correct way to save image from a link ?

use strict;
use warnings;
use LWP::UserAgent;
use LWP::Simple;

my $url = "https://labs.jamesooi.com/images/";
my $ua = LWP::UserAgent->new;
$ua->agent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36(KHTML, like Gecko) Chrome/59.0.3071.104 Safari/537.36");

my $file = 'image-list.txt';
open (DATA, $file) or die "Could not open $file: $!";

while(<DATA>){
  my $link = "$url" . "$_";
  my $filename = "$_";
  print $link;
  print $filename;

  my $req = HTTP::Request->new(GET => $link);
  my $res = $ua->request($req);
  if($res->is_success){
    my $rc = getstore($link, $filename);
    if(is_success($rc)){
      print "Success\n";
    }else{
      print "Error\n";
    }
  } else {
    print $res->status_line, "\n";
  }
}

According to the documentation , getstore(url, file) takes the URL as the first argument and the second argument is the file name where the result is stored. If the file name is a relative path (it doesn't begin with a slash / ) it will be relative to the current working directory.

But you read the name from a file and then treat the full line, including the newline character, as the file name. That's probably not what you want, so you should use chomp to remove the newline.

Apart from that:

  • You are doing first a GET request using LWP::UserAgent to retrieve the file but ignore the response and instead call getstore to retrieve and store the same resource if the first GET was successful. It would be simpler to either just save the result from the first GET or just skip it and use only getstore .

  • You are using DATA as a file handle. While this is not wrong, DATA is already an implicit file handle which points to the program file after the __DATA__ marker, so I recommend to use a different file handle.

When using a simplified version of the code the file gets successfully stored:

use strict;
use warnings;    

use LWP::Simple;

my $url  = "https://labs.jamesooi.com/images/";
my $file = 'image-list.txt';

open (my $fh, '<', $file) or die "Could not open $file: $!";

while ( <$fh> ) {

    chomp;  # remove the newline from the end of the line

    my $link     = $url . $_;
    my $filename = $_;

    my $rc = getstore($link, $filename);

    if (is_success($rc)) {
        print "Success\n";
    }
    else {
        print "Error\n";
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM