2024-06-03 21:50:30 +00:00
|
|
|
|
package devianter
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"log"
|
|
|
|
|
"math"
|
2024-07-29 22:20:00 +00:00
|
|
|
|
"net/url"
|
2024-06-05 11:32:40 +00:00
|
|
|
|
"strconv"
|
2024-06-03 21:50:30 +00:00
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
/* AVATARS AND EMOJIS */
|
|
|
|
|
func AEmedia(name string, t rune) (string, error) {
|
2024-07-29 22:20:00 +00:00
|
|
|
|
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/")
|
2024-07-29 22:20:00 +00:00
|
|
|
|
name_without_dashes := strings.ReplaceAll(name, "-", "_")
|
|
|
|
|
b.WriteString(name_without_dashes[:1])
|
2024-06-03 21:50:30 +00:00
|
|
|
|
b.WriteString("/")
|
2024-07-29 22:20:00 +00:00
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-29 22:20:00 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2024-07-29 22:20:00 +00:00
|
|
|
|
func PerformSearch(query string, page int, scope rune, user ...string) (ss Search, e error) {
|
|
|
|
|
var buildurl strings.Builder
|
2024-06-05 11:32:40 +00:00
|
|
|
|
e = nil
|
2024-06-03 21:50:30 +00:00
|
|
|
|
|
|
|
|
|
// о5 построение ссылок.
|
|
|
|
|
switch scope {
|
2024-06-05 11:32:40 +00:00
|
|
|
|
case 'a': // поиск артов по названию
|
2024-07-29 22:20:00 +00:00
|
|
|
|
buildurl.WriteString("dabrowse/search/all?q=")
|
2024-06-05 11:32:40 +00:00
|
|
|
|
case 't': // поиск артов по тегам
|
2024-07-29 22:20:00 +00:00
|
|
|
|
buildurl.WriteString("dabrowse/networkbar/tag/deviations?tag=")
|
2024-08-14 16:18:40 +00:00
|
|
|
|
case 'g', 'f': // поиск артов пользователя или группы
|
|
|
|
|
if user == nil {
|
2024-06-27 11:52:33 +00:00
|
|
|
|
e = errors.New("missing username (last argument)")
|
2024-06-05 11:32:40 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
2024-08-14 16:18:40 +00:00
|
|
|
|
|
|
|
|
|
buildurl.WriteString("dashared/gallection/search?username=")
|
|
|
|
|
buildurl.WriteString(user[0])
|
|
|
|
|
buildurl.WriteString("&type=")
|
|
|
|
|
if scope == 'g' {
|
|
|
|
|
buildurl.WriteString("gallery")
|
|
|
|
|
} else {
|
|
|
|
|
buildurl.WriteString("collection")
|
|
|
|
|
}
|
|
|
|
|
buildurl.WriteString("&order=most-recent&init=true&limit=50&q=")
|
2024-06-03 21:50:30 +00:00
|
|
|
|
default:
|
2024-08-14 16:18:40 +00:00
|
|
|
|
log.Fatalln("Invalid type.\n- 'a' -- all;\n- 't' -- tag;\n- 'g' - gallery\n- 'f' - folders.")
|
2024-06-03 21:50:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-07-29 22:20:00 +00:00
|
|
|
|
buildurl.WriteString(url.QueryEscape(query))
|
2024-06-05 11:32:40 +00:00
|
|
|
|
if scope != 'g' { // если область поиска не равна поиску по группам, то активируется этот код
|
2024-07-29 22:20:00 +00:00
|
|
|
|
buildurl.WriteString("&page=")
|
2024-06-05 11:32:40 +00:00
|
|
|
|
} else { // иначе вместо страницы будет оффсет и страница умножится на 50
|
2024-07-29 22:20:00 +00:00
|
|
|
|
buildurl.WriteString("&offset=")
|
2024-06-05 11:32:40 +00:00
|
|
|
|
page = 50 * page
|
|
|
|
|
}
|
2024-07-29 22:20:00 +00:00
|
|
|
|
buildurl.WriteString(strconv.Itoa(page))
|
2024-06-03 21:50:30 +00:00
|
|
|
|
|
2024-07-29 22:20:00 +00:00
|
|
|
|
ujson(buildurl.String(), &ss)
|
2024-06-03 21:50:30 +00:00
|
|
|
|
|
2024-08-14 16:18:40 +00:00
|
|
|
|
if ss.Results == nil {
|
2024-06-30 11:39:11 +00:00
|
|
|
|
ss.Results = ss.ResultsGalleryTemp
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-14 16:18:40 +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
|
|
|
|
|
}
|