tvdb - user.go
		
     1  package tvdb
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  	"strconv"
     7  )
     8  
     9  // User represents the user details given by the user endpoint.
    10  type User struct {
    11  	Username             string `json:"userName"`
    12  	Language             string `json:"language"`
    13  	FavoritesDisplaymode string `json:"favoritesDisplaymode"`
    14  }
    15  
    16  // User returns the logged in user details.
    17  func (c *Conn) User() (*User, error) {
    18  	var r struct {
    19  		Data  *User         `json:"data"`
    20  		Error requestErrors `json:"error"`
    21  	}
    22  
    23  	if err := c.get(makeURL("/user", ""), &r); err != nil {
    24  		return nil, err
    25  	}
    26  
    27  	return r.Data, nil
    28  }
    29  
    30  type favourites struct {
    31  	Data struct {
    32  		Favorites []string `json:"favorites"`
    33  	} `json:"data"`
    34  	Error requestErrors `json:"error"`
    35  }
    36  
    37  // Favorites returns a list of show ids that the user has set as favorites.
    38  func (c *Conn) Favorites() ([]uint64, error) {
    39  	var r favourites
    40  
    41  	if err := c.get(makeURL("/user/favorites", ""), &r); err != nil {
    42  		return nil, err
    43  	}
    44  
    45  	ids := make([]uint64, 0, len(r.Data.Favorites))
    46  
    47  	for _, id := range r.Data.Favorites {
    48  		if id == "" {
    49  			continue
    50  		}
    51  
    52  		i, _ := strconv.ParseUint(id, 10, 64)
    53  		ids = append(ids, i)
    54  	}
    55  
    56  	return ids, nil
    57  }
    58  
    59  // AddFavorite adds a show id to the list of user favorites.
    60  func (c *Conn) AddFavorite(id uint64) error {
    61  	return c.put(makeURL("/user/favorites/"+strconv.FormatUint(id, 10), ""), new(favourites))
    62  }
    63  
    64  // RemoveFavorite removes a show id to the list of user favorites.
    65  func (c *Conn) RemoveFavorite(id uint64) error {
    66  	return c.delete(makeURL("/user/favorites/"+strconv.FormatUint(id, 10), ""), new(favourites))
    67  }
    68  
    69  // Rating represents a single rating for an item.
    70  type Rating struct {
    71  	Type   string  `json:"ratingType"`
    72  	ItemID uint64  `json:"ratingItemId"`
    73  	Rating float32 `json:"rating"`
    74  }
    75  
    76  type ratings struct {
    77  	Data  []Rating      `json:"data"`
    78  	Error requestErrors `json:"error"`
    79  }
    80  
    81  // Ratings returns a list of ratings that the user has set.
    82  func (c *Conn) Ratings() ([]Rating, error) {
    83  	var r ratings
    84  
    85  	if err := c.get(makeURL("/user/ratings", ""), &r); err != nil {
    86  		return nil, err
    87  	}
    88  
    89  	return r.Data, nil
    90  }
    91  
    92  // RatingItemType represents the type of rating, currently one of series,
    93  // episode and banner.
    94  type RatingItemType string
    95  
    96  // The currently available Item Types for Rating.
    97  const (
    98  	RatingSeries  RatingItemType = "series"
    99  	RatingEpisode RatingItemType = "episode"
   100  	RatingBanner  RatingItemType = "banner"
   101  )
   102  
   103  // RatingsByType returns a list of ratings for a specific type.
   104  func (c *Conn) RatingsByType(rit RatingItemType) ([]Rating, error) {
   105  	var (
   106  		r ratings
   107  		v url.Values
   108  	)
   109  
   110  	v.Set("itemType", string(rit))
   111  
   112  	if err := c.get(makeURL("/user/ratings/query", v.Encode()), &r); err != nil {
   113  		return nil, err
   114  	}
   115  
   116  	return r.Data, nil
   117  }
   118  
   119  // SetRating sets a user rating for a specific series, episode or banner.
   120  func (c *Conn) SetRating(rit RatingItemType, id uint64, rating uint32) error {
   121  	return c.put(makeURL(fmt.Sprintf("/user/ratings/%s/%d/%d", rit, id, rating), ""), new(ratings))
   122  }
   123  
   124  // RemoveRating removes a user rating for a specific series, episode or banner.
   125  func (c *Conn) RemoveRating(rit RatingItemType, id uint64) error {
   126  	return c.delete(makeURL(fmt.Sprintf("/user/ratings/%s/%d", rit, id), ""), new(ratings))
   127  }
   128