goforces

package module
v0.0.0-...-0f6e605 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2022 License: MIT Imports: 16 Imported by: 0

README

goforces

Build Status GoDocGo Report Card

goforces is go package for the codeforces(http://codeforces.com/) api. Check the Usage

Install

go get github.com/TimurQQ/goforces

Usage

package main

import (
    "context"
    "fmt"

    "github.com/TimurQQ/goforces"
)

func main() {
    ctx := context.Background()
    //logger
    logger := log.New(os.Stderr, "*** ", log.LstdFlags)
    //Codeforces client
    api, _ := goforces.NewClient(logger)
    //Problems
    problems, _ := api.GetProblemSetProblems(ctx, &goforces.ProblemSetProblemsOptions{Tags: []string{"dp"}})
    fmt.Printf("%+v\n", problems)

    //Contest list
    contestList, _ := api.GetContestList(ctx, nil)
    fmt.Printf("%+v\n", contestList)

    //If you use authorized methods, you must set your key and secret
    api.SetAPIKey("<your key>")
    api.SetAPISecret("<your secret>")
    //User friends
    friends, _ := api.GetUserFriends(ctx, nil)
    fmt.Printf("%+v\n", friends)
}

The official codeforces api documentation is here

Documention

Read Godoc

License

MIT License

Documentation

Overview

Package goforces provides tructs and functions for accessing the codeforces API. If the API query is successful, return Go struct.

Queries

Executing queries on Codeforces API is very simple. Almost all of the methods don't require authentication.

logger := log.New(os.Stderr, "*** ", log.LstdFlags)
api, _ := goforces.NewClient(logger)
ctx := context.Background()
problems, _ := api.GetProblemSetProblems(ctx, &goforces.ProblemSetProblemsOptions{Tags:[]string{"dp", "math"}})
for _, problem := range problems.Problems {
	fmt.Printf("%+v\n", problem)
}

Endpoints

Goforces implements almost all of the endpoints defined in the Codeforces API(http://codeforces.com/api/help) More detailed information about the behavior of endpoint and the parameters can be found at the official Codeforces API documentation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BlogEntry

type BlogEntry struct {
	OriginalLocale          string   `json:"originalLocale"`
	AllowViewHistory        bool     `json:"allowViewHistory"`
	CreationTimeSeconds     int      `json:"creationTimeSeconds"`
	Rating                  int      `json:"rating"`
	AuthorHandle            string   `json:"authorHandle"`
	ModificationTimeSeconds int      `json:"modificationTimeSeconds"`
	ID                      int      `json:"id"`
	Title                   string   `json:"title"`
	Locale                  string   `json:"locale"`
	Content                 string   `json:"content"`
	Tags                    []string `json:"tags"`
}

BlogEntry represents a Codeforces BlogEntry

type Client

type Client struct {
	APIKey     string
	APISecret  string
	URL        *url.URL
	HTTPClient *http.Client
	Logger     *log.Logger
}

Client manages the access for the Codeforces API.

func NewClient

func NewClient(logger *log.Logger) (*Client, error)

NewClient takes a logger and return a Client struct. The Client struct can be use for accessing the endpoints

func (*Client) GetBlogEntryComments

func (c *Client) GetBlogEntryComments(ctx context.Context, blogEntryID int) ([]Comment, error)

GetBlogEntryComments implements /blogEntry.comments

func (*Client) GetBlogEntryView

func (c *Client) GetBlogEntryView(ctx context.Context, blogEntryID int) (*BlogEntry, error)

GetBlogEntryView implements /blogEntry.view

func (*Client) GetContestHacks

func (c *Client) GetContestHacks(ctx context.Context, contestID int) ([]Hack, error)

GetContestHacks implements /contest.hacks

func (*Client) GetContestList

func (c *Client) GetContestList(ctx context.Context, options *ContestListOptions) ([]Contest, error)

GetContestList implements /contest.list

func (*Client) GetContestRatingChanges

func (c *Client) GetContestRatingChanges(ctx context.Context, contestID int) ([]RatingChange, error)

GetContestRatingChanges implements /contest.ratingChanges

func (*Client) GetContestStandings

func (c *Client) GetContestStandings(ctx context.Context, contestID int, options *ContestStatndingsOptions) (*Standings, error)

GetContestStandings implements /contest.standings

func (*Client) GetContestStatus

func (c *Client) GetContestStatus(ctx context.Context, contestID int, options *ContestStatusOptions) ([]Submission, error)

GetContestStatus implements /contest.status

func (*Client) GetProblemSetProblems

func (c *Client) GetProblemSetProblems(ctx context.Context, options *ProblemSetProblemsOptions) (*Problems, error)

GetProblemSetProblems implements /problemset.problems

func (*Client) GetProblemSetRecentStatus

func (c *Client) GetProblemSetRecentStatus(ctx context.Context, count int) ([]Submission, error)

GetProblemSetRecentStatus implements /problemset.recentStatus

func (*Client) GetRecentActions

func (c *Client) GetRecentActions(ctx context.Context, maxCount int) ([]RecentAction, error)

GetRecentActions implements /recentActions

func (*Client) GetUserBlogEntries

func (c *Client) GetUserBlogEntries(ctx context.Context, handle string) ([]BlogEntry, error)

GetUserBlogEntries implements /user.blogEntries

func (*Client) GetUserFriends

func (c *Client) GetUserFriends(ctx context.Context, options *UserFriendsOptions) ([]string, error)

GetUserFriends implements /user.friends You must your api key and secret key before call this method.

func (*Client) GetUserInfo

func (c *Client) GetUserInfo(ctx context.Context, handles []string) ([]User, error)

GetUserInfo implements /user.info

func (*Client) GetUserRatedList

func (c *Client) GetUserRatedList(ctx context.Context, options *UserRatedListOptions) ([]User, error)

GetUserRatedList implements /user.ratedList

func (*Client) GetUserRating

func (c *Client) GetUserRating(ctx context.Context, handle string) ([]RatingChange, error)

GetUserRating implements /user.rating

func (*Client) GetUserStatus

func (c *Client) GetUserStatus(ctx context.Context, handle string, options *UserStatusOptions) ([]Submission, error)

GetUserStatus implements /user.status

func (*Client) SetAPIKey

func (c *Client) SetAPIKey(apiKey string)

SetAPIKey takes an user api key. If you use authorized methods, you must set it.

func (*Client) SetAPISecret

func (c *Client) SetAPISecret(apiSecret string)

SetAPISecret takes an user key secret. If you use authorized methods, you must set it.

type Comment

type Comment struct {
	ID                  int    `json:"id"`
	CreationTimeSeconds int    `json:"creationTimeSeconds"`
	CommentatorHandle   string `json:"commentatorHandle"`
	Locale              string `json:"locale"`
	Text                string `json:"text"`
	Rating              int    `json:"rating"`
	ParentCommentID     int    `json:"parentCommentId,omitempty"`
}

Comment represents a Codeforces comment

type Contest

type Contest struct {
	DurationSeconds     int64  `json:"durationSeconds"`
	Frozen              bool   `json:"frozen"`
	ID                  int64  `json:"id"`
	Name                string `json:"name"`
	Phase               string `json:"phase"`
	RelativeTimeSeconds int64  `json:"relativeTimeSeconds"`
	StartTimeSeconds    int64  `json:"startTimeSeconds"`
	Type                string `json:"type"`
}

Contest represents a Codeforces Contest

func (Contest) Before

func (c Contest) Before() bool

Before returns boolean whether contest hasn't started.

func (Contest) Coding

func (c Contest) Coding() bool

Coding returns boolean whether contest is now being held.

func (Contest) ContestURL

func (c Contest) ContestURL() string

ContestURL returns the contest's url.

func (Contest) Div2

func (c Contest) Div2() bool

Div2 returns boolean whether contest'name contains "Div. 2"

func (Contest) Finished

func (c Contest) Finished() bool

Finished returns boolean whether contest was over

type ContestListOptions

type ContestListOptions struct {
	Gym bool `url:"gym"`
}

ContestListOptions represents the option of /contest.list

type ContestStatndingsOptions

type ContestStatndingsOptions struct {
	From           int
	Count          int
	Handles        []string
	Room           int
	ShowUnofficial bool
}

ContestStatndingsOptions represents the option of /contest.standings

type ContestStatusOptions

type ContestStatusOptions struct {
	From   int
	Count  int
	Handle string
}

ContestStatusOptions represents the option of /contest.status

type Hack

type Hack struct {
	CreationTimeSeconds int64         `json:"creationTimeSeconds"`
	Defender            Party         `json:"defender"`
	Hacker              Party         `json:"hacker"`
	ID                  int64         `json:"id"`
	JudgeProtocol       JudgeProtocol `json:"judgeProtocol"`
	Problem             Problem       `json:"problem"`
	Test                string        `json:"test"`
	Verdict             string        `json:"verdict"`
}

Hack represents Codeforces Hack

type JudgeProtocol

type JudgeProtocol struct {
	Manual   string `json:"manual"`
	Protocol string `json:"protocol"`
	Verdict  string `json:"verdict"`
}

JudgeProtocol represents Codefoces JudgeProtocol

type Member

type Member struct {
	Handle string `json:"handle"`
}

Member represents Codeforces Member

type Party

type Party struct {
	ContestID        int      `json:"contestId"`
	Members          []Member `json:"members"`
	ParticipantType  string   `json:"participantType"`
	Ghost            bool     `json:"ghost"`
	StartTimeSeconds int      `json:"startTimeSeconds"`
}

Party represents Codeforces Party

type Problem

type Problem struct {
	ContestID int      `json:"contestId"`
	Index     string   `json:"index"`
	Name      string   `json:"name"`
	Type      string   `json:"type"`
	Points    float32  `json:"points"`
	Rating    int      `json:"rating"`
	Tags      []string `json:"tags"`
}

Problem represents Codeforces problem

func (Problem) ProblemURL

func (p Problem) ProblemURL() string

ProblemURL returns problem's url

type ProblemResult

type ProblemResult struct {
	Points                    float64 `json:"points"`
	Penalty                   int     `json:"penalty,omitempty"`
	RejectedAttemptCount      int64   `json:"rejectedAttemptCount"`
	Type                      string  `json:"type"`
	BestSubmissionTimeSeconds int64   `json:"bestSubmissionTimeSeconds"`
}

ProblemResult represents codeforces ProblemResult

type ProblemSetProblemsOptions

type ProblemSetProblemsOptions struct {
	Tags []string
}

ProblemSetProblemsOptions specifies the optional parameters of the problemset.problems

type ProblemSetRecentStatus

type ProblemSetRecentStatus struct {
	Count int
}

ProblemSetRecentStatus specifies the optional parameters of the problemset.recentStatus

type ProblemStatistics

type ProblemStatistics struct {
	ContestID   int    `json:"contestId"`
	Index       string `json:"index"`
	SolvedCount int    `json:"solvedCount"`
}

ProblemStatistics represents Codeforces ProblemStatistics

type Problems

type Problems struct {
	Problems          []Problem           `json:"problems"`
	ProblemStatistics []ProblemStatistics `json:"problemStatistics"`
}

Problems represents the response from /problemset.problems

type RanklistRow

type RanklistRow struct {
	Party                     Party           `json:"party"`
	Rank                      int64           `json:"rank"`
	Points                    float64         `json:"points"`
	Penalty                   int64           `json:"penalty"`
	SuccessfulHackCount       int64           `json:"successfulHackCount"`
	UnsuccessfulHackCount     int64           `json:"unsuccessfulHackCount"`
	ProblemResults            []ProblemResult `json:"problemResults"`
	LastSubmissionTimeSeconds int64           `json:"lastSubmissionTimeSeconds"`
}

RanklistRow represents Codeforces RanklistRow

type RatingChange

type RatingChange struct {
	ContestID               int64  `json:"contestId"`
	ContestName             string `json:"contestName"`
	Handle                  string `json:"handle"`
	NewRating               int    `json:"newRating"`
	OldRating               int    `json:"oldRating"`
	Rank                    int64  `json:"rank"`
	RatingUpdateTimeSeconds int64  `json:"ratingUpdateTimeSeconds"`
}

RatingChange represents Codeforces RatingChange

func (RatingChange) RatingDiff

func (r RatingChange) RatingDiff() int

RatingDiff returns NewRating - OldRating

type RecentAction

type RecentAction struct {
	TimeSeconds int       `json:"timeSeconds"`
	BlogEntry   BlogEntry `json:"blogEntry"`
	Comment     Comment   `json:"comment,omitempty"`
}

RecentAction represents Codeforces RecentAction

type Standings

type Standings struct {
	Contest  Contest       `json:"contest"`
	Problems []Problem     `json:"problems"`
	Rows     []RanklistRow `json:"rows"`
}

Standings represents Codeforces Standings

type Submission

type Submission struct {
	ID                  int     `json:"id"`
	ContestID           int     `json:"contestId"`
	CreationTimeSeconds int64   `json:"creationTimeSeconds"`
	RelativeTimeSeconds int64   `json:"relativeTimeSeconds"`
	Problem             Problem `json:"problem"`
	Author              Party   `json:"author"`
	ProgrammingLanguage string  `json:"programmingLanguage"`
	Verdict             string  `json:"verdict"`
	Testset             string  `json:"testset"`
	PassedTestCount     int     `json:"passedTestCount"`
	TimeConsumedMillis  int     `json:"timeConsumedMillis"`
	MemoryConsumedBytes int     `json:"memoryConsumedBytes"`
}

Submission represents a Codeforces Submission

func (*Submission) AC

func (s *Submission) AC() bool

AC returns boolean whether submission passed all test cases

type User

type User struct {
	LastName                string `json:"lastName"`
	Country                 string `json:"country"`
	LastOnlineTimeSeconds   int    `json:"lastOnlineTimeSeconds"`
	City                    string `json:"city"`
	Rating                  int    `json:"rating"`
	FriendOfCount           int    `json:"friendOfCount"`
	TitlePhoto              string `json:"titlePhoto"`
	Handle                  string `json:"handle"`
	Avatar                  string `json:"avatar"`
	FirstName               string `json:"firstName"`
	Contribution            int    `json:"contribution"`
	Organization            string `json:"organization"`
	Rank                    string `json:"rank"`
	MaxRating               int    `json:"maxRating"`
	RegistrationTimeSeconds int    `json:"registrationTimeSeconds"`
	MaxRank                 string `json:"maxRank"`
}

User represents Codeforces User

func (User) Color

func (u User) Color() string

Color returns user's color of rating. http://codeforces.com/blog/entry/20638

func (User) Div1

func (u User) Div1() bool

Div1 returns boolean whether user belog to div1

func (User) Div2

func (u User) Div2() bool

Div2 returns boolean whether user belog to div2

type UserFriendsOptions

type UserFriendsOptions struct {
	OnlyOnline bool
}

UserFriendsOptions represents the opetions of /user.friends

type UserRatedListOptions

type UserRatedListOptions struct {
	ActiveOnly bool
}

UserRatedListOptions represents the options of /user.ratedlist

type UserStatusOptions

type UserStatusOptions struct {
	From  int
	Count int
}

UserStatusOptions represents the opetions of /user.status

Jump to

Keyboard shortcuts

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