2024-06-03 21:50:30 +00:00
|
|
|
|
package devianter
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"errors"
|
|
|
|
|
"io"
|
|
|
|
|
"log"
|
|
|
|
|
"math"
|
|
|
|
|
"net/http"
|
2024-06-30 11:39:11 +00:00
|
|
|
|
u "net/url"
|
2024-06-05 11:32:40 +00:00
|
|
|
|
"strconv"
|
2024-06-03 21:50:30 +00:00
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// функция для высера ошибки в stderr
|
|
|
|
|
func err(txt error) {
|
|
|
|
|
if txt != nil {
|
|
|
|
|
println(txt.Error())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// сокращение для вызова щенка и парсинга жсона
|
|
|
|
|
func ujson(data string, output any) {
|
|
|
|
|
input, e := puppy(data)
|
|
|
|
|
err(e)
|
|
|
|
|
|
|
|
|
|
eee := json.Unmarshal([]byte(input), output)
|
|
|
|
|
err(eee)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* REQUEST SECTION */
|
|
|
|
|
// структура для ответа сервера
|
|
|
|
|
type reqrt struct {
|
|
|
|
|
Body string
|
|
|
|
|
Status int
|
|
|
|
|
Cookies []*http.Cookie
|
|
|
|
|
Headers http.Header
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// функция для совершения запроса
|
|
|
|
|
func request(uri string, other ...string) reqrt {
|
|
|
|
|
var r reqrt
|
|
|
|
|
|
|
|
|
|
// создаём новый запрос
|
|
|
|
|
cli := &http.Client{}
|
|
|
|
|
req, e := http.NewRequest("GET", uri, nil)
|
|
|
|
|
err(e)
|
|
|
|
|
|
|
|
|
|
req.Header.Set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:123.0) Gecko/20100101 Firefox/123.0.0")
|
|
|
|
|
|
|
|
|
|
// куки и UA-шник
|
2024-06-27 11:52:33 +00:00
|
|
|
|
for num, rng := range other {
|
|
|
|
|
switch num {
|
|
|
|
|
case 1:
|
|
|
|
|
req.Header.Set("User-Agent", rng)
|
|
|
|
|
case 0:
|
|
|
|
|
req.Header.Set("Cookie", rng)
|
2024-06-03 21:50:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resp, e := cli.Do(req)
|
|
|
|
|
err(e)
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
|
|
body, e := io.ReadAll(resp.Body)
|
|
|
|
|
err(e)
|
|
|
|
|
|
|
|
|
|
// заполняем структуру
|
|
|
|
|
r.Body = string(body)
|
|
|
|
|
r.Cookies = resp.Cookies()
|
|
|
|
|
r.Headers = resp.Header
|
|
|
|
|
r.Status = resp.StatusCode
|
|
|
|
|
|
|
|
|
|
return r
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* AVATARS AND EMOJIS */
|
|
|
|
|
func AEmedia(name string, t rune) (string, error) {
|
|
|
|
|
// список всех возможных расширений
|
|
|
|
|
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/")
|
|
|
|
|
b.WriteString(name[:1])
|
|
|
|
|
b.WriteString("/")
|
|
|
|
|
b.WriteString(name[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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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 DailyDeviationsFunc(page int) (dd DailyDeviations) {
|
|
|
|
|
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-06-13 21:05:21 +00:00
|
|
|
|
func SearchFunc(query string, page int, scope rune, user ...string) (ss Search, e error) {
|
2024-06-03 21:50:30 +00:00
|
|
|
|
var url 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-06-03 21:50:30 +00:00
|
|
|
|
url.WriteString("dabrowse/search/all?q=")
|
2024-06-05 11:32:40 +00:00
|
|
|
|
case 't': // поиск артов по тегам
|
2024-06-03 21:50:30 +00:00
|
|
|
|
url.WriteString("dabrowse/networkbar/tag/deviations?tag=")
|
2024-06-05 11:32:40 +00:00
|
|
|
|
case 'g': // поиск артов пользователя или группы
|
|
|
|
|
if user != nil {
|
|
|
|
|
url.WriteString("dashared/gallection/search?username=")
|
|
|
|
|
for _, a := range user {
|
|
|
|
|
url.WriteString(a)
|
|
|
|
|
}
|
|
|
|
|
url.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)")
|
2024-06-05 11:32:40 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
2024-06-03 21:50:30 +00:00
|
|
|
|
default:
|
2024-06-05 11:32:40 +00:00
|
|
|
|
log.Fatalln("Invalid type.\n- 'a' -- all;\n- 't' -- tag;\n- 'g' - gallery.")
|
2024-06-03 21:50:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-30 11:39:11 +00:00
|
|
|
|
url.WriteString(u.QueryEscape(query))
|
2024-06-05 11:32:40 +00:00
|
|
|
|
if scope != 'g' { // если область поиска не равна поиску по группам, то активируется этот код
|
|
|
|
|
url.WriteString("&page=")
|
|
|
|
|
} else { // иначе вместо страницы будет оффсет и страница умножится на 50
|
|
|
|
|
url.WriteString("&offset=")
|
|
|
|
|
page = 50 * page
|
|
|
|
|
}
|
|
|
|
|
url.WriteString(strconv.Itoa(page))
|
2024-06-03 21:50:30 +00:00
|
|
|
|
|
|
|
|
|
ujson(url.String(), &ss)
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* PUPPY aka DeviantArt API */
|
2024-06-14 17:05:21 +00:00
|
|
|
|
// получение или обновление токена
|
|
|
|
|
var cookie string
|
|
|
|
|
var token string
|
2024-06-03 21:50:30 +00:00
|
|
|
|
|
2024-06-14 17:05:21 +00:00
|
|
|
|
func UpdateCSRF() error {
|
|
|
|
|
if cookie == "" {
|
|
|
|
|
req := request("https://www.deviantart.com/_puppy")
|
2024-06-03 21:50:30 +00:00
|
|
|
|
|
2024-06-14 17:05:21 +00:00
|
|
|
|
for _, content := range req.Cookies {
|
|
|
|
|
cookie = content.Raw
|
|
|
|
|
}
|
2024-06-03 21:50:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-14 17:05:21 +00:00
|
|
|
|
req := request("https://www.deviantart.com", cookie)
|
|
|
|
|
if req.Status != 200 {
|
|
|
|
|
return errors.New(req.Body)
|
2024-06-03 21:50:30 +00:00
|
|
|
|
}
|
2024-06-14 17:05:21 +00:00
|
|
|
|
token = req.Body[strings.Index(req.Body, "window.__CSRF_TOKEN__ = '")+25 : strings.Index(req.Body, "window.__XHR_LOCAL__")-3]
|
2024-06-03 21:50:30 +00:00
|
|
|
|
|
2024-06-14 17:05:21 +00:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func puppy(data string) (string, error) {
|
2024-06-05 11:32:40 +00:00
|
|
|
|
var url strings.Builder
|
|
|
|
|
url.WriteString("https://www.deviantart.com/_puppy/")
|
|
|
|
|
url.WriteString(data)
|
|
|
|
|
url.WriteString("&csrf_token=")
|
|
|
|
|
url.WriteString(token)
|
|
|
|
|
url.WriteString("&da_minor_version=20230710")
|
|
|
|
|
|
|
|
|
|
body := request(url.String(), cookie)
|
2024-06-03 21:50:30 +00:00
|
|
|
|
|
|
|
|
|
// если код ответа не 200, возвращается ошибка
|
|
|
|
|
if body.Status != 200 {
|
|
|
|
|
return "", errors.New(body.Body)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return body.Body, nil
|
|
|
|
|
}
|