Set Cache
Also custom errors
This commit is contained in:
		
							
								
								
									
										34
									
								
								aoc.go
									
									
									
									
									
								
							
							
						
						
									
										34
									
								
								aoc.go
									
									
									
									
									
								
							| @@ -2,9 +2,8 @@ package aoc | |||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"encoding/json" | 	"encoding/json" | ||||||
| 	"errors" |  | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"io/ioutil" | 	"io" | ||||||
| 	"net/http" | 	"net/http" | ||||||
| 	"strconv" | 	"strconv" | ||||||
| 	"time" | 	"time" | ||||||
| @@ -18,10 +17,10 @@ type AoC struct { | |||||||
|  |  | ||||||
| func NewAoC(boardId, session string) (*AoC, error) { | func NewAoC(boardId, session string) (*AoC, error) { | ||||||
| 	if boardId == "" { | 	if boardId == "" { | ||||||
| 		return nil, errors.New("Board ID is required") | 		return nil, new(BoardIdRequiredError) | ||||||
| 	} | 	} | ||||||
| 	if session == "" { | 	if session == "" { | ||||||
| 		return nil, errors.New("Session key is required") | 		return nil, new(SessionRequiredError) | ||||||
| 	} | 	} | ||||||
| 	return &AoC{ | 	return &AoC{ | ||||||
| 		session: session, | 		session: session, | ||||||
| @@ -30,19 +29,29 @@ func NewAoC(boardId, session string) (*AoC, error) { | |||||||
| 	}, nil | 	}, nil | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func (a *AoC) SetCachedLeaderboard(l *Leaderboard) error { | ||||||
|  | 	yr, err := strconv.Atoi(l.Event) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return new(InvalidYearError) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	a.boards[yr] = l | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|  |  | ||||||
| func (a *AoC) GetCachedLeaderboard(year int) (*Leaderboard, error) { | func (a *AoC) GetCachedLeaderboard(year int) (*Leaderboard, error) { | ||||||
| 	if year < 2015 || year > time.Now().Year() { | 	if year < 2015 || year > time.Now().Year() { | ||||||
| 		return nil, errors.New("Invalid Year") | 		return nil, new(InvalidYearError) | ||||||
| 	} | 	} | ||||||
| 	if board, ok := a.boards[year]; ok { | 	if board, ok := a.boards[year]; ok { | ||||||
| 		return board, nil | 		return board, nil | ||||||
| 	} | 	} | ||||||
| 	return nil, errors.New(fmt.Sprintf("Leaderboard (%d) is not cached", year)) | 	return nil, LeaderboardNotCachedError{year: year} | ||||||
| } | } | ||||||
|  |  | ||||||
| func (a *AoC) GetLeaderboard(year int) (*Leaderboard, error) { | func (a *AoC) GetLeaderboard(year int) (*Leaderboard, error) { | ||||||
| 	if year < 2015 || year > time.Now().Year() { | 	if year < 2015 || year > time.Now().Year() { | ||||||
| 		return nil, errors.New("Invalid Year") | 		return nil, new(InvalidYearError) | ||||||
| 	} | 	} | ||||||
| 	if board, ok := a.boards[year]; ok { | 	if board, ok := a.boards[year]; ok { | ||||||
| 		if time.Since(board.LastFetch) < (time.Minute * 15) { | 		if time.Since(board.LastFetch) < (time.Minute * 15) { | ||||||
| @@ -53,7 +62,7 @@ func (a *AoC) GetLeaderboard(year int) (*Leaderboard, error) { | |||||||
| 	a.boards[year], err = a.fetchLeaderboard(year) | 	a.boards[year], err = a.fetchLeaderboard(year) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		if err.Error() == "invalid character '<' looking for beginning of value" { | 		if err.Error() == "invalid character '<' looking for beginning of value" { | ||||||
| 			return nil, errors.New("Invalid Session Cookie") | 			return nil, new(SessionError) | ||||||
| 		} | 		} | ||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
| @@ -77,17 +86,15 @@ func (a *AoC) fetchLeaderboard(year int) (*Leaderboard, error) { | |||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
| 	defer resp.Body.Close() | 	defer resp.Body.Close() | ||||||
| 	body, err = ioutil.ReadAll(resp.Body) | 	body, err = io.ReadAll(resp.Body) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
| 	strBody := string(body) | 	strBody := string(body) | ||||||
| 	//strBody = strings.ReplaceAll(strBody, "\"last_star_ts\":\"0\"", "\"last_star_ts\":0") | 	// strBody = strings.ReplaceAll(strBody, "\"last_star_ts\":\"0\"", "\"last_star_ts\":0") | ||||||
| 	err = json.Unmarshal([]byte(strBody), &leaderboard) | 	err = json.Unmarshal([]byte(strBody), &leaderboard) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		fmt.Println("Error parsing board") | 		return nil, fmt.Errorf("error parsing board: %w", err) | ||||||
| 		fmt.Println(err) |  | ||||||
| 		return nil, err |  | ||||||
| 	} | 	} | ||||||
| 	for k, mbr := range leaderboard.Members { | 	for k, mbr := range leaderboard.Members { | ||||||
| 		starTs, err := strconv.ParseInt(mbr.RawStarTs, 10, 64) | 		starTs, err := strconv.ParseInt(mbr.RawStarTs, 10, 64) | ||||||
| @@ -144,7 +151,6 @@ func (a ByStarsThenScore) Less(i, j int) bool { | |||||||
| } | } | ||||||
|  |  | ||||||
| func (m *Member) UnmarshalJSON(data []byte) error { | func (m *Member) UnmarshalJSON(data []byte) error { | ||||||
|  |  | ||||||
| 	var v map[string]interface{} | 	var v map[string]interface{} | ||||||
| 	var err error | 	var err error | ||||||
|  |  | ||||||
|   | |||||||
							
								
								
									
										35
									
								
								aoc_errors.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								aoc_errors.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,35 @@ | |||||||
|  | package aoc | ||||||
|  |  | ||||||
|  | import "fmt" | ||||||
|  |  | ||||||
|  | type SessionRequiredError struct{} | ||||||
|  |  | ||||||
|  | func (e SessionRequiredError) Error() string { | ||||||
|  | 	return "session key is required" | ||||||
|  | } | ||||||
|  |  | ||||||
|  | type SessionError struct{} | ||||||
|  |  | ||||||
|  | func (e SessionError) Error() string { | ||||||
|  | 	return "invalid session" | ||||||
|  | } | ||||||
|  |  | ||||||
|  | type BoardIdRequiredError struct{} | ||||||
|  |  | ||||||
|  | func (e BoardIdRequiredError) Error() string { | ||||||
|  | 	return "board id is required" | ||||||
|  | } | ||||||
|  |  | ||||||
|  | type InvalidYearError struct{} | ||||||
|  |  | ||||||
|  | func (e InvalidYearError) Error() string { | ||||||
|  | 	return "invalid year" | ||||||
|  | } | ||||||
|  |  | ||||||
|  | type LeaderboardNotCachedError struct { | ||||||
|  | 	year int | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (e LeaderboardNotCachedError) Error() string { | ||||||
|  | 	return fmt.Sprintf("leaderboard (%d) is not cached", e.year) | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user