opentdb

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package opentdb is the library behind the opentdb command line: the HTTP client, request shaping, and the typed data models for the Open Trivia Database (opentdb.com).

The Client sets a real User-Agent, paces requests so a busy session stays polite, and retries transient failures (429 and 5xx). Questions and Categories are the two endpoints this client covers; no API key required.

Index

Constants

View Source
const Host = "opentdb.com"

Host is the site this client talks to.

Variables

This section is empty.

Functions

This section is empty.

Types

type Category added in v0.1.1

type Category struct {
	Rank int    `json:"rank"`
	ID   int    `kit:"id" json:"id"`
	Name string `json:"name"`
}

Category is one entry from the categories endpoint.

type CategoryCount added in v0.1.1

type CategoryCount struct {
	CategoryID int `kit:"id" json:"category_id"`
	Total      int `json:"total"`
	Easy       int `json:"easy"`
	Medium     int `json:"medium"`
	Hard       int `json:"hard"`
}

CategoryCount holds the question count breakdown for one category.

type Client

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

Client talks to opentdb over HTTP.

func NewClient

func NewClient(cfg Config) *Client

NewClient returns a Client configured with cfg.

func (*Client) Categories added in v0.1.1

func (c *Client) Categories(ctx context.Context) ([]Category, error)

Categories fetches the complete list of trivia categories.

func (*Client) Count added in v0.1.1

func (c *Client) Count(ctx context.Context, categoryID int) (CategoryCount, error)

Count fetches the question count breakdown for a given category ID.

func (*Client) Questions added in v0.1.1

func (c *Client) Questions(ctx context.Context, amount int, category int, difficulty string, qtype string) ([]Question, error)

Questions fetches trivia questions from opentdb. amount must be 1–50. Pass 0 for category, "" for difficulty and qtype to use the API defaults (all categories, all difficulties, all types). The request uses encode=url3986 so that special characters arrive as percent-encoded bytes; each string field is decoded with url.QueryUnescape.

type Config added in v0.1.1

type Config struct {
	BaseURL   string
	UserAgent string
	Rate      time.Duration
	Timeout   time.Duration
	Retries   int
}

Config holds all tunable parameters for the Client.

func DefaultConfig added in v0.1.1

func DefaultConfig() Config

DefaultConfig returns a Config with sensible defaults.

type Domain

type Domain struct{}

Domain is the opentdb driver.

func (Domain) Classify

func (Domain) Classify(input string) (uriType, id string, err error)

Classify turns an input into the canonical (type, id). A numeric input maps to type "category"; anything else maps to "query".

func (Domain) Info

func (Domain) Info() kit.DomainInfo

Info describes the scheme, the hostnames a pasted link is matched against, and the identity reused for the binary's help and version.

func (Domain) Locate

func (Domain) Locate(uriType, id string) (string, error)

Locate returns the live https URL for a (type, id).

func (Domain) Register

func (Domain) Register(app *kit.App)

Register installs the client factory and every operation onto app.

type Question added in v0.1.1

type Question struct {
	Rank             int      `json:"rank"`
	Category         string   `kit:"id" json:"category"`
	Type             string   `json:"type"`              // "multiple" or "boolean"
	Difficulty       string   `json:"difficulty"`        // "easy", "medium", "hard"
	Question         string   `json:"question"`          // HTML-decoded
	CorrectAnswer    string   `json:"correct_answer"`    // HTML-decoded
	IncorrectAnswers []string `json:"incorrect_answers"` // HTML-decoded
}

Question is one trivia question from the Open Trivia Database.

Jump to

Keyboard shortcuts

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