StarlioX/functions/wallpaper.go

100 lines
1.9 KiB
Go
Raw Normal View History

2022-09-18 19:28:56 +03:00
package functions
import (
"encoding/json"
2022-09-25 11:04:39 +03:00
"os"
"time"
2022-09-18 19:28:56 +03:00
"github.com/jasonlvhit/gocron"
"github.com/reujab/wallpaper"
"github.com/rodkranz/fetch"
"gopkg.in/yaml.v2"
)
func GetWallpaper() string {
2022-09-25 11:04:39 +03:00
file, err := os.ReadFile("config.yaml")
if err != nil {
Logger(err.Error())
2022-09-18 19:28:56 +03:00
}
data := make(map[interface{}]interface{})
2022-09-25 11:04:39 +03:00
err = yaml.Unmarshal(file, &data)
if err != nil {
Logger(err.Error())
2022-09-18 19:28:56 +03:00
}
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()
2022-09-25 11:04:39 +03:00
res, err := client.Get("https://api.nasa.gov/planetary/apod?api_key="+data["apiKey"].(string), nil)
if err != nil {
Logger(err.Error())
2022-09-18 19:28:56 +03:00
}
2022-09-25 11:04:39 +03:00
body, err := res.ToString()
if err != nil {
Logger(err.Error())
2022-09-18 19:28:56 +03:00
}
2022-09-19 02:06:17 +03:00
var Wallpaper WallpaperStruct
2022-09-25 11:04:39 +03:00
err = json.Unmarshal([]byte(body), &Wallpaper)
if err != nil {
Logger(err.Error())
2022-09-18 19:28:56 +03:00
}
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 {
2022-09-25 11:04:39 +03:00
Logger(err.Error())
2022-09-18 19:28:56 +03:00
}
}
func StartWallpaper() {
2022-09-19 16:55:29 +03:00
type Autostart struct {
Wallpaper int `json:"wallpaper"`
2022-09-19 16:55:29 +03:00
}
client := fetch.NewDefault()
2022-09-25 11:04:39 +03:00
res, err := client.Get("http://localhost:8080/api/get/settings", nil)
if err != nil {
Logger(err.Error())
2022-09-19 16:55:29 +03:00
}
2022-09-25 11:04:39 +03:00
body, err := res.ToString()
if err != nil {
Logger(err.Error())
2022-09-19 16:55:29 +03:00
}
var AutostartSetWallpaper Autostart
2022-09-25 11:04:39 +03:00
err = json.Unmarshal([]byte(body), &AutostartSetWallpaper)
if err != nil {
Logger(err.Error())
2022-09-19 16:55:29 +03:00
}
if AutostartSetWallpaper.Wallpaper == 1 {
2022-09-19 16:55:29 +03:00
times := time.Now()
t := time.Date(times.Year(), times.Month(), times.Day(), 4, 50, times.Second(), times.Nanosecond(), time.UTC)
SetWallpaper()
2022-09-25 11:04:39 +03:00
err = gocron.Every(1).Day().From(&t).Do(SetWallpaper)
2022-09-19 16:55:29 +03:00
if err != nil {
2022-09-25 11:04:39 +03:00
Logger(err.Error())
2022-09-19 16:55:29 +03:00
}
2022-09-25 11:04:39 +03:00
2022-09-19 16:55:29 +03:00
gocron.Start()
2022-09-18 19:28:56 +03:00
}
}