NASA has a website (https://apod.nasa.gov/apod/astropix.html) where publishs every day an astronomy picture plus a brief description
I have this url in my bookmark and usually I use the picture to change my Desktop background (they have a lot of great pictures) so I decided to have a cron to change it automatically but after a while I realized that most of the time I didn't know what I was watching so I included the explanation in the picture and now this is how my desktop looks
Script
I use a little groovy script to download the json where the url and the explanation are and after downloaded the image I "inject" the explation into it in several lines (as the explanation can be large I take only a few hundreds of chars and split them)
import javax.imageio.ImageIO | |
json = new groovy.json.JsonSlurper().parse("https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY".toURL()) | |
url = json.url | |
ext = url.split('\\.').last().toLowerCase() | |
if( ['jpg','png','jpeg'].contains(ext) == false ){ | |
return | |
} | |
image = ImageIO.read(url.toURL()) | |
g = image.graphics | |
g.font = g.font.deriveFont(25f) | |
lines = json.explanation.take(300).split("(?<=\\G.{100})")+["","Made with \u2665 by Puravida Software"] | |
lines.eachWithIndex{ str, idx -> | |
g.drawString str, 20, 20+(idx*30) | |
} | |
g.dispose() | |
ImageIO.write(image, "jpg", new File("${System.getProperty("user.home")}/Pictures/nasa.jpg")) |
as you can see the script perform this actions:
- parse the current JSON directly from NASA's website
- check if the picture is a valid extension (from time to time it's a video)
- download the image into a
Graphics
- split the 300 first chars of the explanation into 3
String
- paint every string at the top of the picture
- save the picture as $HOME/Pictures/nasa.jpg
Cron
I use crontab to run this script every hour:
crontab -e
0 * * * * JAVA_HOME=$HOME/.sdkman/candidates/java/current $HOME/.sdkman/candidates/groovy/current/bin/groovy $HOME/nasa.groovy
but you can use others approach, for example as an startup program
Desktop settings
Now it only remains to change my Desktop background as $HOME/Pictures/nasa.jpg and when the NASA update the picture of the day I'll have my desktop updated
Top comments (0)