You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
1.4 KiB
76 lines
1.4 KiB
2 years ago
|
package main
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"io"
|
||
|
"log"
|
||
|
"net/http"
|
||
|
"os"
|
||
|
"runtime"
|
||
|
|
||
|
"github.com/reujab/wallpaper"
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
apod APODResponse
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
resp, err := http.Get("https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY")
|
||
|
if err != nil {
|
||
|
log.Fatalf("\nUnable to get NASA API Response\n")
|
||
|
return
|
||
|
}
|
||
|
decoder := json.NewDecoder(resp.Body)
|
||
|
err = decoder.Decode(&apod)
|
||
|
if err != nil {
|
||
|
log.Fatalf("\nUnable to decode APOD Response: \n%+v\n", resp)
|
||
|
return
|
||
|
}
|
||
|
err = wallpaper.SetFromURL(apod.Hdurl)
|
||
|
if err != nil {
|
||
|
log.Fatalf("\nUnable to set wallpaper\n")
|
||
|
return
|
||
|
}
|
||
|
err = wallpaper.SetMode(wallpaper.Center)
|
||
|
if err != nil {
|
||
|
log.Fatalf("\nUnable to set wallpaper mode\n")
|
||
|
return
|
||
|
}
|
||
|
|
||
|
if runtime.GOOS != "windows" {
|
||
|
err = downloadFile("/usr/share/backgrounds/spaceface/lock.jpg", apod.Hdurl)
|
||
|
if err != nil {
|
||
|
log.Fatalf("\nUnable to Download lock.jpg image\n")
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
// https://stackoverflow.com/questions/33845770/how-do-i-download-a-file-with-a-http-request-in-go-language/33845771
|
||
|
func downloadFile(filepath string, url string) (err error) {
|
||
|
|
||
|
// Create the file
|
||
|
out, err := os.Create(filepath)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
defer out.Close()
|
||
|
|
||
|
// Get the data
|
||
|
resp, err := http.Get(url)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
defer resp.Body.Close()
|
||
|
|
||
|
// Writer the body to file
|
||
|
_, err = io.Copy(out, resp.Body)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|