From ffc7d5624d46e5ce30d35700fe8eb97ee42aab9e Mon Sep 17 00:00:00 2001 From: daru Date: Sat, 16 Apr 2022 18:13:32 +0200 Subject: [PATCH] Use own struct --- klotz.go | 74 ++++++++++++++++++++++++++--------------------------- knecht.go | 30 ++++++++++++++++------ schaffer.go | 41 +++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+), 46 deletions(-) diff --git a/klotz.go b/klotz.go index ba5166f..b584d5f 100644 --- a/klotz.go +++ b/klotz.go @@ -4,48 +4,46 @@ import "time" type Anime struct { Anime int64 `json:"anime"` - Data AnimeDetail `json:"data"` Users []WatchUser `json:"users"` + Data AnimeDetail `json:"data"` } type AnimeDetail struct { - Title string `json:"title"` - TitleEn string `json:"title_en"` - TitleJp string `json:"title_jp"` - Image struct { - ImageURL string `json:"medium"` - LargeURL string `json:"large"` - ThumbURL string `json:"thumb"` - } `json:"image"` - Type string `json:"type"` - Status string `json:"status"` - Episodes int `json:"episodes"` - Synopsis string `json:"synopsis"` - Genres []struct { - ID int `json:"id"` - Name string `json:"name"` - } `json:"genres"` - StartDate time.Time `json:"start_date"` - EndDate time.Time `json:"end_date"` - Season struct { - Year int `json:"year"` - Season string `json:"season"` - } `json:"season"` - Score float64 `json:"score"` - ScoredBy int `json:"scored_by"` - Rank int `json:"rank"` - Popularity int `json:"popularity"` - Members int `json:"members"` - Source string `json:"source"` - Broadcast struct { - DayOfTheWeek string `json:"day_of_the_week"` - StartTime string `json:"start_time"` - } `json:"broadcast"` - Studios []struct { - ID int `json:"id"` - Name string `json:"name"` - } `json:"studios"` - TrailerURL string `json:"trailer_url"` + Title string `json:"title"` + TitleEn string `json:"title_en"` + TitleJp string `json:"title_jp"` + ImageMediumURL string `json:"image_medium"` + ImageLargeURL string `json:"image_large"` + ImageThumbURL string `json:"image_thumb"` + Type string `json:"type"` + Status string `json:"status"` + Episodes int `json:"episodes"` + Synopsis string `json:"synopsis"` + Genres []AnimeGenre `json:"genres"` + StartDate time.Time `json:"start_date"` + EndDate time.Time `json:"end_date"` + Year int `json:"year"` + Season string `json:"season"` + Score float64 `json:"score"` + ScoredBy int `json:"scored_by"` + Rank int `json:"rank"` + Popularity int `json:"popularity"` + Members int `json:"members"` + Source string `json:"source"` + DayOfTheWeek string `json:"day_of_the_week"` + StartTime string `json:"start_time"` + Studios []AnimeStudio `json:"studios"` + TrailerURL string `json:"trailer_url"` +} + +type AnimeGenre struct { + ID int `json:"id"` + Name string `json:"name"` +} + +type AnimeStudio struct { + ID int `json:"id"` + Name string `json:"name"` } type WatchUser struct { diff --git a/knecht.go b/knecht.go index 8b4501f..0092928 100644 --- a/knecht.go +++ b/knecht.go @@ -77,28 +77,42 @@ func GetUserBytesCached(username string) ([]byte, error) { return data, err } -func GetSeasonDataAll() (*SeasonJikan, []byte, error) { +func GetSeasonDataAll() ([]Anime, []byte, error) { color.Infoln("Aktuelle Season abfragen...") - data, bytes, err := GetSeasonData(1) + data, _, err := GetSeasonData(1) if err != nil { - return data, bytes, err + return nil, nil, err } color.Infof("%d Anime auf %d Seiten\n", data.Pagination.Items.Total, data.Pagination.LastVisiblePage) + animes := make([]Anime, 0) + // convert to anime + for _, a := range data.Data { + animes = append(animes, Anime{ + Anime: a.MalID, + Data: JikanConvert(&a), + }) + } for i := 2; data.Pagination.HasNextPage; i++ { color.Infof("Seite %d abfragen...\n", i) time.Sleep(time.Second) newData, _, err := GetSeasonData(i) if err != nil { - return data, nil, err + return nil, nil, err } data.Pagination.CurrentPage = newData.Pagination.CurrentPage data.Pagination.HasNextPage = newData.Pagination.HasNextPage - data.Data = append(data.Data, newData.Data...) data.Pagination.Items.Count += newData.Pagination.Items.Count + // convert to anime + for _, a := range newData.Data { + animes = append(animes, Anime{ + Anime: a.MalID, + Data: JikanConvert(&a), + }) + } } - color.Infof("%d Anime bekommen\n", len(data.Data)) - bytes, err = json.Marshal(data) - return data, bytes, err + color.Infof("%d Anime bekommen\n", len(animes)) + bytes, err := json.Marshal(animes) + return animes, bytes, err } func GetSeasonData(page int) (*SeasonJikan, []byte, error) { diff --git a/schaffer.go b/schaffer.go index c696b58..b2fb0d0 100644 --- a/schaffer.go +++ b/schaffer.go @@ -5,6 +5,47 @@ import ( "errors" ) +func JikanConvert(jik *SeasonAnimeJikan) AnimeDetail { + res := AnimeDetail{ + Title: jik.Title, + TitleEn: jik.TitleEnglish, + TitleJp: jik.TitleJapanese, + ImageMediumURL: jik.Images.Jpg.ImageURL, + ImageLargeURL: jik.Images.Jpg.LargeImageURL, + ImageThumbURL: jik.Images.Jpg.SmallImageURL, + Type: jik.Type, + Status: jik.Status, + Episodes: jik.Episodes, + Synopsis: jik.Synopsis, + StartDate: jik.Aired.From, + EndDate: jik.Aired.To, + Year: jik.Year, + Season: jik.Season, + Score: jik.Score, + ScoredBy: jik.ScoredBy, + Rank: jik.Rank, + Popularity: jik.Popularity, + Members: jik.Members, + Source: jik.Source, + DayOfTheWeek: jik.Broadcast.Day, + StartTime: jik.Broadcast.Time, + TrailerURL: jik.Trailer.URL, + } + for _, g := range jik.Genres { + res.Genres = append(res.Genres, AnimeGenre{ + ID: g.MalID, + Name: g.Name, + }) + } + for _, s := range jik.Studios { + res.Studios = append(res.Studios, AnimeStudio{ + ID: s.MalID, + Name: s.Name, + }) + } + return res +} + func GetSeasonCache() (*SeasonJikan, error) { data, err := cache.Get(seasonApiJikan) if err != nil {