cflibrary

package module
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2020 License: MIT Imports: 10 Imported by: 0

README

cf-library

GitHub Workflow Status GitHub issues GitHub go.mod Go version gdoc reference GitHub license

Documentation

Index

Constants

View Source
const (
	RegClosed = regState(0)
	RegOpen   = regState(1)
	RegDone   = regState(2)
)

Various registration states of the current user session.

View Source
const (
	SolveAccepted     = solveState(0)
	SolveRejected     = solveState(1)
	SolveNotAttempted = solveState(2)
)

Various solve status of user in any given problem.

Variables

View Source
var (
	ErrInvalidGroup        = fmt.Errorf("invalid group id")
	ErrInvalidContest      = fmt.Errorf("invalid contest id")
	ErrInvalidProblem      = fmt.Errorf("invalid problem id")
	ErrInvalidHandle       = fmt.Errorf("invalid user handle")
	ErrInvalidDetails      = fmt.Errorf("invalid specifier details")
	ErrAccessDenied        = fmt.Errorf("access to webpage denied")
	ErrInvalidSubmissionID = fmt.Errorf("invalid submission id")
	ErrInvalidCredentials  = fmt.Errorf("invalid login credentials")
	ErrNoProblemsPage      = fmt.Errorf("problem(s) page isn't available")
	ErrInvalidLangID       = fmt.Errorf("invalid language id")
	ErrRegistrationClosed  = fmt.Errorf("contest registration not open")
	ErrLoggedInAccessOnly  = fmt.Errorf("logged in session required")
	ErrNotSupported        = fmt.Errorf("operation on specified contest not supported")

	// SessCln is the http.Client to use to fetch all data from codeforces.
	// Ensure SessCln is configured with corresponding cookies (and proxy)
	// to enable session/proxy specific abilities while fetching webpages.
	SessCln *http.Client
)

Various defined errors that are returned by the library. Definition of errors are quite self explanatory.

View Source
var (
	// LangID represents all available languages with ids.
	LangID = map[string]string{
		"GNU GCC C11 5.1.0":                "43",
		"Clang++17 Diagnostics":            "52",
		"GNU G++11 5.1.0":                  "42",
		"GNU G++14 6.4.0":                  "50",
		"GNU G++17 7.3.0":                  "54",
		"Microsoft Visual C++ 2010":        "2",
		"Microsoft Visual C++ 2017":        "59",
		"GNU G++17 9.2.0 (64 bit, msys 2)": "61",
		"C# Mono 5.18":                     "9",
		"D DMD32 v2.086.0":                 "28",
		"Go 1.12.6":                        "32",
		"Haskell GHC 8.6.3":                "12",
		"Java 11.0.5":                      "60",
		"Java 1.8.0_162":                   "36",
		"Kotlin 1.3.10":                    "48",
		"OCaml 4.02.1":                     "19",
		"Delphi 7":                         "3",
		"Free Pascal 3.0.2":                "4",
		"PascalABC.NET 3.4.2":              "51",
		"Perl 5.20.1":                      "13",
		"PHP 7.2.13":                       "6",
		"Python 2.7.15":                    "7",
		"Python 3.7.2":                     "31",
		"PyPy 2.7 (7.2.0)":                 "40",
		"PyPy 3.6 (7.2.0)":                 "41",
		"Ruby 2.0.0p645":                   "8",
		"Rust 1.35.0":                      "49",
		"Scala 2.12.8":                     "20",
		"JavaScript V8 4.8.0":              "34",
		"Node.js 9.4.0":                    "55",
		"ActiveTcl 8.5":                    "14",
		"Io-2008-01-07 (Win32)":            "15",
		"Pike 7.8":                         "17",
		"Befunge":                          "18",
		"OpenCobol 1.0":                    "22",
		"Factor":                           "25",
		"Secret_171":                       "26",
		"Roco":                             "27",
		"Ada GNAT 4":                       "33",
		"Mysterious Language":              "38",
		"FALSE":                            "39",
		"Picat 0.9":                        "44",
		"GNU C++11 5 ZIP":                  "45",
		"Java 8 ZIP":                       "46",
		"J":                                "47",
		"Microsoft Q#":                     "56",
		"Text":                             "57",
	}

	// LangExt corresponds to file extension of
	// given language source code.
	LangExt = map[string]string{
		"GNU C11":               ".c",
		"Clang++17 Diagnostics": ".cpp",
		"GNU C++0x":             ".cpp",
		"GNU C++":               ".cpp",
		"GNU C++11":             ".cpp",
		"GNU C++14":             ".cpp",
		"GNU C++17":             ".cpp",
		"MS C++":                ".cpp",
		"MS C++ 2017":           ".cpp",
		"GNU C++17 (64)":        ".cpp",
		"Mono C#":               ".cs",
		"D":                     ".d",
		"Go":                    ".go",
		"Haskell":               ".hs",
		"Kotlin":                ".kt",
		"Ocaml":                 ".ml",
		"Delphi":                ".pas",
		"FPC":                   ".pas",
		"PascalABC.NET":         ".pas",
		"Perl":                  ".pl",
		"PHP":                   ".php",
		"Python 2":              ".py",
		"Python 3":              ".py",
		"PyPy 2":                ".py",
		"PyPy 3":                ".py",
		"Ruby":                  ".rb",
		"Rust":                  ".rs",
		"JavaScript":            ".js",
		"Node.js":               ".js",
		"Q#":                    ".qs",
		"Java":                  ".java",
		"Java 6":                ".java",
		"Java 7":                ".java",
		"Java 8":                ".java",
		"Java 9":                ".java",
		"Java 10":               ".java",
		"Java 11":               ".java",
		"Tcl":                   ".tcl",
		"F#":                    ".fs",
		"Befunge":               ".bf",
		"Pike":                  ".pike",
		"Io":                    ".io",
		"Factor":                ".factor",
		"Cobol":                 ".cbl",
		"Secret_171":            ".secret_171",
		"Ada":                   ".adb",
		"FALSE":                 ".f",
		"":                      ".txt",
	}
)

Functions

func Login

func Login(usr, passwd string) (string, error)

Login tries logging into codeforces using credentials passed. Checks if any active session exists (in SessCln) before logging in. If you wish to overwrite currently logged in session, set cookies of SessCln to nil before logging in.

If login is successful, returns user handle of now logged in session. Otherwise, if login fails, returns ErrInvalidCredentials as error.

By default, option 'remember me' is checked, ensuring the session has expiry period of one month from date of last login.

Types

type Args

type Args struct {
	ContClass string
	Contest   string
	Problem   string
	Group     string
}

Args holds specified contest details. ContClass is auto evaluated by the library.

func ParseDetails

func ParseDetails(specifier string) (Args, error)

ParseDetails takes in input of any useful specifier data. Parses, validates and returns Args struct containing specifier info. Valid specifier configurations can be viewed at github.com/cp-tools/cf-library.wiki.

func (*Args) GetContests added in v1.3.0

func (a *Args) GetContests() ([]Contest, error)

GetContests extracts contest/gym/group upcoming contest data from respective contest set page corresponding to passed args. Returns a slice []Contest containing details of specified contests.

If contest is not specified, extracts only upcoming contests data.

Set value of a.ContClass to the desired contest class you wish to extract upcoming data from. Ensure you set the group id too, in case you wish to extract all contests in a particular group.

func (*Args) GetCountdown

func (a *Args) GetCountdown() (int64, error)

GetCountdown parses and returns number of seconds for countdown in specified contest to end. If countdown has already ended, returns 0. Extracts data from .../contest/<contest>/countdown.

func (*Args) GetProblems

func (a *Args) GetProblems() ([]Problem, error)

GetProblems parses problem details of all problems matching specified args from contest dashboard page. Returns an array of Problems containing requested data. Extracts all data except SampleTests, which is left nil.

func (*Args) GetSampleTests

func (a *Args) GetSampleTests(mode bool) ([]Problem, error)

GetSampleTests parses all sample test cases of problem(s) matching specified args from the problem/problems page. Returns an array of Problems containing requested data. If mode is set to false, extracts and returns only sample tests in specified problem/problem(s). Else, if set to true, extracts both test cases and problem info from dashboard.

Note that, some older contests (and many gym/group contests) don't support complete problemset page. Fetching data from such contests will return error ErrNoProblemsPage. It also applies similarly for problems with only pdf format of their problem statements.

func (*Args) GetSubmissions

func (a *Args) GetSubmissions(handle string) ([]Submission, error)

GetSubmissions parses and returns submission data of all submissions matching specified args of handle passed in params. If args given are nil, extracts all submissions data of user from codeforces/submissions/<handle>. Otherwise, submissions are extracted from users submissions in contest page.

Currently, groups are not supported (due to a bug on codeforces).

func (*Args) RegisterForContest added in v1.3.0

func (a *Args) RegisterForContest() (RegisterInfo, error)

RegisterForContest registers current user session for contest specified in args. Returns type RegisterInfo containing contest name, terms of agreement and a callback function to register the user if the user agrees to these terms.

If Register() was successful, callback returns nil error.

func (*Args) SubmitSourceCode

func (a *Args) SubmitSourceCode(langID string, sourceCode string) error

SubmitSourceCode submits solution to problem specified in passed parameter args. langID is the language index of the specified language. All language id's corresponding to their languages are detailed at github.com/cp-tools/cf-library.wiki.

If submission was successful, returns nil error.

type Contest added in v1.3.0

type Contest struct {
	Name      string
	Writers   string
	When      string
	Length    string
	RegCount  string
	RegStatus regState
	Details   Args
}

Contest holds details of particular contest row from the codeforces.com/contests page. Currently, only supports upcoming contests.

type Problem

type Problem struct {
	ID          string
	Problem     string
	TimeLimit   string
	MemoryLimit string
	SolveCount  string
	SampleTests []sampleTest
	SolveStatus solveState
	Details     Args
}

Problem row in contest dashboard is parsed to this

type RegisterInfo added in v1.3.0

type RegisterInfo struct {
	Name     string
	Terms    string
	Register func() error
}

RegisterInfo holds data parsed from registration page of a contest. Holds details of contest name, terms of participation and a callback function to register if the user agrees to the terms.

type Submission

type Submission struct {
	ID        string
	When      string
	Who       string
	Problem   string
	Lang      string
	Verdict   string
	Time      string
	Memory    string
	IsRunning bool
	Details   Args
}

Submission row data is parsed to this struct

func (*Submission) GetSourceCode

func (sub *Submission) GetSourceCode() (string, error)

GetSourceCode extracts and returns source code of submission specified in args. If submission is not openly visible, ensure SessCln matches active user session of user's submission.

Groups are currently not supported (due to a bug on codeforces).

Jump to

Keyboard shortcuts

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