http://www.perlmonks.org?node_id=285690

This is a rework of an older script I made which did the same, but was really nasty and I doubt that this is any better *grin* Anyways it grabs the current APOD and outputs it to the cwd of where you called the script. It dates the output file as: apod(yyyy)(mm)(dd), where yyyy,mm,dd is the month day year respectively, and no the parenthesis aren't in the filename.

Edit: Changed the date format from month,day,year to year,month,day to make sorting alot easier.
Pointed out by sauoq

Edit2: Updated URI, added png.

#!/usr/bin/perl use strict; use warnings; use URI::URL; use LWP::Simple qw/get/; my $TOP = "http://apod.nasa.gov/apod/"; my $IMGPATH = 'image/\d{4}/\w+.(gif|jpg|jpeg|png)'; my @timearray = gmtime(); my $time = sprintf "%04d%02d%02d", ($timearray[5]+1900), ($timearray[4 +]+1), ($timearray[3]); my $content = get $TOP or die "Cannot get APOD Page\n"; my ($imgurl) = $content =~ m!($IMGPATH)!i; $imgurl = url($imgurl,$TOP)->abs; my $picture = get $imgurl or die "Cannot grab image: $!\n"; open(OUT, ">apod$time\.$2") or die "Cannot write to file: $!\n"; binmode(OUT); print OUT $picture; close OUT;

Replies are listed 'Best First'.
Re: Astronomy Pic of the Day Grabber
by sauoq (Abbot) on Aug 23, 2003 at 00:12 UTC

    I have a small suggestion. Consider using yyyymmdd as the date format instead of mmddyyyy. Then your files will sort nicely.

    -sauoq
    "My two cents aren't worth a dime.";
    
      Meh, I wasn't thinking of sorting when I did that part :) But I'll fix.
Perl/Tk --- Astronomy Pic of the Day Grabber
by PERLscienceman (Curate) on Aug 25, 2003 at 02:43 UTC
    Greetings OverlordQ and Fellow Monks I saw your Astronomy Pick Of The Day post, liked it, and thought it would be really cool taken one step further and put to Perl/Tk. So... I adapted the code from another post of mine Perl/Tk---Weather Map Grabber and appended the relevant code to your original.

    So now the script does the following:
    1) Grabs the Image
    2) Saves it to a day specific filename yyyymmdd format as suggested by sauoq (I agree it does allow for easier filename sorting)
    3) Displays it in a Tk Window!
    4) Clicking on the Image displays a dialog box containing image source info.

    Important Note for Windows ActiveState Perl 5.6.1 users: as of Build 635, their still appears to be a compatability issue with Tk::JPEG, a module used by this script. I have come up with a 'work-around' for this problem. If you encounter this issue please see my node that addresses this Re: Activestate TK::JPEG compatibility issue -- QUICK WORK AROUND FOUND, Any Better Ideas?. This should not be an issue for Unix/Linux users since they would compile the module on their own specific OS.

    Anyway... enough said. The code is posted below.

    As always, any input or suggestions for improvement of this code are greatly appreciated.
    Enjoy and Have Fun!

    UPDATE: 08/26/2003 -- Modified Perl/Tk Display section of the code to account for the possibility of grabbing a gif image. However, need to work on the proper functional display of an animated gif which it looks like today's image appears to be.
    Any suggestions on how to do this in Perl/Tk? Thanks.


    #!/usr/bin/perl use strict; use warnings; use URI::URL; use LWP::Simple qw/get/; use Tk; use Tk::DialogBox; use Tk::Photo; use Tk::JPEG; my $TOP = "http://antwrp.gsfc.nasa.gov/apod/"; my $IMGPATH = 'image/\d{4}/\w+.(gif|jpg|jpeg)'; my @timearray = gmtime(); #my $time = sprintf "%02d%02d%04d", ($timearray[4]+1), ($timearray[3]) +, ($timearray[5]+1900); my $time = sprintf "%04d%02d%02d", ($timearray[5]+1900),($timearray[4 +]+1), ($timearray[3]); my $titledate=sprintf "%02d-%-02d-%04d", ($timearray[4]+1), ($timearra +y[3]), ($timearray[5]+1900); my $content = get $TOP or die "Cannot get APOD Page\n"; my ($imgurl) = $content =~ m!($IMGPATH)!i; $imgurl = url($imgurl,$TOP)->abs; print "Acquiring Image File ....\n"; my $picture = get $imgurl or die "Cannot grab image: $!\n"; my $ext=$2; my $fmt="jpeg"; if ($ext =~ m/(gif)/ig){$fmt="gif";} my $imgfile="apod$time\.$ext"; print "Saving as $imgfile . \n"; open(OUT, ">$imgfile") or die "Cannot write to file: $!\n"; binmode(OUT); print OUT $picture; close OUT; my $MW=MainWindow->new(-title=>"Astronomy Pic of The Day $titledate"); ### --- Prevents Main Window Resizing $MW->bind('<Configure>' => sub{ my $xe = $MW->XEvent; $MW->maxsize($xe->w, $xe->h); $MW->minsize($xe->w, $xe->h); }); print "generating Tk Window ....\n"; # my $image = $MW->Photo('-format' => 'jpeg', -file=>'apod08222003.jpg +'); #my $image = $MW->Photo('-format' => 'jpeg', -file=>$imgfile); my $image = $MW->Photo('-format' => $fmt, -file=>$imgfile); $MW->Button(-image=>$image, -command => sub { my $dialogA = $MW->DialogBox( -title => "About This Image", -buttons => [ "OK" ], ); $dialogA->add("Label", -borderwidth => '2', -background => "#FAEBD7", #ANTIQUE WHITE -font => 'bold', -justify => 'left', -relief => 'sunken', -text => "This Image is Produced by: N +ASA GSFC,\n and can be found along with other Astronomy Related Image +s here:\nhttp://antwrp.gsfc.nasa.gov/apod/astropix.html")->pack; ### --- Prevents Dialog Box Resizing $dialogA->bind('<Configure>' => sub{ my $xeD = $dialogA->XEvent; $dialogA->maxsize($xeD->w, $xeD->h); $dialogA->minsize($xeD->w, $xeD->h); }); $dialogA->Show; } )->pack; MainLoop(); ## Original Astronomy POD code by OverlordQ 08/22/2003 ## http://www.perlmonks.org/index.pl?node_id=285690 ## Additional Perl/Tk code by: PERLscienceman 08/24/2003 ## Code by PERLscienceman updated 08/26/2003 to compensate for capture + of .gif ## http://www.perlmonks.org/index.pl?node_id=243154