diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..fa75212 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,16 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Launch Package", + "type": "go", + "request": "launch", + "mode": "debug", + "program": "${fileDirname}", + "showLog": true + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..636b799 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "go.toolsEnvVars": { + "GOROOT": "" + } +} \ No newline at end of file diff --git a/README.md b/README.md index e85470a..1c2e10f 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ # Devianter -[![Please don't upload to GitHub](https://nogithub.codeberg.page/badge.svg)](https://nogithub.codeberg.page) [![Go Reference](https://pkg.go.dev/badge/git.macaw.me/skunky/devianter.svg)](https://pkg.go.dev/git.macaw.me/skunky/devianter) +[![Please don't upload to GitHub](https://nogithub.codeberg.page/badge.svg)](https://nogithub.codeberg.page) [![Go Reference](https://pkg.go.dev/badge/git.macaw.me/skunky/devianter.svg)](https://pkg.go.dev/git.macaw.me/skunky/devianter) [![License: AGPL v3](https://img.shields.io/badge/License-AGPL%20v3-blue.svg)](https://www.gnu.org/licenses/agpl-3.0) -A DeviantART guest API library for Go. +A DeviantART API library for Go. I'll probably write up some documentation, but more on that later. diff --git a/comments.go b/comments.go index 24d2d53..928de44 100644 --- a/comments.go +++ b/comments.go @@ -11,7 +11,7 @@ type Thread struct { ID int `json:"commentId"` Parent int `json:"parentId"` - Posted timeStamp + Posted time Author bool `json:"isAuthorHighlited"` Desctiption string @@ -35,7 +35,7 @@ type Comments struct { } // 1 - комментарии поста; 4 - комментарии на стене группы или пользователя -func GetComments(postid string, cursor string, page int, typ int) (cmmts Comments) { +func CommentsFunc(postid string, cursor string, page int, typ int) (cmmts Comments) { for x := 0; x <= page; x++ { ujson( "dashared/comments/thread?typeid="+strconv.Itoa(typ)+ @@ -60,7 +60,7 @@ func GetComments(postid string, cursor string, page int, typ int) (cmmts Comment } e := json.Unmarshal([]byte(m), &content) - try(e) + err(e) for _, a := range content.Blocks { cmmts.Thread[i].Comment = a.Text diff --git a/deviantion.go b/deviantion.go index f2a85e6..2f9410f 100644 --- a/deviantion.go +++ b/deviantion.go @@ -4,26 +4,26 @@ import ( "encoding/json" "strconv" "strings" - "time" + timelib "time" ) // хрень для парсинга времени публикации -type timeStamp struct { - time.Time +type time struct { + timelib.Time } -func (t *timeStamp) UnmarshalJSON(b []byte) (err error) { +func (t *time) UnmarshalJSON(b []byte) (err error) { if b[0] == '"' && b[len(b)-1] == '"' { b = b[1 : len(b)-1] } - t.Time, err = time.Parse("2006-01-02T15:04:05-0700", string(b)) + t.Time, err = timelib.Parse("2006-01-02T15:04:05-0700", string(b)) return } // самая главная структура для поста type Deviation struct { Title, Url, License string - PublishedTime timeStamp + PublishedTime time ID int `json:"deviationId"` NSFW bool `json:"isMature"` @@ -82,7 +82,7 @@ type Post struct { ParsedComments []struct { Author string - Posted timeStamp + Posted time Replies, Likes int } @@ -90,25 +90,12 @@ type Post struct { } // преобразование урла в правильный -func UrlFromMedia(m Media, thumb ...int) string { +func UrlFromMedia(m Media) string { var url strings.Builder - - subtractWidthHeight := func(to int, target ...*int) { - for i, l := 0, len(target); i < l; i++ { - for x := *target[i]; x > to; x -= to { - *target[i] = x - } - } - } - for _, t := range m.Types { if t.T == "fullview" { url.WriteString(m.BaseUri) if m.BaseUri[len(m.BaseUri)-3:] != "gif" && t.W*t.H < 33177600 { - if len(thumb) != 0 { - subtractWidthHeight(thumb[0], &t.W, &t.H) - } - url.WriteString("/v1/fit/w_") url.WriteString(strconv.Itoa(t.W)) url.WriteString(",h_") @@ -123,12 +110,11 @@ func UrlFromMedia(m Media, thumb ...int) string { } } } - return url.String() } // для работы функции нужно ID поста и имя пользователя. -func GetDeviation(id string, user string) Post { +func DeviationFunc(id string, user string) Post { var st Post ujson( "dadeviation/init?deviationid="+id+"&username="+user+"&type=art&include_session=false&expand=deviation.related&preload=true", diff --git a/misc.go b/misc.go index 094fe2e..654ed5e 100644 --- a/misc.go +++ b/misc.go @@ -4,16 +4,13 @@ import ( "errors" "log" "math" - "net/url" + u "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", @@ -28,10 +25,9 @@ func AEmedia(name string, t rune) (string, error) { 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(name[:1]) b.WriteString("/") - b.WriteString(name_without_dashes[1:2]) + b.WriteString(name[1:2]) b.WriteString("/") case 'e': b.WriteString("https://e.deviantart.net/emoticons/") @@ -64,7 +60,7 @@ type DailyDeviations struct { Deviations []Deviation } -func GetDailyDeviations(page int) (dd DailyDeviations) { +func DailyDeviationsFunc(page int) (dd DailyDeviations) { ujson("dabrowse/networkbar/rfy/deviations?page="+strconv.Itoa(page), &dd) return } @@ -78,21 +74,23 @@ type Search struct { ResultsGalleryTemp []Deviation `json:"results"` } -func PerformSearch(query string, page int, scope rune, user ...string) (ss Search, e error) { - var buildurl strings.Builder +func SearchFunc(query string, page int, scope rune, user ...string) (ss Search, e error) { + var url strings.Builder e = nil // о5 построение ссылок. switch scope { case 'a': // поиск артов по названию - buildurl.WriteString("dabrowse/search/all?q=") + url.WriteString("dabrowse/search/all?q=") case 't': // поиск артов по тегам - buildurl.WriteString("dabrowse/networkbar/tag/deviations?tag=") + url.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=") + 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 { e = errors.New("missing username (last argument)") return @@ -101,16 +99,16 @@ func PerformSearch(query string, page int, scope rune, user ...string) (ss Searc log.Fatalln("Invalid type.\n- 'a' -- all;\n- 't' -- tag;\n- 'g' - gallery.") } - buildurl.WriteString(url.QueryEscape(query)) + url.WriteString(u.QueryEscape(query)) if scope != 'g' { // если область поиска не равна поиску по группам, то активируется этот код - buildurl.WriteString("&page=") + url.WriteString("&page=") } else { // иначе вместо страницы будет оффсет и страница умножится на 50 - buildurl.WriteString("&offset=") + url.WriteString("&offset=") page = 50 * page } - buildurl.WriteString(strconv.Itoa(page)) + url.WriteString(strconv.Itoa(page)) - ujson(buildurl.String(), &ss) + ujson(url.String(), &ss) if scope == 'g' { ss.Results = ss.ResultsGalleryTemp diff --git a/todo.md b/todo.md new file mode 100644 index 0000000..db45d13 --- /dev/null +++ b/todo.md @@ -0,0 +1,2 @@ +- groups +- images in comments \ No newline at end of file diff --git a/user-group.go b/user-group.go index c6b53ee..1592568 100644 --- a/user-group.go +++ b/user-group.go @@ -1,12 +1,44 @@ package devianter import ( - "errors" "strconv" "strings" ) // структура группы или пользователя +type GroupAbout struct { + FoundatedAt time `json:"foundationTs"` + Description Text +} +type GroupAdmins struct { + Results []struct { + TypeId int + User struct { + Username string + } + } +} + +type About struct { + Country, Website, WebsiteLabel, Gender string + RegDate int64 `json:"deviantFor"` + Description Text `json:"textContent"` + + SocialLinks []struct { + Value string + } + Interests []struct { + Label, Value string + } +} + +type users struct { + About About + CoverDeviation struct { + Deviation Deviation `json:"coverDeviation"` + } +} + type GRuser struct { ErrorDescription string Owner struct { @@ -74,21 +106,14 @@ type Group struct { } // подходит как группа, так и пользователь -func (s Group) GetGroup() (g GRuser, err error) { - if s.Name == "" { - return g, errors.New("missing Name field") - } +func (s Group) GroupFunc() (g GRuser) { ujson("dauserprofile/init/about?username="+s.Name, &g) return } // гарелея пользователя или группы -func (s Group) GetGallery(page int, folderid ...int) (g Group, err error) { - if s.Name == "" { - return g, errors.New("missing Name field") - } - +func (s Group) Gallery(page int, folderid ...int) (g Group) { var url strings.Builder if folderid[0] > 0 { page-- @@ -112,36 +137,3 @@ func (s Group) GetGallery(page int, folderid ...int) (g Group, err error) { ujson(url.String(), &g.Content) return } - -type GroupAbout struct { - FoundatedAt timeStamp `json:"foundationTs"` - Description Text -} -type GroupAdmins struct { - Results []struct { - TypeId int - User struct { - Username string - } - } -} - -type About struct { - Country, Website, WebsiteLabel, Gender string - RegDate int64 `json:"deviantFor"` - Description Text `json:"textContent"` - - SocialLinks []struct { - Value string - } - Interests []struct { - Label, Value string - } -} - -type users struct { - About About - CoverDeviation struct { - Deviation Deviation `json:"coverDeviation"` - } -} diff --git a/util.go b/util.go index fb9a73d..b98aa1f 100644 --- a/util.go +++ b/util.go @@ -9,7 +9,7 @@ import ( ) // функция для высера ошибки в stderr -func try(txt error) { +func err(txt error) { if txt != nil { println(txt.Error()) } @@ -17,9 +17,11 @@ func try(txt error) { // сокращение для вызова щенка и парсинга жсона func ujson(data string, output any) { - input, err := puppy(data) - try(err) - try(json.Unmarshal([]byte(input), output)) + input, e := puppy(data) + err(e) + + eee := json.Unmarshal([]byte(input), output) + err(eee) } /* REQUEST SECTION */ @@ -32,32 +34,32 @@ type reqrt struct { } // функция для совершения запроса -var UserAgent string - func request(uri string, other ...string) reqrt { var r reqrt // создаём новый запрос cli := &http.Client{} req, e := http.NewRequest("GET", uri, nil) - try(e) + err(e) req.Header.Set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:123.0) Gecko/20100101 Firefox/123.0.0") // куки и UA-шник - if UserAgent != "" { - req.Header.Set("User-Agent", UserAgent) - } - if len(other) != 0 { - req.Header.Set("Cookie", other[0]) + for num, rng := range other { + switch num { + case 1: + req.Header.Set("User-Agent", rng) + case 0: + req.Header.Set("Cookie", rng) + } } resp, e := cli.Do(req) - try(e) + err(e) defer resp.Body.Close() body, e := io.ReadAll(resp.Body) - try(e) + err(e) // заполняем структуру r.Body = string(body)