Amoe has asked for the wisdom of the Perl Monks concerning the following question:
It assumes that valid pictures aren't going to be under 5KB, reckless perhaps, but it suffices for the job. I just know you're gonna cuss that part to pieces, so...fire away! I was also thinking maybe I should check for binary and text data in $content, but I'm not quite sure how to do that without first writing to file and using filetest. Okay, flame away!sub get_object { my($agent, $url, $referer, $save_path); # args: LWP::UserAgent, + url to get, (optional) referer to grab that url with, (optional) pat +h to save it to my($request, $response, $content); # http stuff $agent = shift(); $url = shift(); $referer = shift(); $save_path = shift(); $request = HTTP::Request->new('GET', $url); $request->referer($referer) if ($referer); # if referer param i +s specified set it as the referer header $save_path ? print("Getting picture $url...") : print("Getting inf +o from $url\n"); $response = $agent->request($request); $content = $response->content(); return($content) unless ($save_path); # if we're not saving it, + return the html # now save it if (length($content) >= 5120) # check for a dud picture { open(OUT, ">$save_path") or die("Couldn\'t open picturefile to + save: $!"); binmode(OUT); # if we're saving it it must be a picture +, therefore binary print(OUT $content); close(OUT); return(1); } else { print("Picture was a dud."); return(0); } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Getting pictures with LWP
by damian1301 (Curate) on Aug 27, 2001 at 21:19 UTC | |
by Amoe (Friar) on Aug 27, 2001 at 23:06 UTC | |
|
Re: Getting pictures with LWP
by wog (Curate) on Aug 27, 2001 at 21:38 UTC |