Skip to content

Commit 7c9a2f0

Browse files
authored
feat: add vscode header (#3)
* feat: add vscode extension header * feat: header flag for vscode leetcode extension * chore: update readme for header flag * fix: default flag values
1 parent 804ca19 commit 7c9a2f0

File tree

4 files changed

+34
-8
lines changed

4 files changed

+34
-8
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ Usage of leetdoad:
3434
Debug logs
3535
-version
3636
Show the current leetdoad version
37+
-header
38+
Include a header and footer in the scraped submission file in the style of the [VSCode LeetCode extension](https://marketplace.visualstudio.com/items?itemName=LeetCode.vscode-leetcode).
3739
```
3840

3941
Leetdoad uses a cookie to download your latest submissions from Leetcode, and cookie can be found in your browser when you visit Leetcode website. If you don't know how to find the cookie, Google is your friend.

main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type flags struct {
3434
cookie string
3535
debug bool
3636
version bool
37+
header bool
3738
}
3839

3940
func main() {
@@ -42,7 +43,9 @@ func main() {
4243
flag.StringVar(&f.cookie, "cookie", "", "Cookie that used for scraping problems and solutions from Leetcode website, you can either pass it from here, or set LEETCODE_COOKIE env")
4344
flag.BoolVar(&f.debug, "debug", false, "Debug logs")
4445
flag.BoolVar(&f.version, "version", false, "Show the current leetdoad version")
46+
flag.BoolVar(&f.header, "header", false, "Add LeetCode VSCode extension header")
4547
flag.Parse()
48+
4649
if f.version {
4750
printVersion()
4851
return
@@ -58,7 +61,7 @@ func main() {
5861
client := http.Client{
5962
Timeout: 5 * time.Second,
6063
}
61-
if err := scraper.NewScraper(client, cfg).Scrape(); err != nil {
64+
if err := scraper.NewScraper(client, cfg, f.header).Scrape(); err != nil {
6265
log.Fatal().Msgf("failed to scrape solutions: %s", err.Error())
6366
}
6467
}

pkg/leetcode/submission.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package leetcode
22

33
import (
4+
"fmt"
45
"os"
56
"path/filepath"
67
)
@@ -12,7 +13,7 @@ type Submission struct {
1213
Code string `json:"code"`
1314
}
1415

15-
func (s Submission) WriteTo(fileName string) error {
16+
func (s Submission) WriteTo(fileName string, q Question, includeHeader bool) error {
1617
if err := os.MkdirAll(filepath.Dir(fileName), os.ModePerm); err != nil {
1718
return err
1819
}
@@ -27,10 +28,28 @@ func (s Submission) WriteTo(fileName string) error {
2728
return err
2829
}
2930
}
31+
32+
if includeHeader {
33+
f.WriteString(fmt.Sprintf(`/*
34+
* @lc app=leetcode id=%d lang=%s
35+
*
36+
* [%d] %s
37+
*/
38+
39+
// @lc code=start
40+
`, q.FrontendQuestionID, s.Language, q.FrontendQuestionID, q.QuestionTitleSlug))
41+
}
42+
3043
_, err = f.WriteString(s.Code)
3144
if err != nil {
3245
return err
3346
}
47+
48+
if includeHeader {
49+
f.WriteString(`
50+
// @lc code=end`)
51+
}
52+
3453
return nil
3554
}
3655

pkg/scraper/scraper.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,16 @@ type filePattern struct {
3131
}
3232

3333
type scraper struct {
34-
client http.Client
35-
config *config.InConfig
34+
client http.Client
35+
config *config.InConfig
36+
includeHeader bool
3637
}
3738

38-
func NewScraper(client http.Client, config *config.InConfig) *scraper {
39+
func NewScraper(client http.Client, config *config.InConfig, includeHeader bool) *scraper {
3940
return &scraper{
40-
client: client,
41-
config: config,
41+
client: client,
42+
config: config,
43+
includeHeader: includeHeader,
4244
}
4345
}
4446

@@ -84,7 +86,7 @@ func (s *scraper) Scrape() error {
8486
}
8587
fileName := fmt.Sprintf("%s/%s.%s", pwd, buf.String(), s.config.LanguageMap[sub.Language])
8688
buf.Reset()
87-
if err := sub.WriteTo(fileName); err != nil {
89+
if err := sub.WriteTo(fileName, q, s.includeHeader); err != nil {
8890
return err
8991
}
9092
log.Debug().

0 commit comments

Comments
 (0)