Skip to content

Commit

Permalink
feat: display today streak counter
Browse files Browse the repository at this point in the history
  • Loading branch information
j178 committed Jul 8, 2024
1 parent fa18aed commit 7b273dc
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
13 changes: 13 additions & 0 deletions cmd/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ leetgo submit w330/
log.Info("added failed case to testcases.txt")
}
}

_ = showTodayStreak(c)
}

if hasFailedCase {
Expand Down Expand Up @@ -145,3 +147,14 @@ func appendToTestCases(q *leetcode.QuestionData, result *leetcode.SubmitCheckRes
err = utils.WriteFile(testCasesFile.GetPath(), content)
return true, err
}

func showTodayStreak(c leetcode.Client) error {
streak, err := c.GetStreakCounter()
if err != nil {
return err
}
if streak.TodayCompleted {
fmt.Println("You have completed today's problem!\nTotal streak:", streak.StreakCount)
}
return nil
}
27 changes: 25 additions & 2 deletions leetcode/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func (e UnexpectedStatusCode) IsError() bool {

func (e UnexpectedStatusCode) Error() string {
body := "<empty body>"
if len(e.Body) > 100 {
body = e.Body[:100] + "..."
if len(e.Body) > 500 {
body = e.Body[:500] + "..."
}
return fmt.Sprintf("[%d %s] %s", e.Code, http.StatusText(e.Code), body)
}
Expand Down Expand Up @@ -81,6 +81,7 @@ type Client interface {
GetContestQuestionData(contestSlug string, questionSlug string) (*QuestionData, error)
RegisterContest(slug string) error
UnregisterContest(slug string) error
GetStreakCounter() (StreakCounter, error)
}

type cnClient struct {
Expand Down Expand Up @@ -1012,3 +1013,25 @@ func (c *cnClient) GetQuestionTags() ([]QuestionTag, error) {
}
return tags, nil
}

func (c *cnClient) GetStreakCounter() (StreakCounter, error) {
query := `
query getStreakCounter {
problemsetStreakCounter {
today
streakCount
daysSkipped
todayCompleted
}
}`
var resp gjson.Result
_, err := c.graphqlPost(
graphqlRequest{query: query, authType: requireAuth}, &resp,
)
if err != nil {
return StreakCounter{}, err
}
var counter StreakCounter
err = json.Unmarshal(utils.StringToBytes(resp.Get("data.problemsetStreakCounter").Raw), &counter)
return counter, err
}
7 changes: 7 additions & 0 deletions leetcode/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,10 @@ type QuestionTag struct {
TypeName string `json:"typeName"`
TypeTransName string `json:"typeTransName"`
}

type StreakCounter struct {
Today string `json:"today"`
StreakCount int `json:"streakCount"`
DaysSkipped int `json:"daysSkipped"`
TodayCompleted bool `json:"todayCompleted"`
}

0 comments on commit 7b273dc

Please sign in to comment.