This repository has been archived by the owner on Sep 29, 2024. It is now read-only.
forked from cyruzin/golang-tmdb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
list.go
238 lines (225 loc) · 5.46 KB
/
list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package tmdb
import (
"fmt"
"net/http"
)
// ListDetails type is a struct for details JSON response.
type ListDetails struct {
CreatedBy string `json:"created_by"`
Description string `json:"description"`
FavoriteCount int64 `json:"favorite_count"`
ID string `json:"id"`
Items []struct {
Adult bool `json:"adult,omitempty"` // Movie
BackdropPath string `json:"backdrop_path"`
FirstAirDate string `json:"first_air_date,omitempty"` // TV
GenreIDs []int64 `json:"genre_ids"`
ID int64 `json:"id"`
MediaType string `json:"media_type"`
Name string `json:"name,omitempty"` // TV
OriginalLanguage string `json:"original_language"`
OriginalName string `json:"original_name,omitempty"` // TV
OriginalTitle string `json:"original_title,omitempty"` // Movie
OriginCountry []string `json:"origin_country,omitempty"` // TV
Overview string `json:"overview"`
Popularity float32 `json:"popularity"`
PosterPath string `json:"poster_path"`
ReleaseDate string `json:"release_date,omitempty"` // Movie
Title string `json:"title,omitempty"` // Movie
Video bool `json:"video,omitempty"` // Movie
VoteAverage float32 `json:"vote_average"`
VoteCount int64 `json:"vote_count"`
} `json:"items"`
ItemCount int64 `json:"item_count"`
Iso639_1 string `json:"iso_639_1"`
Name string `json:"name"`
PosterPath string `json:"poster_path"`
}
// GetListDetails get the details of a list.
//
// https://developers.themoviedb.org/3/lists/get-list-details
func (c *Client) GetListDetails(
id string,
urlOptions map[string]string,
) (*ListDetails, error) {
options := c.fmtOptions(urlOptions)
tmdbURL := fmt.Sprintf(
"%s%s%s?api_key=%s%s",
baseURL,
listURL,
id,
c.apiKey,
options,
)
ListDetails := ListDetails{}
if err := c.get(tmdbURL, &ListDetails); err != nil {
return nil, err
}
return &ListDetails, nil
}
// ListItemStatus type is a struct for item status JSON response.
type ListItemStatus struct {
ID string `json:"id"`
ItemPresent bool `json:"item_present"`
}
// GetListItemStatus check if a movie has already been added to the list.
//
// https://developers.themoviedb.org/3/lists/check-item-status
func (c *Client) GetListItemStatus(
id string,
urlOptions map[string]string,
) (*ListItemStatus, error) {
options := c.fmtOptions(urlOptions)
tmdbURL := fmt.Sprintf(
"%s%s%s/item_status?api_key=%s%s",
baseURL,
listURL,
id,
c.apiKey,
options,
)
listItemStatus := ListItemStatus{}
if err := c.get(tmdbURL, &listItemStatus); err != nil {
return nil, err
}
return &listItemStatus, nil
}
// ListResponse type is a struct for list creation JSON response.
type ListResponse struct {
*Response
Success bool `json:"success"`
ListID int64 `json:"list_id"`
}
// ListCreate type is a struct for list creation JSON request.
type ListCreate struct {
Name string `json:"name"`
Description string `json:"description"`
Language string `json:"language"`
}
// CreateList creates a list.
//
// https://developers.themoviedb.org/3/lists/create-list
func (c *Client) CreateList(
list *ListCreate,
) (*ListResponse, error) {
tmdbURL := fmt.Sprintf(
"%s/list?api_key=%s&session_id=%s",
baseURL,
c.apiKey,
c.sessionID,
)
createList := ListResponse{}
if err := c.request(
tmdbURL,
list,
http.MethodPost,
&createList,
); err != nil {
return nil, err
}
return &createList, nil
}
// ListMedia type is a struct for list media JSON request.
type ListMedia struct {
MediaID int64 `json:"media_id"`
}
// AddMovie add a movie to a list.
//
// https://developers.themoviedb.org/3/lists/add-movie
func (c *Client) AddMovie(
listID int,
mediaID *ListMedia,
) (*Response, error) {
tmdbURL := fmt.Sprintf(
"%s/list/%d/add_item?api_key=%s&session_id=%s",
baseURL,
listID,
c.apiKey,
c.sessionID,
)
response := Response{}
if err := c.request(
tmdbURL,
mediaID,
http.MethodPost,
&response,
); err != nil {
return nil, err
}
return &response, nil
}
// RemoveMovie remove a movie from a list.
//
// https://developers.themoviedb.org/3/lists/remove-movie
func (c *Client) RemoveMovie(
listID int,
mediaID *ListMedia,
) (*Response, error) {
tmdbURL := fmt.Sprintf(
"%s/list/%d/remove_item?api_key=%s&session_id=%s",
baseURL,
listID,
c.apiKey,
c.sessionID,
)
response := Response{}
if err := c.request(
tmdbURL,
mediaID,
http.MethodPost,
&response,
); err != nil {
return nil, err
}
return &response, nil
}
// ClearList clear all of the items from a list.
//
// https://developers.themoviedb.org/3/lists/clear-list
func (c *Client) ClearList(
listID int,
confirm bool,
) (*Response, error) {
tmdbURL := fmt.Sprintf(
"%s/list/%d/clear?api_key=%s&session_id=%s&confirm=%t",
baseURL,
listID,
c.apiKey,
c.sessionID,
confirm,
)
response := Response{}
if err := c.request(
tmdbURL,
listID,
http.MethodPost,
&response,
); err != nil {
return nil, err
}
return &response, nil
}
// DeleteList deletes a list.
//
// https://developers.themoviedb.org/3/lists/delete-list
func (c *Client) DeleteList(
listID int,
) (*Response, error) {
tmdbURL := fmt.Sprintf(
"%s/list/%d?api_key=%s&session_id=%s",
baseURL,
listID,
c.apiKey,
c.sessionID,
)
response := Response{}
if err := c.request(
tmdbURL,
nil,
http.MethodDelete,
&response,
); err != nil {
return nil, err
}
return &response, nil
}