krz/devianter

A DeviantArt guest API library for Go.

clone: git clone https://gitbay.org/krz/devianter.git

v0.3.1: user-group.go · raw

  1package devianter
  2
  3import (
  4	"errors"
  5	"strconv"
  6	"strings"
  7)
  8
  9// структура группы или пользователя
 10type GRuser struct {
 11	ErrorDescription string
 12	Owner            struct {
 13		Group    bool `json:"isGroup"`
 14		Username string
 15	}
 16	Gruser struct {
 17		ID   int `json:"gruserId"`
 18		Page struct {
 19			Modules []struct {
 20				Name       string
 21				ModuleData struct {
 22					GroupAbout  GroupAbout
 23					GroupAdmins GroupAdmins
 24					users
 25				}
 26			}
 27		}
 28	}
 29	Extra struct {
 30		Tag   string `json:"gruserTagline"`
 31		Stats struct {
 32			Deviations, Watchers, Watching, Pageviews, CommentsMade, Favourites, Friends int
 33			FeedComments                                                                 int `json:"commentsReceivedProfile"`
 34		}
 35	} `json:"pageExtraData"`
 36}
 37
 38type Gallery struct {
 39	Gruser struct {
 40		ID   int `json:"gruserId"`
 41		Page struct {
 42			Modules []struct {
 43				Name       string
 44				ModuleData struct {
 45					// группы
 46					Folders struct {
 47						HasMore bool
 48						Results []struct {
 49							Deviations int `json:"totalItemCount"`
 50							FolderId   int
 51							Size       int
 52							Name       string
 53							Thumb      Deviation
 54						}
 55					}
 56
 57					// галерея
 58					Folder struct {
 59						HasMore    bool
 60						Username   string
 61						Pages      int `json:"totalPageCount"`
 62						Deviations []Deviation
 63					} `json:"folderDeviations"`
 64				}
 65			}
 66		}
 67	}
 68	HasMore bool
 69	Results []Deviation
 70}
 71
 72type Group struct {
 73	Name    string // обязательно заполнить
 74	Content Gallery
 75}
 76
 77// подходит как группа, так и пользователь
 78func (s Group) Get() (g GRuser, daError Error, err error) {
 79	if s.Name == "" {
 80		return g, daError, errors.New("missing Name field")
 81	}
 82	daError = ujson("dauserprofile/init/about?username="+s.Name, &g)
 83
 84	return
 85}
 86
 87func (s Group) Favourites(page int, all bool, folderid ...int) (g Group, err Error) {
 88	var url strings.Builder
 89
 90	if fid := folderid[0]; fid > 0 || all {
 91		url.WriteString("dashared/gallection/contents")
 92		if all {
 93			url.WriteString("?all_folder=true")
 94		} else {
 95			url.WriteString("?folderid=")
 96			url.WriteString(strconv.Itoa(fid))
 97		}
 98		url.WriteString("&type=collection&")
 99	} else {
100		url.WriteString("dauserprofile/init/favourites?deviations_")
101	}
102
103	url.WriteString("limit=50&username=")
104	url.WriteString(s.Name)
105	url.WriteString("&with_subfolders=true&offset=")
106	url.WriteString(strconv.Itoa(page * 50))
107
108	err = ujson(url.String(), &g.Content)
109	return
110}
111
112// гарелея пользователя или группы
113func (s Group) Gallery(page int, folderid ...int) (g Group, daError Error, err error) {
114	if s.Name == "" {
115		return g, daError, errors.New("missing Name field")
116	}
117
118	var url strings.Builder
119	if folderid[0] > 0 {
120		page--
121		url.WriteString("dashared/gallection/contents?username=")
122		url.WriteString(s.Name)
123		url.WriteString("&folderid=")
124		url.WriteString(strconv.Itoa(folderid[0]))
125		url.WriteString("&offset=")
126		url.WriteString(strconv.Itoa(page * 50))
127		url.WriteString("&type=gallery&")
128	} else {
129		url.WriteString("dauserprofile/init/gallery?username=")
130		url.WriteString(s.Name)
131		url.WriteString("&page=")
132		url.WriteString(strconv.Itoa(page))
133		url.WriteString("&deviations_")
134	}
135	url.WriteString("limit=50")
136	url.WriteString("&with_subfolders=false")
137
138	daError = ujson(url.String(), &g.Content)
139	return
140}
141
142type GroupAbout struct {
143	FoundatedAt timeStamp `json:"foundationTs"`
144	Description Text
145}
146type GroupAdmins struct {
147	Results []struct {
148		TypeId int
149		User   struct {
150			Username string
151		}
152	}
153}
154
155type About struct {
156	Country, Website, WebsiteLabel, Gender string
157	RegDate                                int64 `json:"deviantFor"`
158	Description                            Text  `json:"textContent"`
159
160	SocialLinks []struct {
161		Value string
162	}
163	Interests []struct {
164		Label, Value string
165	}
166}
167
168type users struct {
169	About          About
170	CoverDeviation struct {
171		Deviation Deviation `json:"coverDeviation"`
172	}
173}