Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

wget - getting status code?

by ultranerds (Friar)
on May 21, 2009 at 11:02 UTC ( #765421=perlquestion: print w/ replies, xml ) Need Help??
ultranerds has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I know this isn't really stricly perl related, but seeing as its going in a Perl script (and you guys seem to know most things :P), I thought I'd ask here =)

Basically, I'm trying to check if an image exists, before downloading it via wget. For example

http://www.test.com/something.gif

...gives a 404 status, while:

http://perlmonks.org/images/monkpics/pater_hat_sm.gif

..gives a 200.

I know its possible to do this via wget, but I can't for the life of me remember how / find the script I did it in previously.

Any suggestions?

TIA

Andy

Comment on wget - getting status code?
Replies are listed 'Best First'.
Re: wget - getting status code?
by lakshmananindia (Chaplain) on May 21, 2009 at 11:17 UTC

    Refer LWP::UserAgent

    --Lakshmanan G.

    The great pleasure in my life is doing what people say you cannot do.


      Thanks - I managed to get it working with:

      # now lets actually get the image.. my $ua = LWP::UserAgent->new; $ua->timeout(10); $ua->env_proxy; my $response = $ua->get($image); if ($response->is_success) { #print $response->content; # or whatever open(WRITEIT,">$save_to") || die qq|Cant write to $save_to, r +eason: $!|; binmode WRITEIT; print WRITEIT $response->content; close(WRITEIT); my ($x,$y) = imgsize($save_to); print qq|Got sizes: $x x $y \n|; return ($x,$y,$save_to_url); } else { # save a bad status... return (0,0,undef); }


      (just in case anyone else comes across this issue :)

      Cheers

      Andy
        Just for accuracy - I think your solution is not the real answer to the question you've asked initially.

        Probably irrelevant here, but you said you'd want to check if the image exists _before_ downloading it. In your code, you download it and _then_ check if it exists.

        If downloading is the single option, you're code is OK. But if you'd want to avoid say images bigger then 2 GiB, you're code would waste bandwide, downloading the images and then disposing them.

        To actually just check for the existence of an image, you'll want to use $ua->head($image) instead of $ua->get($image).

        Just in case that could matter.


        Krambambuli
        ---

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://765421]
Approved by targetsmart
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (14)
As of 2015-07-29 20:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (268 votes), past polls