-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPOD.pm
193 lines (151 loc) · 4.45 KB
/
APOD.pm
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env perl
# by gammy
use warnings;
use strict;
=head1 NAME
APOD - Astronomy Picture Of the Day module.
=head1 DESCRIPTION
A simple helper to fetch the image and description of NASAs
Astronomy Picture Of The Day.
=head1 SYNOPSIS
use APOD;
my $apod = new APOD;
# Peek at the APOD website to fill in the image url, description, etc.
# Always call this method first.
$apod->peek(); # takes any APOD page url as optional argument
$apod->destination("/tmp/");
if(-e $apod->destination . '/' . $apod->filename) {
die "We already have the APOD!\n";
}
$apod->save_image();
$apod->save_description();
=head1 Getters
$url_to_image = $apod->url;
$image_data = $apod->image; # requires ->get_image() or ->save_image()
$original_filename = $apod->filename;
$description = $apod->description;
$destination_dir = $apod->description;
=head1 Setters
$new_dest = $apod->destination("/new/destination/");
=cut
package APOD;
use base qw/HTML::Parser/;
use constant BASE_URL => 'https://apod.nasa.gov/apod';
use constant BASE_PAGE => BASE_URL . '/astropix.html';
use Carp;
use LWP::Simple;
use HTML::Parser;
my $ua;
my $text_buf;
my $path_found = 0;
# Getters
sub page_url { return $_[0]->{page_url}; }
sub url { return $_[0]->{url}; }
sub image { return $_[0]->{image}; }
sub filename { return $_[0]->{filename}; }
sub description { return $_[0]->{description}; }
# Getter/setter
sub destination {
my $self = shift;
if(@_) {
$self->{destination} = shift;
} else {
if(! $self->{destination}) {
$self->{destination} = '.';
}
}
return $self->{destination};
};
# Get the APOD webpage and find image url & description.
# Always call this first.
# Takes optional page URL as argument
sub peek {
my $self = shift;
$self->{description} = undef;
$self->{url} = undef;
$self->{image} = undef;
$self->{filename} = undef;
$path_found = 0;
if(@_) {
$self->{page_url} = shift;
} else {
$self->{page_url} = BASE_PAGE;
}
$ua = LWP::UserAgent->new();
my $html;
my $ret = $ua->get($self->{page_url});
if($ret->is_success) {
$html = $ret->decoded_content;
} else {
croak "Failed to get \"" . $self->{page_url} . "\": " . $ret->status_line;
}
$self->parse($html);
}
sub get_image {
my $self = shift;
my $url = $self->{url};
my $ret = $ua->get($url);
if($ret->is_success) {
$self->{image} = $ret->decoded_content;
} else {
croak "Can't get \"$url\": " . $ret->status_line;
}
}
sub save_image {
my $self = shift;
my $dst = $self->destination . '/' . $self->{filename};
croak "Call peek() first!" if ! $self->{description};
$self->get_image();
# user-defined argument overrides the /entire/ path including 'destination'
if(@_) {
$dst = shift;
}
open F, '>', $dst or croak "Can't open \"$dst\": $!";
print F $self->{image};
close F;
}
sub save_description {
my $self = shift;
my $dst = $self->destination . '/' . $self->{filename} . '.txt';
croak "Call peek() first!" if ! $self->{description};
# user-defined argument overrides the /entire/ path including 'destination'
if(@_) {
$dst = shift;
}
open F, '>', $dst or croak "Can't open \"$dst\": $!";
print F $self->{description};
close F;
}
# Overrides empty HTML::Parser sub
sub start {
my ($self, $tagname, $attr, $attrseq, $origtext) = @_;
if($tagname eq 'p') {
if($text_buf =~m/Explanation:/s) {
# The order of these is not random.
$text_buf =~s/\n+/\n/g;
$text_buf =~s/\n/ /g;
$text_buf =~s/ +/ /g;
$text_buf =~s/^ //g;
$text_buf =~s/ $//g;
$text_buf =~s/^Explanation: //;
$self->{description} = $text_buf .
" ($_[0]->{page_url})";
}
$text_buf = '';
}elsif($tagname eq 'a') {
if(defined $attr->{href} && $attr->{href}=~m#(image/.*)#s) {
if (! $path_found) {
$self->{url} = BASE_URL . '/' . $1;
$self->{filename} = substr($self->{url},
rindex($self->{url}, '/') + 1);
}
$path_found = 1;
}
}
}
# Overrides empty HTML::Parser sub
sub text {
my ($self, $text) = @_;
$text_buf .= $text;
}
1;