-
Notifications
You must be signed in to change notification settings - Fork 0
/
giantbomb.go
163 lines (145 loc) · 3.74 KB
/
giantbomb.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
package main
import (
//"bytes"
"encoding/json"
"errors"
"github.com/GitbookIO/diskache"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
)
var api_key = [insert-your-api-key]
var GiantBombCache *diskache.Diskache
type GiantBomb struct {
}
type Results struct {
Name string `json:"name"`
Description string `json:"description"`
Platforms Platforms `json:"platforms"`
Image Image `json:"image"`
ResourceType string `json:"resource_type"`
}
type Image struct {
IconURL string `json:"icon_url"`
MediumURL string `json:"medium_url"`
ScreenURL string `json:"screen_url"`
SmallURL string `json:"small_url"`
SuperURL string `json:"super_url"`
ThumbURL string `json:"thumb_url"`
TinyURL string `json:"tiny_url"`
}
type Platforms []struct {
APIDetailURL string `json:"api_detail_url"`
ID int `json:"id"`
Name string `json:"name"`
SiteDetailURL string `json:"site_detail_url"`
Abbreviation string `json:"abbreviation"`
}
type Result struct {
Error string `json:"error"`
Limit int `json:"limit"`
Offset int `json:"offset"`
NumberOfPageResults int `json:"number_of_page_results"`
NumberOfTotalResults int `json:"number_of_total_results"`
StatusCode int `json:"status_code"`
Res []Results `json:"results"`
Version string `json:"version"`
}
func (g *GiantBomb) SetCacheDirectory(directory string) {
// cache directory
opts := &diskache.Opts{
Directory: directory + "/giantbomb",
}
GiantBombCache, _ = diskache.New(opts)
}
func (g *GiantBomb) GetInfo(gameName string) (r Results, err error) {
url, _ := url.Parse("http://www.giantbomb.com/api/search/?field_list=image,name,genres,platforms,description&limit=1&format=json&resources=game")
q := url.Query()
q.Set("api_key", api_key)
q.Set("queary", gameName)
url.RawQuery = q.Encode()
var res Result
log.Println("getinfo for ", gameName)
if content, inCache := GiantBombCache.Get(gameName); inCache {
if er := parseJson(string(content), &res); er != nil {
log.Println(er)
err = er
return
}
if result, e := getExactName(res, gameName); e != nil {
err = e
return
} else {
r = result
return
}
} else {
resp, e1 := http.Get(url.RequestURI())
if e1 != nil {
log.Println("error getting url for", gameName)
err = e1
return
}
defer resp.Body.Close()
if data, er2 := ioutil.ReadAll(resp.Body); er2 != nil {
log.Println("cant readall", er2)
return
} else {
if er := parseJson(string(data), &res); er != nil {
log.Println(er)
err = er
return
}
GiantBombCache.Set(gameName, []byte(data))
if result, e := getExactName(res, gameName); e != nil {
err = e
return
} else {
r = result
return
}
}
}
return
}
func getExactName(result Result, gameName string) (res Results, err error) {
if (len(result.Res)) <= 0 {
err = errors.New("not enough data")
return
}
for _, g := range result.Res {
if g.Name == gameName {
res = g
return
}
}
err = errors.New("Game Name not found in list")
return
}
func parseJson(jsonStream string, target interface{}) error {
return json.NewDecoder(strings.NewReader(jsonStream)).Decode(target)
}
func GetPlatformsFilter(platformsInfo Platforms) int {
platform := 0
for _, platformInfo := range platformsInfo {
if platformInfo.Abbreviation == "LIN" {
platform += 1
}
if platformInfo.Abbreviation == "MAC" {
platform += 2
}
if platformInfo.Abbreviation == "PC" {
platform += 4
}
}
// found nothing, set it to unknown
if platform == 0 {
platform = 8
}
return platform
}
func Filter(platformsInfo Platforms, filter int) bool {
return (filter & GetPlatformsFilter(platformsInfo)) > 0
}