aboutsummaryrefslogtreecommitdiff
path: root/misc.go
blob: 094fe2ebbcde2946d98f26ed94a0262d55400dfe (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package devianter

import (
	"errors"
	"log"
	"math"
	"net/url"
	"strconv"
	"strings"
)

/* AVATARS AND EMOJIS */
func AEmedia(name string, t rune) (string, error) {
	if len(name) < 2 {
		return "", errors.New("name must be specified")
	}
	// список всех возможных расширений
	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])
		b.WriteString("/")
		b.WriteString(name_without_dashes[1:2])
		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
		}
	}

	return "", errors.New("user not exists")
}

/* DAILY DEVIATIONS */
type DailyDeviations struct {
	HasMore bool
	Strips  []struct {
		Codename, Title string
		TitleType       string
		Deviations      []Deviation
	}
	Deviations []Deviation
}

func GetDailyDeviations(page int) (dd DailyDeviations) {
	ujson("dabrowse/networkbar/rfy/deviations?page="+strconv.Itoa(page), &dd)
	return
}

/* SEARCH */
type Search struct {
	Total              int `json:"estTotal"`
	Pages              int // only for 'a' and 'g' scope.
	HasMore            bool
	Results            []Deviation `json:"deviations"`
	ResultsGalleryTemp []Deviation `json:"results"`
}

func PerformSearch(query string, page int, scope rune, user ...string) (ss Search, e error) {
	var buildurl strings.Builder
	e = nil

	// о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 {
			e = errors.New("missing username (last argument)")
			return
		}
	default:
		log.Fatalln("Invalid type.\n- 'a' -- all;\n- 't' -- tag;\n- 'g' - gallery.")
	}

	buildurl.WriteString(url.QueryEscape(query))
	if scope != 'g' { // если область поиска не равна поиску по группам, то активируется этот код
		buildurl.WriteString("&page=")
	} else { // иначе вместо страницы будет оффсет и страница умножится на 50
		buildurl.WriteString("&offset=")
		page = 50 * page
	}
	buildurl.WriteString(strconv.Itoa(page))

	ujson(buildurl.String(), &ss)

	if scope == 'g' {
		ss.Results = ss.ResultsGalleryTemp
	}

	// расчёт, сколько всего страниц по запросу. без токена 417 страниц - максимум
	totalfloat := int(math.Round(float64(ss.Total / 25)))
	for x := 0; x < totalfloat; x++ {
		if x <= 417 {
			ss.Pages = x
		}
	}

	return
}