From 23ca4cd08d882cbde02ba20b3fa477ecf43be8e5 Mon Sep 17 00:00:00 2001 From: rtz12 Date: Tue, 15 Nov 2016 18:30:31 +0100 Subject: =?UTF-8?q?API=20f=C3=BCr=20Anilist-Import=20erweitert?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit diff --git a/goanilist.go b/goanilist.go index dc5d4b9..c280bb5 100644 --- a/goanilist.go +++ b/goanilist.go @@ -9,6 +9,7 @@ import ( "log" "net/http" "net/url" + "sort" "time" ) @@ -127,6 +128,39 @@ func (c *Client) User() (*UserResult, error) { return res, nil } +func (c *Client) Favourites(user int) (*FavouritesResult, error) { + if !c.IsCoupled { + return nil, ErrAnilistNotCoupled + } + if err := c.RenewToken(); err != nil { + return nil, err + } + res := &FavouritesResult{} + err := c.get(fmt.Sprintf("user/%d/favourites", user), res) + if err != nil { + return nil, err + } + if res.Characters != nil { + sort.Stable(res.Characters) + } + return res, nil +} + +func (c *Client) CharacterPage(id int) (*CharacterPageResult, error) { + if !c.IsCoupled { + return nil, ErrAnilistNotCoupled + } + if err := c.RenewToken(); err != nil { + return nil, err + } + res := &CharacterPageResult{} + err := c.get(fmt.Sprintf("character/%d/page", id), res) + if err != nil { + return nil, err + } + return res, nil +} + func (c *Client) post(path string, values url.Values, result interface{}) error { resp, err := http.PostForm(fmt.Sprintf("%s%s", API_URL, path), values) if err != nil { @@ -154,6 +188,7 @@ func (c *Client) handleResponse(resp *http.Response, path string, values url.Val return err } err = json.Unmarshal(body, result) + //log.Println(string(body)) //Praktisch zum testen von neuen API-Funktionen if err != nil { log.Println(string(body)) return err diff --git a/types.go b/types.go index e451806..5bb1bec 100644 --- a/types.go +++ b/types.go @@ -1,30 +1,49 @@ package goanilist -import "encoding/json" +import ( + "encoding/json" + "strconv" +) type APIError interface { Get() *FlexibleError } type FlexibleError struct { - RawError json.RawMessage `json:"error"` + RawError *json.RawMessage `json:"error"` Error string ErrorMessage string `json:"error_message"` } type complexError struct { - Error string `json:"error"` - ErrorDescription string `json:"error_description"` + Error string `json:"error"` + ErrorDescription string `json:"error_description"` + Status int `json:"status"` + Messages []string `json:"messages"` } func (e *FlexibleError) ParseRawError() { + if e.RawError == nil { + return + } ce := complexError{} - err := json.Unmarshal(e.RawError, &ce) + err := json.Unmarshal(*e.RawError, &ce) if err == nil { - e.Error = ce.Error - e.ErrorMessage = ce.ErrorDescription + if ce.Error != "" { + e.Error = ce.Error + e.ErrorMessage = ce.ErrorDescription + } else { + e.Error = strconv.Itoa(ce.Status) + e.ErrorMessage = "" + for k, v := range ce.Messages { + if k > 0 { + e.ErrorMessage += "\n" + } + e.ErrorMessage += v + } + } } else { - e.Error = string(e.RawError) + e.Error = string(*e.RawError) } } @@ -46,3 +65,87 @@ type UserResult struct { ID int `json:"id"` DisplayName string `json:"display_name"` } + +type FavouritesResult struct { + FlexibleError + Characters CharacterResultCollection `json:"character"` +} + +type CharacterResultCollection []CharacterResult + +func (c CharacterResultCollection) Len() int { + return len(c) +} + +func (c CharacterResultCollection) Less(i, j int) bool { + return c[i].Order < c[j].Order +} + +func (c CharacterResultCollection) Swap(i, j int) { + c[i], c[j] = c[j], c[i] +} + +type CharacterResult struct { + FlexibleError + ID int `json:"id"` + GivenName string `json:"name_first"` + FamilyName string `json:"name_last"` + JapaneseName string `json:"name_japanese"` + AlternativeName string `json:"name_alt"` + Info string `json:"info"` + Role string `json:"role"` + ImageURL string `json:"image_url_lge"` + ThumbURL string `json:"image_url_med"` + Order int `json:"order"` +} + +type CharacterPageResult struct { + FlexibleError + CharacterResult + Anime []AnimeResult `json:"anime"` + Manga []MangaResult `json:"manga"` +} + +type SeriesResult struct { + FlexibleError + ID int `json:"id"` + SeriesType string `json:"series_type"` + Type string `json:"type"` + TitleRomaji string `json:"title_romaji"` + TitleEnglish string `json:"title_english"` + TitleJapanese string `json:"title_japanese"` + Synonyms []string `json:"synonyms"` + Description string `json:"description"` + StartDate int64 `json:"start_date_fuzzy"` + EndDate int64 `json:"end_date_fuzzy"` + Season int `json:"season"` + Genres []string `json:"genres"` + Adult bool `json:"adult"` + AverageScore float32 `json:"average_score"` + Popularity int `json:"popularity"` + Favourite bool `json:"favourite"` + ImageURL string `json:"image_url_lge"` + ThumbSmallURL string `json:"image_url_sml"` + ThumbBigURL string `json:"image_url_med"` + BannerURL string `json:"image_url_banner"` + UpdatedAt int64 `json:"updated_at"` +} + +type AnimeResult struct { + FlexibleError + SeriesResult + TotalEpisodes int `json:"total_episodes"` + EpisodeDuration int `json:"duration"` + AiringStatus string `json:"airing_status"` + YoutubeID string `json:"youtube_id"` + Hashtag string `json:"hashtag"` + Source string `json:"source"` +} + +type MangaResult struct { + FlexibleError + SeriesResult + TotalChapters int `json:"total_chapters"` + TotalVolumes int `json:"total_volumes"` + PublishingStatus string `json:"publishing_status"` +} -- cgit v0.10.1