src

package
v0.0.0-...-2ba3125 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2017 License: BSD-3-Clause Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Examination Year range
	MinYear = 13
	MaxYear = 29

	// Examination Season selections
	SeasonSpring = "haru"
	SeasonAutumn = "aki"
	SeasonAll    = "all" // used for QueryRange only.

	// Question No.
	MinNo = 1
	MaxNo = 80
)

These represents the minimum and maximum query range for the F.E. examination.

View Source
const JSONVersion = "1.0.0"

current version for json data structure.

View Source
const LeastIntervalTime = 5 * time.Second

the minimum time for request interval.

View Source
const VariationCoef = 2

interval time varies plus or minus VariationCoef second.

Variables

View Source
var (
	// source loacation for F.E. examination.
	FE = Source{
		URL: `http://www.fe-siken.com/kakomon/{{.Year}}_{{.Season}}/q{{.No}}.html`,
		QueryRange: QueryRange{
			MaxYear: 29, MinYear: 13,
			MaxNo: 80, MinNo: 1,
			Season: SeasonAll,
		},
	}

	// source location for A.P. examination.
	AP = Source{
		URL: `http://www.ap-siken.com/kakomon/{{.Year}}_{{.Season}}/q{{.No}}.html`,
		QueryRange: QueryRange{
			MaxYear: 29, MinYear: 13,
			MaxNo: 80, MinNo: 1,
			Season: SeasonAll,
		},
	}
)

Default Source definitions. You can use these to construct new Getter by:

fe_getter := src.NewGetter(src.FE, src.LeastIntervalTime)
View Source
var MaxQueryRange = QueryRange{-1, -1, -1, -1, SeasonAll}

MaxQueryRange indicates the maximum range of query in the source.

Functions

func GenerateURL

func GenerateURL(q Query) (string, error)

generate source URL with given query.

func MustURL

func MustURL(url string, err error) string

helper function to consume error for url string. it will panic if error is not nil.

func RandomURL

func RandomURL(qr QueryRange) (string, error)

generate random target URL. randomized range is defined by given QueryRange. use default range if MaxQueryRange is given.

Types

type Getter

type Getter struct {
	// contains filtered or unexported fields
}

Getter is a interface for F.E. question and answer from webpage. serial requests are splited by some interval time so that the number of accessing the outer server is reduced.

func NewGetter

func NewGetter(s Source, intervalTime time.Duration) *Getter

return new Getter with question source and intervalTime for server request. it will panic if intervalTime less than LeastIntervalTime.

func (*Getter) Get

func (g *Getter) Get(ctx context.Context, q Query) (Response, error)

Get returns a response, which contains F.E question and its answer selected by Query, from website. This process takes some time. You can cancel it by canceling context.

The interval wait time is inserted between serial calling of this method.

func (*Getter) GetRandom

func (g *Getter) GetRandom(ctx context.Context, qr QueryRange) (Response, error)

GetRandom returns a response, which contains F.E question and its answer selected randomly in range QueryRange, from website. This process takes some time. You can cancel it by canceling context.

The interval wait time is inserted between serial calling of this method. use maximum query range if MaxQueryRange is given.

type Query

type Query struct {
	Year   int
	Season string
	No     int
}

Query is a query for source URL. Its fields specifies which question is searched for.

type QueryRange

type QueryRange struct {
	MaxYear, MinYear int
	MaxNo, MinNo     int
	Season           string // SeasonSpring | SeasonAutumn | SeasonAll
}

QueryRange represents query range for randomly selected.

type Response

type Response struct {
	Question   string   `json:"question"`
	Selections []string `json:"selections"`

	Answer      string `json:"answer"`
	Explanation string `json:"explanation"`

	// indicates Question, Selections or Answer contain some image.
	// the response can not be represented by plain text only.
	HasImage bool `json:"hasImage"`

	URL string `json:"url"` // source URL

	Version string `json:"version"` // version for json data structure
}

Response is a result of parsing a web page that have the F.E. question and its answer.

func Get

func Get(ctx context.Context, q Query) (Response, error)

Get() returns a response, which contains F.E question and its answer selected by Query, from website. This process takes some time. You can cancel it by canceling context.

func GetRandom

func GetRandom(ctx context.Context, qr QueryRange) (Response, error)

GetRandom() returns a response, which is randomly selected F.E question and its answer, from website. This process takes some time. You can cancel it by canceling context. use maximum query range if MaxQueryRange is given.

func ParseHTML

func ParseHTML(html string) (Response, error)

ParseHTML is helper funtion which parses html text and converts to Response.

type Source

type Source struct {
	URL string // URL template for source server.

	QueryRange // Acceptable range for query.
}

Source is the source definition for getting the questions from the extenal server.

func (Source) Validates

func (src Source) Validates(q Query) error

check whether given query has correct value range in the source? nil error means query is valid.

func (Source) ValidatesRange

func (src Source) ValidatesRange(qr QueryRange) error

check whether QueryRange is in correct range in the Source? return nil if query is valid.

func (Source) ValidatesSelf

func (src Source) ValidatesSelf() error

check whether itself has correct values? return nil if it is valid.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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