commons

package
v0.1.0 Latest Latest
Warning

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

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

Documentation

Overview

Package commons is the library behind the commons command line: the HTTP client, request shaping, wire types, and the typed data models for Wikimedia Commons.

The Client is the spine every command shares. It sets a real User-Agent (required by Wikimedia policy), paces requests so a busy session stays polite, and retries the transient failures (429 and 5xx) that any public API throws under load. Build your endpoint calls and JSON decoding on top of it.

Index

Constants

View Source
const APIBase = "https://commons.wikimedia.org/w/api.php"

APIBase is the Wikimedia Commons MediaWiki API endpoint.

View Source
const BaseURL = "https://" + Host

BaseURL is the root of the wiki, used to build human-readable page URLs.

View Source
const Host = "commons.wikimedia.org"

Host is the commons hostname used for URI matching in domain.go.

Variables

This section is empty.

Functions

This section is empty.

Types

type Category

type Category struct {
	Title  string `json:"title"`
	PageID int    `json:"page_id"`
	NS     int    `json:"ns"`
}

Category is an entry returned by the category-members list.

type Client

type Client struct {
	HTTP      *http.Client
	UserAgent string
	BaseURL   string
	Rate      time.Duration
	Retries   int
	// contains filtered or unexported fields
}

Client talks to the Wikimedia Commons API over HTTP.

func NewClient

func NewClient(cfg Config) *Client

NewClient returns a Client from cfg.

func (*Client) GetFile

func (c *Client) GetFile(ctx context.Context, title string) (*File, error)

GetFile fetches file info and metadata for a single file. title should be the bare filename without "File:" prefix; the prefix is added automatically.

func (*Client) ListCategory

func (c *Client) ListCategory(ctx context.Context, name string, limit int) ([]*Category, error)

ListCategory lists files that belong to a Wikimedia Commons category. name should be the bare category name without "Category:" prefix.

func (*Client) RecentUploads

func (c *Client) RecentUploads(ctx context.Context, limit int) ([]*File, error)

RecentUploads returns recently uploaded files.

func (*Client) SearchFiles

func (c *Client) SearchFiles(ctx context.Context, query string, limit, offset int) ([]*File, error)

SearchFiles searches Wikimedia Commons files by keyword. limit controls how many results to return; offset enables pagination.

type Config

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

Config holds tunable parameters for the Client.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns the recommended Config for production use.

type Domain

type Domain struct{}

Domain is the Wikimedia Commons driver. It carries no state; the per-run client is built by the factory Register hands kit.

func (Domain) Classify

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

Classify turns any accepted input into the canonical (uriType, id). For file URLs and "File:" prefixed titles the type is "file". For category URLs the type is "category".

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 is the inverse: the live https URL for a (uriType, id).

func (Domain) Register

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

Register installs the client factory and every operation onto app.

type File

type File struct {
	FileName    string `json:"file_name" kit:"id"`
	Title       string `json:"title"`
	PageID      int    `json:"page_id"`
	URL         string `json:"url,omitempty"`
	Size        int    `json:"size,omitempty"`
	MediaType   string `json:"media_type,omitempty"`
	Description string `json:"description,omitempty"`
	Author      string `json:"author,omitempty"`
	License     string `json:"license,omitempty"`
	Date        string `json:"date,omitempty"`
}

File is a Wikimedia Commons media file. FileName is the bare name without "File:" prefix; it doubles as the kit id.

Jump to

Keyboard shortcuts

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