This repository has been archived by the owner on Feb 15, 2024. It is now read-only.
forked from ryanbradynd05/go-tmdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
98 lines (82 loc) · 2.28 KB
/
main.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
package tmdb
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"sync"
"time"
)
const baseURL string = "https://api.themoviedb.org/3"
var (
requestMutex = &sync.Mutex{}
rateLimitReset time.Time
)
// TMDb container struct for global properties
type TMDb struct {
apiKey string
}
type apiStatus struct {
Code int `json:"status_code"`
Message string `json:"status_message"`
}
// Init setup the apiKey
func Init(apiKey string) *TMDb {
return &TMDb{apiKey: apiKey}
}
// ToJSON converts from struct to JSON
func ToJSON(payload interface{}) (string, error) {
jsonRes := []byte("{}") //Default value in case of error
jsonRes, err := json.MarshalIndent(payload, "", " ")
return string(jsonRes), err
}
func getTmdb(url string, payload interface{}) (interface{}, error) {
// Go single-threaded so we can deal with the rate limit
requestMutex.Lock()
defer requestMutex.Unlock()
now := time.Now()
if rateLimitReset.After(now) { // We have a reset time in the future, so we're out of requests
// Wait for rate limiter to be reset
<-time.After(rateLimitReset.Sub(now))
}
res, err := http.Get(url)
if err != nil { // HTTP connection error
return payload, err
}
defer res.Body.Close() //Clean up
if res.Header.Get(`x-ratelimit-remaining`) == `0` { // Out of requests for this period
reset := res.Header.Get(`x-ratelimit-reset`)
iReset, err := strconv.ParseInt(reset, 10, 64)
if err == nil {
// Set the reset time here, the next request will trip it
rateLimitReset = time.Unix(iReset+1, 0)
}
}
body, err := ioutil.ReadAll(res.Body)
if err != nil { // Failed to read body
return payload, err
}
defer res.Body.Close()
if res.StatusCode >= 200 && res.StatusCode < 300 { // Success!
json.Unmarshal(body, &payload)
return payload, nil
}
// Handle failure modes
var status apiStatus
err = json.Unmarshal(body, &status)
if err != nil {
return payload, err
}
return payload, fmt.Errorf("Code (%d): %s", status.Code, status.Message)
}
func getOptionsString(options map[string]string, availableOptions map[string]struct{}) string {
var optionsString = ""
for key, val := range options {
if _, ok := availableOptions[key]; ok {
newString := fmt.Sprintf("%s&%s=%s", optionsString, key, val)
optionsString = newString
}
}
return optionsString
}