2022-09-18 19:28:56 +03:00
|
|
|
package functions
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"github.com/jasonlvhit/gocron"
|
|
|
|
"github.com/reujab/wallpaper"
|
|
|
|
"github.com/rodkranz/fetch"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func GetWallpaper() string {
|
|
|
|
file, readErr := os.ReadFile("config.yaml")
|
|
|
|
if readErr != nil {
|
|
|
|
panic(readErr)
|
|
|
|
}
|
|
|
|
data := make(map[interface{}]interface{})
|
|
|
|
|
|
|
|
marshalErr := yaml.Unmarshal(file, &data)
|
|
|
|
if marshalErr != nil {
|
|
|
|
panic(marshalErr)
|
|
|
|
}
|
|
|
|
|
2022-09-19 02:06:17 +03:00
|
|
|
type WallpaperStruct struct {
|
2022-09-18 19:28:56 +03:00
|
|
|
Hdurl string `json:"hdurl"`
|
|
|
|
Url string `json:"url"`
|
|
|
|
Media_type string `json:"media_type"`
|
|
|
|
}
|
|
|
|
|
|
|
|
client := fetch.NewDefault()
|
|
|
|
res, getErr := client.Get("https://api.nasa.gov/planetary/apod?api_key="+data["apiKey"].(string), nil)
|
|
|
|
if getErr != nil {
|
|
|
|
panic(getErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
body, StringErr := res.ToString()
|
|
|
|
if StringErr != nil {
|
|
|
|
panic(StringErr)
|
|
|
|
}
|
|
|
|
|
2022-09-19 02:06:17 +03:00
|
|
|
var Wallpaper WallpaperStruct
|
|
|
|
jsonErr := json.Unmarshal([]byte(body), &Wallpaper)
|
2022-09-18 19:28:56 +03:00
|
|
|
|
|
|
|
if jsonErr != nil {
|
|
|
|
panic(jsonErr)
|
|
|
|
}
|
|
|
|
|
2022-09-19 02:06:17 +03:00
|
|
|
if Wallpaper.Media_type == "video" {
|
|
|
|
Wallpaper.Url = Wallpaper.Url[30 : len(Wallpaper.Url)-6]
|
|
|
|
return "https://img.youtube.com/vi/" + Wallpaper.Url + "/0.jpg"
|
2022-09-18 19:28:56 +03:00
|
|
|
}
|
|
|
|
|
2022-09-19 02:06:17 +03:00
|
|
|
return Wallpaper.Hdurl
|
2022-09-18 19:28:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func SetWallpaper() {
|
|
|
|
err := wallpaper.SetFromURL(GetWallpaper())
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func StartWallpaper() {
|
|
|
|
SetWallpaper()
|
|
|
|
times := time.Now()
|
|
|
|
t := time.Date(times.Year(), times.Month(), times.Day(), 4, 50, times.Second(), times.Nanosecond(), time.UTC)
|
|
|
|
err := gocron.Every(1).Day().From(&t).Do(SetWallpaper)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|