wikimedia

package module
v0.0.0-...-dcae5fa Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2018 License: MIT Imports: 5 Imported by: 2

README

go-wikimedia is an interface to the Wikimedia (Wikipedia, Wiktionary, etc.) API.

It is a work in progress currently used to build applications that query
Wikipedia and Wiktionary in different languages.

== Installation

go get github.com/pmylund/go-wikimedia

== Documentation

go doc github.com/pmylund/go-wikimedia
or http://go.pkgdoc.org/github.com/pmylund/go-wikimedia

== Usage

import (
	"fmt"
	"github.com/pmylund/go-wikimedia"
	"net/url"
)

w, err := wikimedia.New("http://en.wikipedia.org/w/api.php")
if err != nil {
	...
}
f := url.Values{
	"action": {"query"},
	"prop":   {"extracts"},
	"titles": {"Osmosis|Procrastination"},
}
res, err := w.Query(f)
if err != nil {
	return nil, err
}
for _, v := range res.Query.Pages {
	fmt.Println(v.Title, "-", v.Extract)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ApiPage

type ApiPage struct {
	PageId  int    `json:"pageid"`
	Ns      int    `json:"ns"`
	Title   string `json:"title"`
	Extract string `json:"extract"`
}

type ApiPageCategory

type ApiPageCategory struct {
	SortKey string `json:"sortkey"`
	Hidden  string `json:"hidden"`
	Name    string `json:"*"`
}

type ApiPageSection

type ApiPageSection struct {
	TOCLevel   int    `json:"toclevel"`
	Level      string `json:"level"`
	Line       string `json:"line"`
	Number     string `json:"number"`
	Index      string `json:"index"`
	FromTitle  string `json:"fromtitle"`
	ByteOffset int    `json:"byteoffset"`
	Anchor     string `json:"anchor"`
}

type ApiParse

type ApiParse struct {
	PageId     int               `json:"pageid"`
	Title      string            `json:"title"`
	Categories []ApiPageCategory `json:"categories"`
	Sections   []ApiPageSection  `json:"sections"`
}

type ApiQuery

type ApiQuery struct {
	Pages      map[string]ApiPage `json:"pages"`
	Search     []ApiSearch        `json:"search"`
	SearchInfo ApiSearchInfo      `json:"searchinfo"`
}

type ApiQueryContinue

type ApiQueryContinue struct {
	Search ApiQueryContinueSearch `json:"search"`
}

type ApiQueryContinueSearch

type ApiQueryContinueSearch struct {
	SrOffset int `json:"sroffset"`
}

type ApiResponse

type ApiResponse struct {
	Query         ApiQuery         `json:"query"`
	QueryContinue ApiQueryContinue `json:"query-continue"`
	Parse         ApiParse         `json:"parse"`
}

A Wikimedia API response

func (*ApiResponse) StripHtml

func (a *ApiResponse) StripHtml()

Strip all HTML tags from the response

type ApiSearch

type ApiSearch struct {
	Ns        int       `json:"ns"`
	Title     string    `json:"title"`
	Snippet   string    `json:"snippet"`
	Size      int       `json:"size"`
	WordCount int       `json:"wordcount"`
	Timestamp time.Time `json:"timestamp"`
}

type ApiSearchInfo

type ApiSearchInfo struct {
	Totalhits int `json:"totalhits"`
}

type Wikimedia

type Wikimedia struct {
	// Full URL of the Wikimedia API, e.g. url.Parse("http://en.wikipedia.org/w/api.php")
	Url *url.URL

	// Automatically strip HTML tags from API responses
	StripHtml bool

	// HTTP client to use (defaults to http.DefaultClient)
	Client *http.Client

	// User-Agent header to provide
	UserAgent string
	// contains filtered or unexported fields
}

A Wikimedia API client

func New

func New(apiUrl string) (*Wikimedia, error)

Set up a client that queries the specified API, e.g. http://en.wikipedia.org/w/api.php or http://da.wiktionary.org/w/api.php. Returns an error if the URL is invalid.

func (*Wikimedia) Query

func (w *Wikimedia) Query(vals url.Values) (*ApiResponse, error)

Queries the Wikimedia API using the specified values, and returns an ApiResponse. See http://en.wikipedia.org/w/api.php for a reference.

Jump to

Keyboard shortcuts

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