devianter/misc.go

129 lines
3.5 KiB
Go
Raw Normal View History

2024-06-03 21:50:30 +00:00
package devianter
import (
"errors"
"log"
"math"
"net/url"
"strconv"
2024-06-03 21:50:30 +00:00
"strings"
)
/* AVATARS AND EMOJIS */
func AEmedia(name string, t rune) (string, error) {
if len(name) < 2 {
return "", errors.New("name must be specified")
}
2024-06-03 21:50:30 +00:00
// список всех возможных расширений
var extensions = [3]string{
".jpg",
".png",
".gif",
}
// надо
name = strings.ToLower(name)
// построение ссылок. билдер потому что он быстрее обычного сложения строк.
var b strings.Builder
switch t {
case 'a':
b.WriteString("https://a.deviantart.net/avatars-big/")
name_without_dashes := strings.ReplaceAll(name, "-", "_")
b.WriteString(name_without_dashes[:1])
2024-06-03 21:50:30 +00:00
b.WriteString("/")
b.WriteString(name_without_dashes[1:2])
2024-06-03 21:50:30 +00:00
b.WriteString("/")
case 'e':
b.WriteString("https://e.deviantart.net/emoticons/")
b.WriteString(name[:1])
b.WriteString("/")
default:
log.Fatalln("Invalid type.\n- 'a' -- avatar;\n- 'e' -- emoji.")
}
b.WriteString(name)
// проверка ссылки на доступность
for x := 0; x < len(extensions); x++ {
req := request(b.String() + extensions[x])
if req.Status == 200 {
return req.Body, nil
}
}
2024-06-27 11:52:33 +00:00
return "", errors.New("user not exists")
2024-06-03 21:50:30 +00:00
}
2024-06-14 17:05:21 +00:00
/* DAILY DEVIATIONS */
type DailyDeviations struct {
HasMore bool
Strips []struct {
Codename, Title string
TitleType string
Deviations []Deviation
}
Deviations []Deviation
}
func GetDailyDeviations(page int) (dd DailyDeviations) {
2024-06-14 17:05:21 +00:00
ujson("dabrowse/networkbar/rfy/deviations?page="+strconv.Itoa(page), &dd)
return
}
2024-06-03 21:50:30 +00:00
/* SEARCH */
2024-06-13 21:05:21 +00:00
type Search struct {
2024-06-30 11:39:11 +00:00
Total int `json:"estTotal"`
Pages int // only for 'a' and 'g' scope.
HasMore bool
Results []Deviation `json:"deviations"`
ResultsGalleryTemp []Deviation `json:"results"`
2024-06-03 21:50:30 +00:00
}
func PerformSearch(query string, page int, scope rune, user ...string) (ss Search, e error) {
var buildurl strings.Builder
e = nil
2024-06-03 21:50:30 +00:00
// о5 построение ссылок.
switch scope {
case 'a': // поиск артов по названию
buildurl.WriteString("dabrowse/search/all?q=")
case 't': // поиск артов по тегам
buildurl.WriteString("dabrowse/networkbar/tag/deviations?tag=")
case 'g': // поиск артов пользователя или группы
if user != nil {
buildurl.WriteString("dashared/gallection/search?username=")
buildurl.WriteString(user[0])
buildurl.WriteString("&type=gallery&order=most-recent&init=true&limit=50&q=")
} else {
2024-06-27 11:52:33 +00:00
e = errors.New("missing username (last argument)")
return
}
2024-06-03 21:50:30 +00:00
default:
log.Fatalln("Invalid type.\n- 'a' -- all;\n- 't' -- tag;\n- 'g' - gallery.")
2024-06-03 21:50:30 +00:00
}
buildurl.WriteString(url.QueryEscape(query))
if scope != 'g' { // если область поиска не равна поиску по группам, то активируется этот код
buildurl.WriteString("&page=")
} else { // иначе вместо страницы будет оффсет и страница умножится на 50
buildurl.WriteString("&offset=")
page = 50 * page
}
buildurl.WriteString(strconv.Itoa(page))
2024-06-03 21:50:30 +00:00
ujson(buildurl.String(), &ss)
2024-06-03 21:50:30 +00:00
2024-06-30 11:39:11 +00:00
if scope == 'g' {
ss.Results = ss.ResultsGalleryTemp
}
2024-06-03 21:50:30 +00:00
// расчёт, сколько всего страниц по запросу. без токена 417 страниц - максимум
2024-06-13 21:05:21 +00:00
totalfloat := int(math.Round(float64(ss.Total / 25)))
for x := 0; x < totalfloat; x++ {
2024-06-03 21:50:30 +00:00
if x <= 417 {
ss.Pages = x
}
}
return
}