-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstats.go
More file actions
160 lines (153 loc) · 4.7 KB
/
stats.go
File metadata and controls
160 lines (153 loc) · 4.7 KB
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
package gomodio
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
// GameStats struct represents a game's stats
type GameStats struct {
GameID int `json:"game_id"`
ModsCountTotal int `json:"mods_count_total"`
ModsDownloadsToday int `json:"mods_downloads_today"`
ModsDownloadsTotal int `json:"mods_downloads_total"`
ModsDownloadsDailyAverage int `json:"mods_downloads_daily_average"`
ModsSubscribersTotal int `json:"mods_subscribers_total"`
DateExpires int `json:"date_expires"`
}
// ModStats struct represents a group of stats of a mod
type ModStats struct {
Data []Stats `json:"data"`
ResultCount int `json:"result_count"`
ResultLimit int `json:"result_limit"`
ResultTotal int `json:"result_total"`
ResultOffset int `json:"result_offset"`
}
// Stats struct represents a stats object
type Stats struct {
ModID int `json:"mod_id"`
PopularityRankPosition int `json:"popularity_rank_position"`
PopularityRankTotalMods int `json:"popularity_rank_total_mods"`
DownloadsTotal int `json:"downloads_total"`
SubscribersTotal int `json:"subscribers_total"`
RatingsTotal int `json:"ratings_total"`
RatingsPositive int `json:"ratings_positive"`
RatingsNegative int `json:"ratings_negative"`
RatingsPercentagePositive int `json:"ratings_percentage_positive"`
RatingsWeightedAggregate float64 `json:"ratings_weighted_aggregate"`
RatingsDisplayText string `json:"ratings_display_text"`
DateExpires int `json:"date_expires"`
}
// GetGameStats gets a game's stats
func (u *User) GetGameStats(gameID int) (gs *GameStats, err error) {
client := http.Client{Timeout: time.Duration(5 * time.Second)}
req, err := http.NewRequest("GET", "https://api.mod.io/v1/games/"+strconv.Itoa(gameID)+"mods/stats?api_key="+u.APIKey(), nil)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
if err != nil {
return nil, err
}
if u.OAuth2Token() != "" || len(u.OAuth2Token()) > 1 {
req.Header.Set("Authorization", "Bearer "+u.OAuth2Token())
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if resp.StatusCode != 200 {
var errObj ErrorCase
e := json.Unmarshal(body, &errObj)
if e != nil {
log.Fatalln(e)
}
return nil, HandleResponseError(errObj)
}
if err != nil {
return nil, err
}
err = json.Unmarshal(body, &gs)
if err != nil {
return nil, err
}
return gs, nil
}
// GetModStats gets a mod's stats
func (u *User) GetModStats(modID, gameID int) (s *Stats, err error) {
client := http.Client{Timeout: time.Duration(5 * time.Second)}
req, err := http.NewRequest("GET", "https://api.mod.io/v1/games/"+strconv.Itoa(gameID)+"mods/"+strconv.Itoa(modID)+"/stats?api_key="+u.APIKey(), nil)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
if err != nil {
return nil, err
}
if u.OAuth2Token() != "" || len(u.OAuth2Token()) > 1 {
req.Header.Set("Authorization", "Bearer "+u.OAuth2Token())
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if resp.StatusCode != 200 {
var errObj ErrorCase
e := json.Unmarshal(body, &errObj)
if e != nil {
log.Fatalln(e)
}
return nil, HandleResponseError(errObj)
}
if err != nil {
return nil, err
}
err = json.Unmarshal(body, &s)
if err != nil {
return nil, err
}
return s, nil
}
// GetModsStats gets a game's mod's stats
func (u *User) GetModsStats(gameID int, options map[string]int) (ms *ModStats, err error) {
var queryBody url.Values
if options != nil {
for k, v := range options {
queryBody.Add(k, strconv.Itoa(v))
}
}
queryBody.Add("api_key", u.APIKey())
client := http.Client{Timeout: time.Duration(5 * time.Second)}
req, err := http.NewRequest("GET", "https://api.mod.io/v1/games/"+strconv.Itoa(gameID)+"mods/stats", strings.NewReader(queryBody.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
if err != nil {
return nil, err
}
if u.OAuth2Token() != "" || len(u.OAuth2Token()) > 1 {
req.Header.Set("Authorization", "Bearer "+u.OAuth2Token())
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if resp.StatusCode != 200 {
var errObj ErrorCase
e := json.Unmarshal(body, &errObj)
if e != nil {
log.Fatalln(e)
}
return nil, HandleResponseError(errObj)
}
if err != nil {
return nil, err
}
err = json.Unmarshal(body, &ms)
if err != nil {
return nil, err
}
return ms, nil
}