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 if err := c.get(makeURL("/user", ""), &r); err != nil {
23 return nil, err
24 }
25 return r.Data, nil
26 }
27
28 type favourites struct {
29 Data struct {
30 Favorites []string `json:"favorites"`
31 } `json:"data"`
32 Error requestErrors `json:"error"`
33 }
34
35 // Favorites returns a list of show ids that the user has set as favorites
36 func (c *Conn) Favorites() ([]uint64, error) {
37 var r favourites
38 if err := c.get(makeURL("/user/favorites", ""), &r); err != nil {
39 return nil, err
40 }
41 ids := make([]uint64, 0, len(r.Data.Favorites))
42 for _, id := range r.Data.Favorites {
43 if id == "" {
44 continue
45 }
46 i, _ := strconv.ParseUint(id, 10, 64)
47 ids = append(ids, i)
48 }
49 return ids, nil
50 }
51
52 // AddFavorite adds a show id to the list of user favorites
53 func (c *Conn) AddFavorite(id uint64) error {
54 return c.put(makeURL("/user/favorites/"+strconv.FormatUint(id, 10), ""), new(favourites))
55 }
56
57 // RemoveFavorite removes a show id to the list of user favorites
58 func (c *Conn) RemoveFavorite(id uint64) error {
59 return c.delete(makeURL("/user/favorites/"+strconv.FormatUint(id, 10), ""), new(favourites))
60 }
61
62 // Rating represents a single rating for an item
63 type Rating struct {
64 Type string `json:"ratingType"`
65 ItemID uint64 `json:"ratingItemId"`
66 Rating float32 `json:"rating"`
67 }
68
69 type ratings struct {
70 Data []Rating `json:"data"`
71 Error requestErrors `json:"error"`
72 }
73
74 // Ratings returns a list of ratings that the user has set
75 func (c *Conn) Ratings() ([]Rating, error) {
76 var r ratings
77 if err := c.get(makeURL("/user/ratings", ""), &r); err != nil {
78 return nil, err
79 }
80 return r.Data, nil
81 }
82
83 // RatingItemType represents the type of rating, currently one of series,
84 // episode and banner
85 type RatingItemType string
86
87 // The currently available Item Types for Rating
88 const (
89 RatingSeries RatingItemType = "series"
90 RatingEpisode RatingItemType = "episode"
91 RatingBanner RatingItemType = "banner"
92 )
93
94 // RatingsByType returns a list of ratings for a specific type
95 func (c *Conn) RatingsByType(rit RatingItemType) ([]Rating, error) {
96 var (
97 r ratings
98 v url.Values
99 )
100 v.Set("itemType", string(rit))
101 if err := c.get(makeURL("/user/ratings/query", v.Encode()), &r); err != nil {
102 return nil, err
103 }
104 return r.Data, nil
105 }
106
107 // SetRating sets a user rating for a specific series, episode or banner
108 func (c *Conn) SetRating(rit RatingItemType, id uint64, rating uint32) error {
109 return c.put(makeURL(fmt.Sprintf("/user/ratings/%s/%d/%d", rit, id, rating), ""), new(ratings))
110 }
111
112 // RemoveRating removes a user rating for a specific series, episode or banner
113 func (c *Conn) RemoveRating(rit RatingItemType, id uint64) error {
114 return c.delete(makeURL(fmt.Sprintf("/user/ratings/%s/%d", rit, id), ""), new(ratings))
115 }