/
ng-daily-wallpaper
executable file
·107 lines (74 loc) · 2.7 KB
/
ng-daily-wallpaper
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
#!perl
# ABSTRACT: use National Geographic daily photo of the day as desktop background
use strict;
use autodie ':all';
use File::Path 'mkpath';
use Getopt::Long;
use POSIX 'strftime';
use Pod::Usage::CommandLine qw(GetOptions pod2usage);
use WWW::Mechanize;
my %opts = (
prev => 0,
'download-only' => 0,
dir => "$ENV{HOME}/Pictures/National Geographic");
GetOptions(\%opts,
'prev|p=i',
'dir|d=s',
'date=s',
'download-only') or pod2usage;
my $url =
'http://photography.nationalgeographic.com/photography/photo-of-the-day/';
my $mech = WWW::Mechanize->new;
my $date = $opts{date} || strftime('%Y-%m-%d', localtime);
my $picfile = download_picture();
if (-e $picfile) {
unless ($opts{'download-only'}) {
system qw(gconftool-2 --type string --set /desktop/gnome/background/picture_filename), $picfile;
system qw(gconftool-2 --type string --set /desktop/gnome/background/picture_options zoom);
}
}
# download source picture from National Geographic and save
sub download_picture {
my $tempfile = "$opts{dir}/${date}.jpg";
return $tempfile if -e $tempfile;
$mech->get($url);
for (1 .. $opts{prev}) {
$mech->follow_link(text_regex => qr/previous/i);
}
my $link = $mech->find_link(text_regex => qr/download wallpaper.*pixels/i)
or die "This wallpaper is not downloadable\n";
unless (-d $opts{dir}) {
mkpath $opts{dir};
}
$mech->get($link, ':content_file' => $tempfile);
return $tempfile;
}
1;
__END__
=head1 SYNOPSIS
ng-daily-wallpaper [options]
Options:
--help display this help message
--dir directory where images are saved
--date use previously downloaded picture for this date
--prev get Nth previoius picture of the day (up to 3)
--download-only only download picture (do not set as wallpaper)
=head1 DESCRIPTION
This program downloads the current picture of the day from National Geographic,
resizes a copy to fit your monitor, and sets it as your desktop background.
=head1 OPTIONS
=over 4
=item B<--date>
Use the given date. If a picture was already downloaded for that date, then
that picture will be used. Otherwise the current picture will be downloaded
and saved for the given date.
=item B<--dir, -d>
Set directory where images are saved.
default: C<$HOME/Pictures/National Geographic>
=item B<--download-only>
Just download the picture and resize it. Do not set as desktop wallpaper
=item B<--prev, -p>
Get Nth previous picture of the day. This may not work for large values.
Download links are not always available on older pictures of the day.
Default: 0 (get current day's picture)
=back