mirror of
https://github.com/Starlio-app/StarlioX
synced 2024-11-22 08:46:22 +03:00
add other functions
This commit is contained in:
parent
41a46c7c6c
commit
1370774f6d
3 changed files with 163 additions and 0 deletions
75
api/controllers/settings.go
Normal file
75
api/controllers/settings.go
Normal file
|
@ -0,0 +1,75 @@
|
|||
package controllers
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"net/http"
|
||||
|
||||
"github.com/Redume/EveryNasa/api/utils"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
var SettingsGet = func(w http.ResponseWriter, r *http.Request) {
|
||||
db, errOpen := sql.Open("sqlite3", "EveryNasa.db")
|
||||
if errOpen != nil {
|
||||
panic(errOpen)
|
||||
}
|
||||
|
||||
query, errQuery := db.Query("SELECT * FROM settings")
|
||||
if errQuery != nil {
|
||||
panic(errQuery)
|
||||
}
|
||||
|
||||
defer query.Close()
|
||||
|
||||
var lang string
|
||||
var autostart int
|
||||
var autoupdate int
|
||||
|
||||
for query.Next() {
|
||||
err := query.Scan(&lang, &autostart, &autoupdate)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
var data = map[string]interface{}{"lang": lang, "autostart": autostart, "autoupdate": autoupdate}
|
||||
utils.Respond(w, data)
|
||||
}
|
||||
}
|
||||
|
||||
var SettingsUpdate = func(w http.ResponseWriter, r *http.Request) {
|
||||
db, errOpen := sql.Open("sqlite3", "EveryNasa.db")
|
||||
if errOpen != nil {
|
||||
panic(errOpen)
|
||||
}
|
||||
|
||||
lang := r.FormValue("lang")
|
||||
autostart := r.FormValue("autostart")
|
||||
autoupdate := r.FormValue("autoupdate")
|
||||
|
||||
if lang == "" && autostart == "" && autoupdate == "" {
|
||||
utils.Respond(w, utils.Message(false, "All fields are required"))
|
||||
return
|
||||
}
|
||||
|
||||
if lang != "" {
|
||||
_, err := db.Exec("UPDATE settings SET lang = ?", lang)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
if autostart != "" {
|
||||
_, err := db.Exec("UPDATE settings SET autostart = ?", autostart)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
if autoupdate != "" {
|
||||
_, err := db.Exec("UPDATE settings SET autoupdate = ?", autoupdate)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
utils.Respond(w, utils.Message(true, "Settings updated"))
|
||||
}
|
15
api/utils/utils.go
Normal file
15
api/utils/utils.go
Normal file
|
@ -0,0 +1,15 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func Message(status bool, message string) map[string]interface{} {
|
||||
return map[string]interface{}{"status": status, "message": message}
|
||||
}
|
||||
|
||||
func Respond(w http.ResponseWriter, data map[string]interface{}) {
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(data)
|
||||
}
|
73
functions/wallpaper.go
Normal file
73
functions/wallpaper.go
Normal file
|
@ -0,0 +1,73 @@
|
|||
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)
|
||||
}
|
||||
|
||||
type Wallpaper struct {
|
||||
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)
|
||||
}
|
||||
|
||||
var wallpaper Wallpaper
|
||||
jsonErr := json.Unmarshal([]byte(body), &wallpaper)
|
||||
|
||||
if jsonErr != nil {
|
||||
panic(jsonErr)
|
||||
}
|
||||
|
||||
if wallpaper.Media_type == "video" {
|
||||
wallpaper.Url = wallpaper.Url[30 : len(wallpaper.Url)-6]
|
||||
|
||||
return "https://img.youtube.com/vi/" + wallpaper.Url + "/0.jpg"
|
||||
}
|
||||
|
||||
return wallpaper.Hdurl
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue