Skip to content

Instantly share code, notes, and snippets.

@lierdakil
Created June 15, 2020 03:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lierdakil/5e94029afa863f5012e98309741d3493 to your computer and use it in GitHub Desktop.
Save lierdakil/5e94029afa863f5012e98309741d3493 to your computer and use it in GitHub Desktop.
Downloads NASA Astronomy Picture of the Day and displays it as the background using feh
#!/usr/bin/env -S deno run --allow-net=api.nasa.gov,apod.nasa.gov --allow-env --allow-write=${HOME}/.apodwallpaper --allow-run
// Filename: apod.ts
// Purposes: Downloads NASA Astronomy Picture of the Day and displays it as the background using feh
// Author(s): Nikolay "Lierdakil" Yakimov https://github.com/lierdakil
// !!! Replace this with your NASA API key; go to https://api.nasa.gov/ to get one
const api_key = 'DEMO_KEY'
// Read home variable
const home = Deno.env.get('HOME')
if(home == undefined) {
throw new Error('$HOME undefined')
}
// Create directory
const apod_dir = `${home}/.apodwallpaper`
try {
await Deno.mkdir(apod_dir)
} catch (e) {
if (e.name !== 'AlreadyExists') {
throw e
}
}
// Get random record
const response = await fetch(`https://api.nasa.gov/planetary/apod?api_key=${api_key}&count=1`)
const result = await response.json()
// Get image url
const hdurl = result[0].hdurl
// Fetch image
const response_img = await fetch(hdurl)
const result_img = await response_img.blob()
const ext = hdurl.match(/\.([^.]*)$/)[1]
const apod_file = `${home}/.apodwallpaper/apod.${ext}`
// Save image
await Deno.writeFile(apod_file, new Deno.Buffer(await result_img.arrayBuffer()).bytes())
// Run feh
const proc = Deno.run({
cmd: ['feh', '--no-xinerama', '--bg-fill', apod_file],
stderr: 'inherit',
stdout: 'inherit',
})
await proc.status()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment