-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch_apod.pl
executable file
·60 lines (58 loc) · 2.05 KB
/
fetch_apod.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env perl
# fetch_apod fetches the Astromy Picure of the day
# Copyright (C) 2013 Simon Plasger
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# See COPYING for details.
#
# See README for informations about how to use this program.
use LWP::Simple;
use fetch_apod_configuration;
# Constans for errors
use constant APOD_SUCCESS => 0;
use constant APOD_SITEFETCH_ERROR => 1;
use constant APOD_CONTENT_ERROR => 2;
use constant APOD_IMAGEFETCH_ERROR => 3;
$\ = "\n";
my $source;
if(1 == scalar(@ARGV)) # 1 Argument given?
{
$source = shift(@ARGV);
}
else
{
$source = $fetch_apod_configuration::defaultsource;
}
my $apodsite = get($source); # Load website
if(!defined($apodsite)) # Fetch error?
{
print "Couldn't fetch the APOD site";
exit APOD_SITEFETCH_ERROR;
}
my @useful = split("<br>\n", $apodsite); # Parsing out the useful content
my $useful = $useful[1];
my @linkseparator = split("\"", $useful);
if($linkseparator[0] ne "\n<a href=") # If this error appears, the layout of apod.nasa.gov possibly changed
{
print "Wrong content";
exit APOD_CONTENT_ERROR;
}
my $url = "http://apod.nasa.gov/".$linkseparator[1];
my @imagename = split("/", $linkseparator[1]);
my $imagename = $fetch_apod_configuration::safepath.$imagename[2];
my $copy = getstore($url, $imagename); # The download
if(!$copy)
{
print "Couldn't fetch image";
exit APOD_IMAGEFETCH_ERROR;
}
exit APOD_SUCCESS;