2024-09-02 08:16:54 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
2024-09-03 12:36:19 +00:00
|
|
|
"encoding/json"
|
2024-09-02 08:16:54 +00:00
|
|
|
"math/rand"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"git.macaw.me/skunky/devianter"
|
|
|
|
)
|
|
|
|
|
|
|
|
type API struct {
|
2024-09-23 06:38:32 +00:00
|
|
|
main *skunkyart
|
2024-09-03 12:36:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type info struct {
|
2024-09-23 06:38:32 +00:00
|
|
|
Version string `json:"version"`
|
|
|
|
Settings settingsParams `json:"settings"`
|
2024-09-03 12:36:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a API) Info() {
|
2024-09-23 06:38:32 +00:00
|
|
|
json, err := json.Marshal(info{
|
|
|
|
Version: a.main.Version,
|
|
|
|
Settings: settingsParams{
|
|
|
|
Nsfw: CFG.Nsfw,
|
|
|
|
Proxy: CFG.Proxy,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
try(err)
|
|
|
|
a.main.Writer.Write(json)
|
2024-09-03 12:36:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a API) Error(description string, status int) {
|
2024-09-23 06:38:32 +00:00
|
|
|
a.main.Writer.WriteHeader(status)
|
|
|
|
var response strings.Builder
|
|
|
|
response.WriteString(`{"error":"`)
|
|
|
|
response.WriteString(description)
|
|
|
|
response.WriteString(`"}`)
|
|
|
|
wr(a.main.Writer, response.String())
|
2024-09-02 08:16:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a API) sendMedia(d *devianter.Deviation) {
|
2024-09-23 06:38:32 +00:00
|
|
|
mediaUrl, name := devianter.UrlFromMedia(d.Media)
|
2024-09-03 12:36:19 +00:00
|
|
|
a.main.SetFilename(name)
|
2024-09-23 06:38:32 +00:00
|
|
|
|
|
|
|
if len(mediaUrl) != 0 {
|
|
|
|
mediaUrl = mediaUrl[21:]
|
2024-09-02 08:16:54 +00:00
|
|
|
dot := strings.Index(mediaUrl, ".")
|
2024-09-03 12:36:19 +00:00
|
|
|
a.main.Writer.Header().Del("Content-Type")
|
2024-09-23 06:38:32 +00:00
|
|
|
a.main.DownloadAndSendMedia(mediaUrl[:dot], mediaUrl[dot+11:])
|
|
|
|
}
|
2024-09-02 08:16:54 +00:00
|
|
|
}
|
|
|
|
|
2024-09-03 12:36:19 +00:00
|
|
|
// TODO: сделать фильтры
|
2024-09-02 08:16:54 +00:00
|
|
|
func (a API) Random() {
|
2024-09-23 06:38:32 +00:00
|
|
|
for attempt := 1; ; {
|
|
|
|
if attempt > 3 {
|
|
|
|
a.Error("Sorry, butt NSFW on this are disabled, and the instance failed to find a random art without NSFW", 500)
|
|
|
|
}
|
|
|
|
|
|
|
|
s, err, daErr := devianter.PerformSearch(string(rand.Intn(999)), rand.Intn(30), 'a')
|
|
|
|
try(err)
|
|
|
|
if daErr.RAW != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
deviation := &s.Results[rand.Intn(len(s.Results))]
|
|
|
|
|
|
|
|
if deviation.NSFW && !CFG.Nsfw {
|
|
|
|
attempt++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
a.sendMedia(deviation)
|
|
|
|
return
|
|
|
|
}
|
2024-09-02 08:16:54 +00:00
|
|
|
}
|