| 
									
										
										
										
											2024-07-14 21:52:15 +00:00
										 |  |  | /* | 
					
						
							|  |  |  | Copyright © 2024 filifa | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This program is free software: you can redistribute it and/or modify it under | 
					
						
							|  |  |  | the terms of the GNU General Public License as published by the Free Software | 
					
						
							|  |  |  | Foundation, either version 3 of the License, or (at your option) any later | 
					
						
							|  |  |  | version. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This program is distributed in the hope that it will be useful, but WITHOUT ANY | 
					
						
							|  |  |  | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A | 
					
						
							|  |  |  | PARTICULAR PURPOSE. See the GNU General Public License for more details. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | You should have received a copy of the GNU General Public License along with | 
					
						
							|  |  |  | this program. If not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2024-07-02 04:16:44 +00:00
										 |  |  | package statsapi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2024-07-14 16:32:09 +00:00
										 |  |  | 	"encoding/json" | 
					
						
							| 
									
										
										
										
											2024-07-13 20:09:10 +00:00
										 |  |  | 	"io" | 
					
						
							| 
									
										
										
										
											2024-07-02 04:16:44 +00:00
										 |  |  | 	"net/http" | 
					
						
							|  |  |  | 	"net/url" | 
					
						
							| 
									
										
										
										
											2024-07-14 16:32:09 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/evanphx/json-patch/v5" | 
					
						
							| 
									
										
										
										
											2024-07-02 04:16:44 +00:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var DefaultClient = NewClient(http.DefaultClient) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-28 20:16:01 +00:00
										 |  |  | func RequestSchedule(sportId, teamId, date string) ([]byte, error) { | 
					
						
							|  |  |  | 	return DefaultClient.RequestSchedule(sportId, teamId, date) | 
					
						
							| 
									
										
										
										
											2024-07-02 04:16:44 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-13 20:35:43 +00:00
										 |  |  | func RequestFeed(gamePk string) ([]byte, error) { | 
					
						
							|  |  |  | 	return DefaultClient.RequestFeed(gamePk) | 
					
						
							| 
									
										
										
										
											2024-07-02 04:16:44 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-14 18:32:25 +00:00
										 |  |  | func RequestDiffPatch(gamePk, startTimecode, pushUpdateId string) (DiffPatchResponse, error) { | 
					
						
							| 
									
										
										
										
											2024-07-13 20:35:43 +00:00
										 |  |  | 	return DefaultClient.RequestDiffPatch(gamePk, startTimecode, pushUpdateId) | 
					
						
							| 
									
										
										
										
											2024-07-02 04:16:44 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-24 00:04:57 +00:00
										 |  |  | func RequestContent(gamePk string) ([]byte, error) { | 
					
						
							|  |  |  | 	return DefaultClient.RequestContent(gamePk) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-29 00:07:59 +00:00
										 |  |  | func RequestStandings(leagueId string) ([]byte, error) { | 
					
						
							|  |  |  | 	return DefaultClient.RequestStandings(leagueId) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-14 18:32:25 +00:00
										 |  |  | type DiffPatchResponse []byte | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-02 04:16:44 +00:00
										 |  |  | type Client struct { | 
					
						
							|  |  |  | 	baseURL    url.URL | 
					
						
							|  |  |  | 	httpClient *http.Client | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func NewClient(c *http.Client) *Client { | 
					
						
							|  |  |  | 	return &Client{ | 
					
						
							|  |  |  | 		baseURL: url.URL{ | 
					
						
							|  |  |  | 			Scheme: "https", | 
					
						
							|  |  |  | 			Host:   "statsapi.mlb.com", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		httpClient: c, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-14 18:32:25 +00:00
										 |  |  | func (resp *DiffPatchResponse) ExtractPatches() ([]jsonpatch.Patch, error) { | 
					
						
							| 
									
										
										
										
											2024-07-14 16:32:09 +00:00
										 |  |  | 	var patches []jsonpatch.Patch | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-15 02:32:00 +00:00
										 |  |  | 	var objs []map[string]jsonpatch.Patch | 
					
						
							| 
									
										
										
										
											2024-07-14 18:32:25 +00:00
										 |  |  | 	err := json.Unmarshal([]byte(*resp), &objs) | 
					
						
							| 
									
										
										
										
											2024-07-14 16:32:09 +00:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return patches, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, obj := range objs { | 
					
						
							| 
									
										
										
										
											2024-07-15 02:32:00 +00:00
										 |  |  | 		patch := obj["diff"] | 
					
						
							|  |  |  | 		patches = append(patches, patch) | 
					
						
							| 
									
										
										
										
											2024-07-14 16:32:09 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return patches, err | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-28 20:16:01 +00:00
										 |  |  | func (c *Client) RequestSchedule(sportId, teamId, date string) ([]byte, error) { | 
					
						
							| 
									
										
										
										
											2024-07-02 04:16:44 +00:00
										 |  |  | 	endpoint := url.URL{Path: "api/v1/schedule"} | 
					
						
							|  |  |  | 	query := endpoint.Query() | 
					
						
							|  |  |  | 	query.Add("sportId", sportId) | 
					
						
							|  |  |  | 	query.Add("teamId", teamId) | 
					
						
							| 
									
										
										
										
											2024-07-28 20:16:01 +00:00
										 |  |  | 	query.Add("date", date) | 
					
						
							| 
									
										
										
										
											2024-07-02 04:16:44 +00:00
										 |  |  | 	endpoint.RawQuery = query.Encode() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-14 18:41:57 +00:00
										 |  |  | 	return c.get(&endpoint) | 
					
						
							| 
									
										
										
										
											2024-07-02 04:16:44 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-13 20:35:43 +00:00
										 |  |  | func (c *Client) RequestFeed(gamePk string) ([]byte, error) { | 
					
						
							| 
									
										
										
										
											2024-07-02 04:16:44 +00:00
										 |  |  | 	endpoint := url.URL{Path: "api/v1.1/game/" + gamePk + "/feed/live"} | 
					
						
							| 
									
										
										
										
											2024-07-14 18:41:57 +00:00
										 |  |  | 	return c.get(&endpoint) | 
					
						
							| 
									
										
										
										
											2024-07-02 04:16:44 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-14 18:32:25 +00:00
										 |  |  | func (c *Client) RequestDiffPatch(gamePk, startTimecode, pushUpdateId string) (DiffPatchResponse, error) { | 
					
						
							| 
									
										
										
										
											2024-07-02 04:16:44 +00:00
										 |  |  | 	endpoint := url.URL{Path: "api/v1.1/game/" + gamePk + "/feed/live/diffPatch"} | 
					
						
							|  |  |  | 	query := endpoint.Query() | 
					
						
							|  |  |  | 	query.Add("language", "en") | 
					
						
							|  |  |  | 	query.Add("startTimecode", startTimecode) | 
					
						
							|  |  |  | 	query.Add("pushUpdateId", pushUpdateId) | 
					
						
							|  |  |  | 	endpoint.RawQuery = query.Encode() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-14 18:41:57 +00:00
										 |  |  | 	return c.get(&endpoint) | 
					
						
							| 
									
										
										
										
											2024-07-02 04:16:44 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-24 00:04:57 +00:00
										 |  |  | func (c *Client) RequestContent(gamePk string) ([]byte, error) { | 
					
						
							|  |  |  | 	endpoint := url.URL{Path: "api/v1/game/" + gamePk + "/content"} | 
					
						
							|  |  |  | 	return c.get(&endpoint) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-29 00:07:59 +00:00
										 |  |  | func (c *Client) RequestStandings(leagueId string) ([]byte, error) { | 
					
						
							|  |  |  | 	endpoint := url.URL{Path: "api/v1/standings"} | 
					
						
							|  |  |  | 	query := endpoint.Query() | 
					
						
							|  |  |  | 	query.Add("leagueId", leagueId) | 
					
						
							|  |  |  | 	endpoint.RawQuery = query.Encode() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return c.get(&endpoint) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-14 18:41:57 +00:00
										 |  |  | func (c *Client) get(endpoint *url.URL) ([]byte, error) { | 
					
						
							|  |  |  | 	url := c.baseURL.ResolveReference(endpoint) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-20 17:05:23 +00:00
										 |  |  | 	resp, err := c.httpClient.Get(url.String()) | 
					
						
							| 
									
										
										
										
											2024-07-02 04:16:44 +00:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2024-07-13 20:09:10 +00:00
										 |  |  | 		return nil, err | 
					
						
							| 
									
										
										
										
											2024-07-02 04:16:44 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	defer resp.Body.Close() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-13 20:09:10 +00:00
										 |  |  | 	body, err := io.ReadAll(resp.Body) | 
					
						
							|  |  |  | 	return body, err | 
					
						
							| 
									
										
										
										
											2024-07-02 04:16:44 +00:00
										 |  |  | } |