Skip to content

Instantly share code, notes, and snippets.

@dylanjamesdev
Last active July 16, 2026 23:22
Show Gist options
  • Select an option

  • Save dylanjamesdev/2f9a4501f5a02ee6203e7dae20ba0ba2 to your computer and use it in GitHub Desktop.

Select an option

Save dylanjamesdev/2f9a4501f5a02ee6203e7dae20ba0ba2 to your computer and use it in GitHub Desktop.
NASA Daily APOD Wallpaper - Arch w/ PlasmaKDE
#!/usr/bin/perl
use strict;
use warnings;
use HTTP::Tiny;
use JSON::PP;
use File::Path qw(make_path);
my $home = $ENV{HOME};
my $save_dir = "$home/Pictures/NASA";
make_path($save_dir) unless -d $save_dir;
my $api_url = 'https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY&hd=true';
my $http = HTTP::Tiny->new();
my $response = $http->get($api_url);
unless ($response->{success}) {
die "Failed to fetch API data: $response->{status} $response->{reason}\n";
}
my $data = decode_json($response->{content});
if ($data->{media_type} ne 'image') {
print "Today's APOD is a $data->{media_type}, not an image. Leaving current wallpaper.\n";
exit 0;
}
my $image_url = $data->{hdurl};
my $image_title = $data->{title};
my $date = $data->{date};
if (!$image_url) {
die "Could not find an HD image URL in the NASA API response.\n";
}
my $image_path = "$save_dir/apod-$date.jpg";
my $img_response = $http->mirror($image_url, $image_path);
unless ($img_response->{success}) {
die "Failed to download image: $img_response->{status} $img_response->{reason}\n";
}
opendir(my $dh, $save_dir) or warn "Cannot open $save_dir\n";
while (my $file = readdir($dh)) {
next if $file eq "apod-$date.jpg";
unlink "$save_dir/$file" if $file =~ /^apod-.*\.jpg$/;
}
closedir($dh);
my $uid = $<;
$ENV{DBUS_SESSION_BUS_ADDRESS} = "unix:path=/run/user/$uid/bus";
system('plasma-apply-wallpaperimage', $image_path) == 0
or die "Failed to apply wallpaper\n";
system('notify-send', 'NASA APOD Updated', $image_title, '--icon=plasma') == 0
or warn "Failed to send notification\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment