Skip to content
Open
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
23 changes: 11 additions & 12 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@ name: Go

on:
push:
branches: [ master ]
branches: [master]
pull_request:
branches: [ master ]
branches: [master]

jobs:

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.15
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.22

- name: Build
run: go build -v ./...
- name: Build
run: go build -v ./...

- name: Test
run: go test -v ./...
- name: Test
run: go test -v ./...
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ vendor
out
config.yaml
.DS_Store

/results
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,21 @@ cookies: >-
# output directory
output: results
# limit 5 parallel download threads
parallelDownloads: 5
parallelDownloads: 5
# wait time between downloads in seconds
waitTime: 1
waitTime: 1
# burst per wait time
rate: 10
rate: 10
# select questions to download (optional)
specificQuestions:
- "question-slug-1"
- "question-slug-2"
- "question-slug-3"
# select users to download their submissions (optional)
specificUsers:
- "user-1"
- "user-2"
- "user-3"
```

Run `./hackerrank-dl` with `config.yaml` in the working directory to start the magic.
Expand Down
21 changes: 12 additions & 9 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,26 @@ package main

import (
"fmt"
"io/ioutil"
"os"

"gopkg.in/yaml.v2"
)

type Config struct {
Contest string `yaml:"contest"`
Cookies string `yaml:"cookies"`
Output string `yaml:"output"`
OutDir string `yaml:"-"`
ParallelDownloads int `yaml:"parallelDownloads"`
MaxWaitTime int `yaml:"waitTime"`
Rate int `yaml:"rate"`
Contest string `yaml:"contest"`
Cookies string `yaml:"cookies"`
Output string `yaml:"output"`
OutDir string `yaml:"-"`
ParallelDownloads int `yaml:"parallelDownloads"`
MaxWaitTime int `yaml:"waitTime"`
Rate int `yaml:"rate"`
SpecificQuestions []string `yaml:"specificQuestions,omitempty"`
SpecificUsers []string `yaml:"specificUsers,omitempty"`
}


func ParseConfig(filename string) (*Config, error) {
data, err := ioutil.ReadFile(filename)
data, err := os.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("error reading config file, %v", err)
}
Expand Down
8 changes: 8 additions & 0 deletions examples/sample-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,11 @@ output: results
parallelDownloads: 5 # limit 1 parallel download
waitTime: 1 #seconds
rate: 10 # rate per wait time
specificQuestions:
- "question-slug-1"
- "question-slug-2"
- "question-slug-3"
specificUsers:
- "user-1"
- "user-2"
- "user-3"
13 changes: 9 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
module github.com/kasvith/hackerrank-dl

go 1.14
go 1.22

require (
github.com/go-resty/resty/v2 v2.2.0
github.com/sirupsen/logrus v1.4.2
gopkg.in/yaml.v2 v2.2.8
github.com/go-resty/resty/v2 v2.11.0
github.com/sirupsen/logrus v1.9.3
gopkg.in/yaml.v2 v2.4.0
)

require (
golang.org/x/net v0.21.0 // indirect
golang.org/x/sys v0.17.0 // indirect
)
70 changes: 55 additions & 15 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,24 +1,64 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-resty/resty/v2 v2.2.0 h1:vgZ1cdblp8Aw4jZj3ZsKh6yKAlMg3CHMrqFSFFd+jgY=
github.com/go-resty/resty/v2 v2.2.0/go.mod h1:nYW/8rxqQCmI3bPz9Fsmjbr2FBjGuR2Mzt6kDh3zZ7w=
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/go-resty/resty/v2 v2.11.0 h1:i7jMfNOJYMp69lq7qozJP+bjgzfAzeOhuGlyDrqxT/8=
github.com/go-resty/resty/v2 v2.11.0/go.mod h1:iiP/OpA0CkcL3IGt1O0+/SIItFUbkkyw5BGXiVdTu+A=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20200222125558-5a598a2470a0 h1:MsuvTghUPjX762sGLnGsxC3HM0B5r83wEtYcYR8/vRs=
golang.org/x/net v0.0.0-20200222125558-5a598a2470a0/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
6 changes: 3 additions & 3 deletions persist.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ package main

import (
"fmt"
log "github.com/sirupsen/logrus"
"io/ioutil"
"os"
"path/filepath"

log "github.com/sirupsen/logrus"
)

func CreateDownloadDir(path string) error {
Expand All @@ -52,7 +52,7 @@ func SaveDownload(config *Config, question string, data *SubmissionData) error {
return fmt.Errorf("error creating download directory, %v", err)
}
log.Infof("saving file: %s", filePath)
err = ioutil.WriteFile(filePath, []byte(data.Code), os.ModePerm)
err = os.WriteFile(filePath, []byte(data.Code), os.ModePerm)
if err != nil {
return fmt.Errorf("error saving file, %v", err)
}
Expand Down
39 changes: 30 additions & 9 deletions questions.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ package main
import (
"encoding/json"
"fmt"

"github.com/go-resty/resty/v2"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -63,17 +64,37 @@ func GetAllQuestions(client *resty.Client, config *Config) (*Questions, error) {
limit := 50
qs, err := getQuestions(client, config.Contest, 0, limit)
if err != nil {
return nil, fmt.Errorf("error fetching question names, %v", err)
return nil, fmt.Errorf("error fetching question names, %v", err)
}

// we get total questions in two requests
if qs.Total > limit {
// ok we got more to download
qs2, err := getQuestions(client, config.Contest, limit, qs.Total)
if err != nil {
return nil, fmt.Errorf("error fetching question names, %v", err)
}
qs.Models = append(qs.Models, qs2.Models...)
// Filter for specific questions if SpecificQuestions is not empty
if len(config.SpecificQuestions) > 0 {
filteredModels := []struct {
ID int `json:"id"`
Slug string `json:"slug"`
}{}

for _, q := range qs.Models {
for _, specificSlug := range config.SpecificQuestions {
if q.Slug == specificSlug {
filteredModels = append(filteredModels, q)
break
}
}
}
qs.Models = filteredModels
qs.Total = len(filteredModels)
} else {
// If no specific questions, get the rest of the questions
if qs.Total > limit {
qs2, err := getQuestions(client, config.Contest, limit, qs.Total)
if err != nil {
return nil, fmt.Errorf("error fetching question names, %v", err)
}
qs.Models = append(qs.Models, qs2.Models...)
}
}

return qs, nil
}

32 changes: 23 additions & 9 deletions submissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ package main
import (
"encoding/json"
"fmt"
"slices"

"github.com/go-resty/resty/v2"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -71,24 +72,37 @@ func getContestSubmissionData(client *resty.Client, contest string, question str
}

// filter submissions and filter only top score, oldest submission for a team
func filterSubmissions(submissions *Submissions) SubmissionMap {
func filterSubmissions(submissions *Submissions, config *Config) SubmissionMap {
hm := make(SubmissionMap)

for _, s := range submissions.Models {
if s.HackerUsername == "[deleted]" {
continue
}

if val, ok := hm[s.HackerUsername]; ok {
if s.Score > val.Score {
hm[s.HackerUsername] = s
} else if s.Score == val.Score && s.CreatedAt < val.CreatedAt {
// if we have two submissions with same score, pick the oldest one
if(len(config.SpecificUsers) > 0) {
if (slices.Contains(config.SpecificUsers, s.HackerUsername)) {
if val, ok := hm[s.HackerUsername]; ok {
if s.Score > val.Score {
hm[s.HackerUsername] = s
} else if s.Score == val.Score && s.CreatedAt > val.CreatedAt {
hm[s.HackerUsername] = s
}
continue
}
hm[s.HackerUsername] = s
}
continue
} else {
if val, ok := hm[s.HackerUsername]; ok {
if s.Score > val.Score {
hm[s.HackerUsername] = s
} else if s.Score == val.Score && s.CreatedAt > val.CreatedAt {
hm[s.HackerUsername] = s
}
continue
}
hm[s.HackerUsername] = s
}
hm[s.HackerUsername] = s
}

return hm
Expand All @@ -110,7 +124,7 @@ func GetAllSubmissions(client *resty.Client, config *Config, question string) (S
}
qs.Models = append(qs.Models, qs2.Models...)
}
return filterSubmissions(qs), nil
return filterSubmissions(qs, config), nil
}

type SubmissionData struct {
Expand Down