Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

newbie quest for Image::Grab

by lone5ive (Novice)
on May 30, 2003 at 10:31 UTC ( [id://261801]=perlquestion: print w/replies, xml ) Need Help??

lone5ive has asked for the wisdom of the Perl Monks concerning the following question:

Hello wiseones..

I am a newbie to perl and am wrestling with the Image::Grab module. I have got it working fine to grab the images that i want but, I am trying to workout how to use the Date and MD5 options. Having read the info on CPAN, elsewhere and doing a super search here.. I am still none the wiser. Here is my code..

#!/usr/bin/perl -w use strict; use Image::Grab; my $image = new Image::Grab; $image->url('http://www.mysite.com/my.jpg'); $image->grab_new;

# save image bit here

I can see that i can use $image->date to see the date info for the file, which is in epochtime. Do I compare this with my local time or am i completely barking up the wrong tree?

I would like to run script as a cronjob and only get the image if it has been updated. I have the MD5 module installed.

Any advice would be appreciated, i know i still have alot to learn grasshopper! :o)

Linux MDK9.0::Perl 5.8.0::Image::Grab 1.4

Replies are listed 'Best First'.
•Re: newbie quest for Image::Grab
by merlyn (Sage) on May 30, 2003 at 14:20 UTC
    I would like to run script as a cronjob and only get the image if it has been updated.
    Honing in on this, I'd say a better bet is to use LWP::Simple's mirror method:
    use LWP::Simple qw(mirror); my $result = mirror("http://host.example.com/url/foo.jpg", "localfile. +jpg"); if ($result == 304) { print "no update needed\n": } elsif ($result = 200) { print "New image has arrived!\n"; } else { die "Bad mirror status: $result\n"; }
    This is presuming that the image is either a static file, updated from time to time, or that the image is dynamically generated but with a proper handling of "if-modified-since".

    Using mirror with a proper webserver guarantees minimal web traffic. I use it to mirror web pages and images a lot. I've even written a few columns using it. {grin}

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re: newbie quest for Image::Grab
by wufnik (Friar) on May 30, 2003 at 12:29 UTC
    hola, i'd highly recommend that you check out

    http://mah.everybody.org/hacks/perl/getcomics.txt,

    (core by Mark Hershberger, adapted by J.Stoffel)

    a spritely wee script which downloads images intelligently; it is almost directly applicable to your problem of finding new images.

    the business end of how it determines newness is in the following snippet, which could pretty much directly be copied & pasted to achieve what you want. rc is the request obtained from a lwp::useragent
    # Get the MD5 fingerprint of the file. open(IMAGE, "<$imagedir/$imagename") || die "Can't open $imagedir/$ima +gename\n"; $context = new MD5; $context->MD5::reset(); $context->MD5::add(<IMAGE>); $filehash = $context->hexdigest(); close(IMAGE); # Get the MD5 fingerprint of the new picture open(TEMP, ">/tmp/getcomics.$$") || die "Can't open tempfile\n"; print TEMP $rc->content(); close(TEMP); open(TEMP, "</tmp/getcomics.$$") || die "Can't open tempfile\n"; $context->MD5::reset(); $context->MD5::add(<TEMP>); $newhash = $context->hexdigest(); close(TEMP); unlink "/tmp/getcomics.$$"; # See if the old info is different from the new. if( $newhash ne $filehash ) { # strut your funky stuff *here* }
    hope that helps,

    ...wufnik

    -- in the world of the mules there are no rules --
Re: newbie quest for Image::Grab
by arthas (Hermit) on May 30, 2003 at 10:39 UTC
    All epoch strings are in UTC (Universal Time Coordinated, also known as UTC), so you can safely compare the time you get with your system's time (time() will return you an UTC epoch string)... provided the hardware clock is set correctly on both the web server and your system of course. ;)

    Michele.
      Hi Michele

      Thanks for the reply. Having checked there time, unfortuneately it is way out of sync.. so no solution there! I shall keep trying..

      Regards Tom

Re: newbie quest for Image::Grab
by acarvalh (Novice) on Jun 02, 2003 at 14:24 UTC

    Why just not use time? :-) This will give you the "number of seconds from the epoch" format you're looking for.

    my $epoch_time = time;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://261801]
Front-paged by jonnyfolk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-03-29 10:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found