leetgode

package module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 14, 2020 License: MIT Imports: 15 Imported by: 0

README

leetgode

PkgGoDev test

Description

LeetCode CLI for Gophers.

VS.

Requirement

The leetgode CLI needs the authorization to execute some sub commands. Specifically, it needs LEETCODE_SESSION, and csrftoken.

  1. Open chrome and paste the link below to the chrome linkbar.
    • chrome://settings/cookies/detail?site=leetcode.com
  2. Copy the contents of LEETCODE_SESSION, and csrftoken.
  3. Export below environment values by the use of LEETCODE_SESSION, and csrftoken.
export LEETCODE_SESSION=${LEETCODE_SESSION}
export LEETCODE_TOKEN=${csrftoken}

Usage

leetgode help
Usage: leetgode is leetcode cli for gophers.

SubCommands:
 exec     Submit solution
 generate Generate the skeleton code with the test file by id
 help     Help shows usages
 list     List problems
 pick     Pick a problem by id
 test     Test solution

Install

You can download binary from release page and place it in $PATH directory.

go get
go get -u https://github.com/budougumi0617/leetgode/cmd/leetgode
macOS

If you want to install on macOS, you can use Homebrew.

brew install budougumi0617/tap/leetgode

Contribution

  1. Fork (https://github.com/budougumi0617/leetgode/fork)
  2. Create a feature branch
  3. Commit your changes
  4. Rebase your local changes against the master branch
  5. Run test suite with the go test ./... command and confirm that it passes
  6. Run gofmt -s
  7. Create new Pull Request

License

MIT

Author

Yoichiro Shimizu(@budougumi0617)

Documentation

Index

Constants

View Source
const (
	LIST     CmdName = "list"
	PICK             = "pick"
	GENERATE         = "generate"
	TEST             = "test"
	EXEC             = "exec"
	HELP             = "help"
)
View Source
const (
	Easy   Level = 1
	Medium       = 2
	Hard         = 3
)

Variables

View Source
var CmdMap = map[CmdName]Cmd{
	EXEC:     &ExecCmd{},
	LIST:     &ListCmd{},
	GENERATE: &GenerateCmd{},
	TEST:     &TestCmd{},
	PICK:     &PickCmd{},
	HELP:     &HelpCmd{},
}
View Source
var PaidOnlyError = fmt.Errorf("not support premium question for now")

Functions

func ShowUsage

func ShowUsage(w io.Writer) error

Types

type CheckResult

type CheckResult struct {
	StatusCode             int           `json:"status_code"`
	Lang                   string        `json:"lang"`
	CompileError           string        `json:"compile_error"`
	FullCompileError       string        `json:"full_compile_error"`
	RunSuccess             bool          `json:"run_success"`
	StatusRuntime          string        `json:"status_runtime"`
	Memory                 int           `json:"memory"`
	CodeAnswer             []string      `json:"code_answer"`
	CodeOutput             interface{}   `json:"code_output"` // stringのときと[]stringのときがありうる
	ElapsedTime            int           `json:"elapsed_time"`
	TaskFinishTime         int64         `json:"task_finish_time"`
	ExpectedStatusCode     int           `json:"expected_status_code"`
	ExpectedLang           string        `json:"expected_lang"`
	ExpectedRunSuccess     bool          `json:"expected_run_success"`
	ExpectedStatusRuntime  string        `json:"expected_status_runtime"`
	ExpectedMemory         int           `json:"expected_memory"`
	ExpectedCodeAnswer     []string      `json:"expected_code_answer"`
	ExpectedCodeOutput     []interface{} `json:"expected_code_output"`
	ExpectedElapsedTime    int           `json:"expected_elapsed_time"`
	ExpectedTaskFinishTime int64         `json:"expected_task_finish_time"`
	CorrectAnswer          bool          `json:"correct_answer"`
	TotalCorrect           int           `json:"total_correct"`
	TotalTestcases         int           `json:"total_testcases"`
	RuntimePercentile      interface{}   `json:"runtime_percentile"`
	StatusMemory           string        `json:"status_memory"`
	MemoryPercentile       interface{}   `json:"memory_percentile"`
	PrettyLang             string        `json:"pretty_lang"`
	SubmissionID           string        `json:"submission_id"`
	StatusMsg              string        `json:"status_msg"`
	State                  string        `json:"state"`
	QuestionID             string        `json:"question_id"`
	CompareResult          string        `json:"compare_result"`
	StdOutput              string        `json:"std_output"`
	LastTestcase           string        `json:"last_testcase"`
}

CheckResult is the response from below URL. https://leetcode.com/submissions/detail/${id}/check/

type Cmd

type Cmd interface {
	Name() string
	Usage() string
	MaxArg() int
	Run(context.Context, io.Writer, []string) error
}

type CmdName

type CmdName string

type Code

type Code struct {
	Text        string `json:"text"`
	Value       string `json:"value"`
	DefaultCode string `json:"defaultCode"`
}

Code the struct of leetcode codes.

type Codes

type Codes []*Code

Codes the slice of Code

func (*Codes) UnmarshalJSON

func (c *Codes) UnmarshalJSON(b []byte) error

type Difficulty

type Difficulty struct {
	Level Level `json:"level"`
}

type ExecCmd

type ExecCmd struct{}

func (*ExecCmd) MaxArg

func (c *ExecCmd) MaxArg() int

func (*ExecCmd) Name

func (c *ExecCmd) Name() string

func (*ExecCmd) Run

func (c *ExecCmd) Run(ctx context.Context, out io.Writer, args []string) error

TODO: refactoring exec and test.

func (*ExecCmd) Usage

func (c *ExecCmd) Usage() string

type Format

type Format struct {
	Referer        string
	QuestionID     string
	Content        string
	Stats          string
	CodeDefinition *Code
	SampleTestCase string
	EnableRunCode  bool
	MetaData       string
}

type GenerateCmd

type GenerateCmd struct{}

func (*GenerateCmd) MaxArg

func (g *GenerateCmd) MaxArg() int

func (*GenerateCmd) Name

func (c *GenerateCmd) Name() string

func (*GenerateCmd) Run

func (g *GenerateCmd) Run(ctx context.Context, out io.Writer, args []string) error

func (*GenerateCmd) Usage

func (g *GenerateCmd) Usage() string

type GetQuestionBody

type GetQuestionBody struct {
	Query         string               `json:"query"`
	Variables     GetQuestionVariables `json:"variables"`
	OperationName string               `json:"operationName"`
}

type GetQuestionDetailResult

type GetQuestionDetailResult struct {
	Data QdrData `json:"data"`
}

type GetQuestionResponse

type GetQuestionResponse struct {
	Data GetQuestionResponseData `json:"data"`
}

type GetQuestionResponseData

type GetQuestionResponseData struct {
	Question *Question `json:"question"`
}

type GetQuestionVariables

type GetQuestionVariables struct {
	TitleSlug string `json:"titleSlug"`
}

type HelpCmd

type HelpCmd struct{}

func (*HelpCmd) MaxArg

func (c *HelpCmd) MaxArg() int

func (*HelpCmd) Name

func (c *HelpCmd) Name() string

func (*HelpCmd) Run

func (c *HelpCmd) Run(ctx context.Context, out io.Writer, args []string) error

func (*HelpCmd) Usage

func (c *HelpCmd) Usage() string

type LCOp

type LCOp func(*LeetCode) error

type LeetCode

type LeetCode struct {
	BaseURL string
	// contains filtered or unexported fields
}

func NewLeetCode

func NewLeetCode(ops ...LCOp) (*LeetCode, error)

func (*LeetCode) Check

func (lc *LeetCode) Check(ctx context.Context, q *Question, id string) (*CheckResult, error)

func (*LeetCode) GetQuestion

func (lc *LeetCode) GetQuestion(ctx context.Context, stat Stat) (*Question, error)

func (*LeetCode) GetQuestionByFrontendID

func (lc *LeetCode) GetQuestionByFrontendID(ctx context.Context, id int) (*Question, error)

func (*LeetCode) GetStats

func (lc *LeetCode) GetStats(ctx context.Context) ([]*StatStatusPair, error)

func (*LeetCode) Submit

func (lc *LeetCode) Submit(ctx context.Context, q *Question, ans string) (string, error)

func (*LeetCode) Test

func (lc *LeetCode) Test(ctx context.Context, q *Question, ans string) (string, error)

type Level

type Level int

func (Level) String

func (l Level) String() string

type ListCmd

type ListCmd struct{}

func (*ListCmd) MaxArg

func (c *ListCmd) MaxArg() int

func (*ListCmd) Name

func (c *ListCmd) Name() string

func (*ListCmd) Run

func (c *ListCmd) Run(ctx context.Context, out io.Writer, _ []string) error

func (*ListCmd) Usage

func (c *ListCmd) Usage() string

type PickCmd

type PickCmd struct {
}

func (*PickCmd) MaxArg

func (c *PickCmd) MaxArg() int

func (*PickCmd) Name

func (c *PickCmd) Name() string

func (*PickCmd) Run

func (c *PickCmd) Run(ctx context.Context, out io.Writer, args []string) error

func (*PickCmd) Usage

func (c *PickCmd) Usage() string

type ProblemsResult

type ProblemsResult struct {
	UserName        string            `json:"user_name"`
	NumSolved       int               `json:"num_solved"`
	NumTotal        int               `json:"num_total"`
	AcEasy          int               `json:"ac_easy"`
	AcMedium        int               `json:"ac_medium"`
	AcHard          int               `json:"ac_hard"`
	StatStatusPairs []*StatStatusPair `json:"stat_status_pairs"`
	FrequencyHigh   int               `json:"frequency_high"`
	FrequencyMid    int               `json:"frequency_mid"`
	CategorySlug    string            `json:"category_slug"`
}

type QdrData

type QdrData struct {
	IsCurrentUserAuthenticated bool     `json:"isCurrentUserAuthenticated"`
	Question                   Question `json:"question"`
}

type Question

type Question struct {
	Slug               string      `json:"-"`
	Referer            string      `json:"-"`
	FrontendQuestionID int         `json:"-"`
	QuestionID         string      `json:"questionId"`
	Content            string      `json:"content"`
	Stats              string      `json:"stats"`
	CodeDefinition     Codes       `json:"codeDefinition"`
	SampleTestCase     string      `json:"sampleTestCase"`
	EnableRunCode      bool        `json:"enableRunCode"`
	MetaData           string      `json:"metaData"`
	TranslatedContent  interface{} `json:"translatedContent"`
}

type SolutionRequest

type SolutionRequest struct {
	Lang       string `json:"lang"` // golang
	QuestionID string `json:"question_id"`
	TestMode   string `json:"test_mode"` // false
	Name       string `json:"name"`
	DataInput  string `json:"data_input"` // ex: "[2,7,11,15]\n9", load from Question.SampleTestCase
	TypedCode  string `json:"typed_code"` // load from file
}

SolutionRequest is the parameters for below URL. https://leetcode.com/problems/${title_slug}/interpret_solution/

type SolutionResult

type SolutionResult struct {
	InterpretID string `json:"interpret_id"`
	TestCase    string `json:"test_case"`
}

SolutionResult is the response from below URL. https://leetcode.com/problems/${title_slug}/interpret_solution/

type Stat

type Stat struct {
	QuestionID          int         `json:"question_id"`
	QuestionArticleLive interface{} `json:"question__article__live"`
	QuestionArticleSlug interface{} `json:"question__article__slug"`
	QuestionTitle       string      `json:"question__title"`
	QuestionTitleSlug   string      `json:"question__title_slug"`
	QuestionHide        bool        `json:"question__hide"`
	TotalAcs            int         `json:"total_acs"`
	TotalSubmitted      int         `json:"total_submitted"`
	FrontendQuestionID  int         `json:"frontend_question_id"`
	IsNewQuestion       bool        `json:"is_new_question"`
}

type StatStatusPair

type StatStatusPair struct {
	Stat       Stat        `json:"stat"`
	Status     interface{} `json:"status"`
	Difficulty Difficulty  `json:"difficulty"`
	PaidOnly   bool        `json:"paid_only"`
	IsFavor    bool        `json:"is_favor"`
	Frequency  int         `json:"frequency"`
	Progress   int         `json:"progress"`
}

type SubmitRequest

type SubmitRequest struct {
	Lang       string `json:"lang"`
	QuestionID string `json:"question_id"`
	TestMode   string `json:"test_mode"`
	Name       string `json:"name"`
	JudgeType  string `json:"judge_type"`
	TypedCode  string `json:"typed_code"`
}

SubmitRequest is the request parameters for below URL. https://leetcode.com/problems/${slug}/submit/

type SubmitResult

type SubmitResult struct {
	SubmissionID int `json:"submission_id"`
}

SubmitResult is the response from below URL. https://leetcode.com/problems/${slug}/submit/

type TestCmd

type TestCmd struct{}

func (*TestCmd) MaxArg

func (c *TestCmd) MaxArg() int

func (*TestCmd) Name

func (c *TestCmd) Name() string

func (*TestCmd) Run

func (c *TestCmd) Run(ctx context.Context, out io.Writer, args []string) error

func (*TestCmd) Usage

func (c *TestCmd) Usage() string

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL