StarlioX/functions/wallpaper.go

110 lines
2.1 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
"time"
2022-09-18 19:28:56 +03:00
"github.com/jasonlvhit/gocron"
"github.com/reujab/wallpaper"
"github.com/rodkranz/fetch"
)
2022-10-23 15:32:35 +03:00
func GetWallpaper(date string) string {
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-10-23 15:32:35 +03:00
res, err := client.Get("https://api.nasa.gov/planetary/apod?api_key=1gI9G84ZafKDEnrbydviGknReOGiVK9jqrQBE3et&date="+
date,
nil)
2022-09-25 11:04:39 +03:00
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" {
2022-10-23 15:32:35 +03:00
Wallpaper.Url = "https://img.youtube.com/vi/" + Wallpaper.Url[30:41] + "/maxresdefault.jpg"
return "https://img.youtube.com/vi/" + Wallpaper.Url[30:41] + "/maxresdefault.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() {
2022-10-23 15:32:35 +03:00
times := time.Now()
t := time.Date(times.Year(),
times.Month(),
times.Day(),
times.Hour(),
times.Minute(),
times.Second(),
times.Nanosecond(),
time.UTC)
err := wallpaper.SetFromURL(GetWallpaper(t.Format("2006-01-02")))
2022-09-18 19:28:56 +03:00
if err != nil {
2022-10-23 15:32:35 +03:00
t = t.AddDate(0, 0, -1)
err = wallpaper.SetFromURL(GetWallpaper(t.Format("2006-01-02")))
if err != nil {
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-10-23 15:32:35 +03:00
res, err := client.Get("http://localhost:3000/api/get/settings", nil)
2022-09-25 11:04:39 +03:00
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()
2022-10-23 15:32:35 +03:00
t := time.Date(times.Year(),
times.Month(),
times.Day(),
4,
50,
times.Second(),
times.Nanosecond(),
time.UTC)
2022-09-19 16:55:29 +03:00
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
}
}