Skip to content

Instantly share code, notes, and snippets.

@florentsorel
Created July 6, 2020 22:47
Show Gist options
  • Save florentsorel/39accb657af9bbce853e0fbcbf23e890 to your computer and use it in GitHub Desktop.
Save florentsorel/39accb657af9bbce853e0fbcbf23e890 to your computer and use it in GitHub Desktop.
package betaseries
import (
"bytes"
"fmt"
)
type ShowService service
type Error struct {
Code int `json:"code"`
Message string `json:"text"`
}
type Errors []Error
type ShowResponse struct {
Show Show `json:"show"`
Errors `json:"errors"`
}
type ShowsResponse struct {
Shows []Show `json:"shows"`
Errors `json:"errors"`
}
type SeasonsDetails struct {
Number int `json:"number"`
Episodes int `json:"episodes"`
}
type Showrunner struct {
ID string `json:"id"`
Name string `json:"name"`
Picture string `json:"picture"`
}
type Genres struct {
Adventure string `json:"Adventure"`
Drama string `json:"Drama"`
Fantasy string `json:"Fantasy"`
}
type Notes struct {
Total int `json:"total"`
Mean float64 `json:"mean"`
User int `json:"user"`
}
type Images struct {
Show string `json:"show"`
Banner string `json:"banner"`
Box string `json:"box"`
Poster string `json:"poster"`
}
type SocialLinks struct {
Type string `json:"type"`
ExternalID string `json:"external_id"`
}
type Next struct {
ID interface{} `json:"id"`
Code string `json:"code"`
Date interface{} `json:"date"`
Title interface{} `json:"title"`
Image interface{} `json:"image"`
}
type User struct {
Archived bool `json:"archived"`
Favorited bool `json:"favorited"`
Remaining int `json:"remaining"`
Status int `json:"status"`
Last string `json:"last"`
Tags interface{} `json:"tags"`
Next Next `json:"next"`
FriendsWatching []interface{} `json:"friends_watching"`
}
type Available struct {
Last int `json:"last"`
First int `json:"first"`
}
type Svod struct {
ID string `json:"id"`
LinkURL string `json:"link_url"`
Name string `json:"name"`
Available Available `json:"available"`
Logo string `json:"logo"`
}
type Platforms struct {
Svod Svod `json:"svod"`
}
type Show struct {
ID int `json:"id"`
ThetvdbID int `json:"thetvdb_id"`
ImdbID string `json:"imdb_id"`
Title string `json:"title"`
OriginalTitle string `json:"original_title"`
Description string `json:"description"`
Seasons string `json:"seasons"`
SeasonsDetails []SeasonsDetails `json:"seasons_details"`
Episodes string `json:"episodes"`
Followers string `json:"followers"`
Comments string `json:"comments"`
Similars string `json:"similars"`
Characters string `json:"characters"`
Creation string `json:"creation"`
Showrunner Showrunner `json:"showrunner"`
Genres Genres `json:"genres"`
Length string `json:"length"`
Network string `json:"network"`
Rating string `json:"rating"`
Status string `json:"status"`
Language string `json:"language"`
Notes Notes `json:"notes"`
InAccount bool `json:"in_account"`
Images Images `json:"images"`
Aliases map[int]string `json:"aliases"`
SocialLinks []SocialLinks `json:"social_links"`
User User `json:"user"`
NextTrailer string `json:"next_trailer"`
ResourceURL string `json:"resource_url"`
Platforms Platforms `json:"platforms"`
}
func (s *ShowService) Discover() ([]Show, error) {
url := fmt.Sprintf("shows/discover")
req, err := s.client.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
var shows *ShowsResponse
_, err = s.client.Do(req, &shows)
if err != nil {
return nil, err
}
if shows.Errors.Err() != nil {
return nil, shows.Errors.Err()
}
return shows.Shows, nil
}
func (s *ShowService) Display(id int) (*Show, error) {
url := fmt.Sprintf("shows/display?id=%d", id)
req, err := s.client.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
var show *ShowResponse
_, err = s.client.Do(req, &show)
if err != nil {
return nil, err
}
if show.Errors.Err() != nil {
return nil, show.Errors.Err()
}
return &show.Show, nil
}
func (errs Errors) Err() error {
if len(errs) == 0 {
return nil
}
errS := bytes.NewBuffer(nil)
for _, e := range errs {
fmt.Fprintf(errS, "Error: Code: %v Message: %v", e.Code, e.Message)
}
return fmt.Errorf(errS.String())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment