-
Notifications
You must be signed in to change notification settings - Fork 0
/
yts.go
138 lines (120 loc) · 3.4 KB
/
yts.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
package yts
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"time"
)
// APIEndpoint represents the API's endpoint
const APIEndpoint = "https://yts.ag/api/v2"
// Sort options
const (
SortByTitle = "title"
SortByYear = "year"
SortByRating = "rating"
SortByPeers = "peers"
SortBySeeds = "seeds"
SortByDownload = "download_count"
SortByLike = "like_count"
SortByDateAdded = "date_added"
)
// Order options
const (
OrderAsc = "asc"
OrderDesc = "desc"
)
// Movie represents the movies
type Movie struct {
DateUploaded string `json:"date_uploaded"`
DateUploadedUnix int64 `json:"date_uploaded_unix"`
Genres []string `json:"genres"`
ID int `json:"id"`
ImdbID string `json:"imdb_code"`
Language string `json:"language"`
MediumCover string `json:"medium_cover_image"`
Rating float64 `json:"rating"`
Runtime int `json:"runtime"`
SmallCover string `json:"small_cover_image"`
State string `json:"state"`
Title string `json:"title"`
TitleLong string `json:"title_long"`
Torrents []Torrent `json:"torrents"`
Year int `json:"year"`
}
// Torrent represents the quality for a torrent
type Torrent struct {
DateUploaded string `json:"date_uploaded"`
DateUploadedUnix int64 `json:"date_uploaded_unix"`
Hash string `json:"hash"`
Peers int `json:"peers"`
Quality string `json:"quality"`
Seeds int `json:"seeds"`
Size string `json:"size"`
SizeBytes int `json:"size_bytes"`
URL string `json:"url"`
}
// Data represents the data inside the response body
type Data struct {
PageNumber int `json:"int"`
Movies []Movie `json:"movies"`
}
// Result represents the response from the API
type Result struct {
Status string `json:"status"`
StatusMessage string `json:"status_message"`
Data Data `json:"data"`
}
func getMovieList(v url.Values) ([]Movie, error) {
endpoint := APIEndpoint + "/list_movies.json?" + v.Encode()
var httpClient = &http.Client{
Timeout: time.Second * 10,
}
resp, err := httpClient.Get(endpoint)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("got error %d while getting movie list", resp.StatusCode)
}
var result Result
err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
return nil, err
}
return result.Data.Movies, nil
}
// GetList gets a list of movies from a page number
func GetList(pageNumber, minRating int, sort, order string) ([]Movie, error) {
v := url.Values{}
v.Set("limit", "50")
v.Set("sort_by", sort)
v.Set("order_by", order)
v.Set("minimum_rating", strconv.Itoa(minRating))
v.Set("page", strconv.Itoa(pageNumber))
return getMovieList(v)
}
// Search searches movies
func Search(movieTitle string) ([]Movie, error) {
v := url.Values{}
v.Set("query_term", movieTitle)
return getMovieList(v)
}
// Status returns an error if the YTS API is not responding correctly
func Status() error {
v := url.Values{}
v.Set("limit", "1")
v.Set("sort_by", SortByPeers)
v.Set("order_by", OrderDesc)
v.Set("minimum_rating", "6")
movies, err := getMovieList(v)
if err != nil {
return err
}
if len(movies) == 0 {
return fmt.Errorf("no movies returned")
}
return nil
}