-
Notifications
You must be signed in to change notification settings - Fork 0
/
msf.go
163 lines (148 loc) · 5.71 KB
/
msf.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 (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
"google.golang.org/appengine"
"google.golang.org/appengine/datastore"
"google.golang.org/appengine/log"
"google.golang.org/appengine/memcache"
"google.golang.org/appengine/urlfetch"
)
const cumulative_regular_stats = "https://api.mysportsfeeds.com/v1.2/pull/nhl/2017-2018-regular/cumulative_player_stats.json"
const cumulative_playoff_stats = "https://api.mysportsfeeds.com/v1.2/pull/nhl/2018-playoff/cumulative_player_stats.json"
func FetchRegular(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(appengine.NewContext(r), 60*time.Second)
defer cancel()
client := urlfetch.Client(ctx)
req, err := http.NewRequest("GET", cumulative_regular_stats, nil)
req.SetBasicAuth(MSFUsername, MSFPassword)
res, err := client.Do(req)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer res.Body.Close()
if res.StatusCode == 200 {
target := MSFCumulativePlayerStatsResponse{}
json.NewDecoder(res.Body).Decode(&target)
log.Infof(ctx, fmt.Sprintf("%v", target.CumulativePlayerStats.LastUpdatedOn))
for _, msfPlayer := range target.CumulativePlayerStats.Players {
if msfPlayer.PlayerStats.Stats.Points.Value == 0 {
continue
}
playerKey := datastore.NewKey(ctx, "Player", msfPlayer.Description.ID, 0, nil)
player := toPlayer(msfPlayer)
if _, err := datastore.Put(ctx, playerKey, &player); err != nil {
log.Errorf(ctx, "datastore.Put: %v", err)
}
}
if err = memcache.Delete(ctx, ListPlayersMemcacheKey); err != nil {
log.Warningf(ctx, "Error unsetting key: %v", err)
}
} else {
log.Errorf(ctx, fmt.Sprintf("Recieved Error %d", res.StatusCode))
}
fmt.Fprintf(w, "HTTP GET returned status %v", res.Status)
}
func FetchPlayoff(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(appengine.NewContext(r), 60*time.Second)
defer cancel()
client := urlfetch.Client(ctx)
req, err := http.NewRequest("GET", cumulative_playoff_stats, nil)
req.SetBasicAuth(MSFUsername, MSFPassword)
res, err := client.Do(req)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer res.Body.Close()
if res.StatusCode == 200 {
target := MSFCumulativePlayerStatsResponse{}
json.NewDecoder(res.Body).Decode(&target)
log.Infof(ctx, fmt.Sprintf("%v", target.CumulativePlayerStats.LastUpdatedOn))
for _, msfPlayer := range target.CumulativePlayerStats.Players {
if msfPlayer.PlayerStats.Stats.Points.Value == 0 {
continue
}
playerKey := datastore.NewKey(ctx, "PlayoffPlayer", msfPlayer.Description.ID, 0, nil)
player := toPlayer(msfPlayer)
if _, err := datastore.Put(ctx, playerKey, &player); err != nil {
log.Errorf(ctx, "datastore.Put: %v", err)
}
}
if err = memcache.Delete(ctx, ListPlayoffPlayersMemcacheKey); err != nil {
log.Warningf(ctx, "Error unsetting key: %v", err)
}
} else {
log.Errorf(ctx, fmt.Sprintf("Recieved Error %d", res.StatusCode))
}
fmt.Fprintf(w, "HTTP GET returned status %v", res.Status)
}
type MSFCumulativePlayerStatsResponse struct {
CumulativePlayerStats MSFCumulativePlayerStats `json:"cumulativeplayerstats"`
}
type MSFCumulativePlayerStats struct {
LastUpdatedOn string `json:"lastUpdatedOn"`
Players []MSFPlayer `json:"playerstatsentry"`
}
type MSFPlayer struct {
Description MSFDescription `json:"player"`
Team MSFTeam `json:"team"`
PlayerStats MSFPlayerStats `json:"stats"`
}
type MSFDescription struct {
ID string `json:"ID"`
LastName string `json:"LastName"`
FirstName string `json:"FirstName"`
JerseyNumber string `json:"JerseyNumber"`
Position string `json:"Position"`
Height string `json:"Height"`
Weight string `json:"Weight"`
BirthDate string `json:"BirthDate"`
Age int64 `json:"Age,string"`
BirthCity string `json:"BirthCity"`
BirthCountry string `json:"BirthCountry"`
IsRookie bool `json:"IsRookie"`
}
type MSFTeam struct {
ID string `json:"ID"`
City string `json:"City"`
Name string `json:"Name"`
Abbreviation string `json:"Abbreviation"`
}
type MSFPlayerStats struct {
GamesPlayed MSFStatsDimension `json:"GamesPlayed"`
Stats MSFStats `json:"stats"`
}
type MSFStats struct {
Goals MSFStatsDimension `json:"Goals"`
Assists MSFStatsDimension `json:"Assists"`
Points MSFStatsDimension `json:"Points"`
HatTricks MSFStatsDimension `json:"HatTricks"`
PlusMinus MSFStatsDimension `json:"PlusMinus"`
Shots MSFStatsDimension `json:"Shots"`
BlockedShots MSFStatsDimension `json:"BlockedShots"`
ShotPercentage MSFStatsDimension `json:"ShotPercentage"`
Penalties MSFStatsDimension `json:"Penalties"`
PenaltyMinutes MSFStatsDimension `json:"PenaltyMinutes"`
PowerplayGoals MSFStatsDimension `json:"PowerplayGoals"`
PowerplayAssists MSFStatsDimension `json:"PowerplayAssists"`
PowerplayPoints MSFStatsDimension `json:"PowerplayPoints"`
ShorthandedGoals MSFStatsDimension `json:"ShorthandedGoals"`
ShorthandedAssists MSFStatsDimension `json:"ShorthandedAssists"`
ShorthandedPoints MSFStatsDimension `json:"ShorthandedPoints"`
GameWinningGoals MSFStatsDimension `json:"GameWinningGoals"`
GameTyingGoals MSFStatsDimension `json:"GameTyingGoals"`
Hits MSFStatsDimension `json:"Hits"`
Faceoffs MSFStatsDimension `json:"Faceoffs"`
FaceoffWins MSFStatsDimension `json:"FaceoffWins"`
FaceoffLosses MSFStatsDimension `json:"FaceoffLosses"`
FaceoffPercent MSFStatsDimension `json:"FaceoffPercent"`
}
type MSFStatsDimension struct {
Dimension string `json:"@abbreviation"`
Value int64 `json:"#text,string"`
}