Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .eslintrc.json

This file was deleted.

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ yarn-error.log*

# vercel
.vercel

.vercel
38 changes: 38 additions & 0 deletions api/index.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package handler

import (
"fmt"
"leetcode-stats/src"
"leetcode-stats/templates"
"net/http"
)

func Handler(w http.ResponseWriter, r *http.Request) {
var q = r.URL.Query()
w.Header().Add("Content-Type", "image/svg+xml")
w.Header().Add("Cache-Control", "s-max-age=60, stale-while-revalidate")
if q["username"] != nil && len(q["username"][0]) > 0 {
submissionData, err := src.GetSubmissionStats(q["username"][0])
if err != nil {
fmt.Fprintf(w, "Error: %v", err)
return
}
var theme src.Theme
if q["theme"] != nil && len(q["theme"][0]) > 0 {
theme = src.GenerateTheme(q["theme"][0])
} else {
theme = src.GenerateTheme("light")
}

tmpl := templates.GetSubmissionStatsTemplate()
tmpl.ExecuteTemplate(w, "T", struct {
SubmissionData src.SubmissionData
Theme src.Theme
}{
SubmissionData: submissionData,
Theme: theme,
})
} else {
fmt.Fprintf(w, "<svg>Something went wrong s</svg>")
}
}
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module leetcode-stats

go 1.17

require github.com/machinebox/graphql v0.2.2

require (
github.com/matryer/is v1.4.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
github.com/machinebox/graphql v0.2.2 h1:dWKpJligYKhYKO5A2gvNhkJdQMNZeChZYyBbrZkBZfo=
github.com/machinebox/graphql v0.2.2/go.mod h1:F+kbVMHuwrQ5tYgU9JXlnskM8nOaFxCAEolaQybkjWA=
github.com/matryer/is v1.4.0 h1:sosSmIWwkYITGrxZ25ULNDeKiMNzFSr4V/eqBQP0PeE=
github.com/matryer/is v1.4.0/go.mod h1:8I/i5uYgLzgsgEloJE1U6xx5HkBQpAZvepWuujKwMRU=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
3 changes: 0 additions & 3 deletions next.config.js

This file was deleted.

26 changes: 0 additions & 26 deletions package.json

This file was deleted.

46 changes: 0 additions & 46 deletions pages/_app.js

This file was deleted.

27 changes: 0 additions & 27 deletions pages/api/index.js

This file was deleted.

11 changes: 0 additions & 11 deletions pages/index.js

This file was deleted.

Binary file removed public/favicon-16x16.png
Binary file not shown.
Binary file removed public/favicon-192x192.png
Binary file not shown.
Binary file removed public/favicon-32x32.png
Binary file not shown.
Binary file removed public/favicon-96x96.png
Binary file not shown.
4 changes: 0 additions & 4 deletions public/vercel.svg

This file was deleted.

42 changes: 42 additions & 0 deletions src/GenerateTheme.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package src

type Theme struct {
Background string
Text string
SecondaryText string
EasyDifficulty string
MediumDifficulty string
HardDifficulty string
}

func GenerateTheme(theme string) Theme {
switch theme {
case "dark":
return Theme{
Background: "#292A2B",
Text: "#CFCCC7",
SecondaryText: "#7A807C",
EasyDifficulty: "#43A047",
MediumDifficulty: "#FB8C00",
HardDifficulty: "#E91E63",
}
case "midnight":
return Theme{
Background: "#1A1B27",
Text: "#70A4FC",
SecondaryText: "#BE90F2",
EasyDifficulty: "#38BCAD",
MediumDifficulty: "#38BCAD",
HardDifficulty: "#38BCAD",
}
default:
return Theme{
Background: "#FFF",
Text: "#262626",
SecondaryText: "#8A8A8E",
EasyDifficulty: "#43A047",
MediumDifficulty: "#FB8C00",
HardDifficulty: "#E91E63",
}
}
}
54 changes: 54 additions & 0 deletions src/GetSubmissionStats.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package src

import (
"context"

"github.com/machinebox/graphql"
)

type SubmissionData struct {
AllQuestionsCount []struct {
Difficulty string `json:"difficulty"`
Count int `json:"count"`
} `json:"allQuestionsCount"`
MatchedUser struct {
SubmitStats struct {
AcSubmissionNum []struct {
Difficulty string `json:"difficulty"`
Count int `json:"count"`
Submissions int `json:"submissions"`
} `json:"acSubmissionNum"`
} `json:"submitStats"`
} `json:"matchedUser"`
}

func GetSubmissionStats(username string) (SubmissionData, error) {
client := graphql.NewClient("https://leetcode.com/graphql")

req := graphql.NewRequest(`
query getUserProfile($username: String!) {
allQuestionsCount {
difficulty
count
}
matchedUser(username: $username) {
submitStats {
acSubmissionNum {
difficulty
count
submissions
}
}
}
}
`)
req.Var("username", username)

ctx := context.Background()

var respData SubmissionData
if err := client.Run(ctx, req, &respData); err != nil {
return respData, err
}
return respData, nil
}
Loading