wikimedia

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2018 License: MIT Imports: 6 Imported by: 0

README

go-wikimedia

go-wikimedia is an interface to the Wikimedia (Wikipedia, Wiktionary, etc.) API implemented in the Go programming language.

This project was originally created by Patrick Mylund Nielsen. I forked his repo for my project pastime. If you notice a bug, feel free to submit an issue on this repo.

Installation

go get github.com/ciehanski/go-wikimedia

Documentation

https://godoc.org/github.com/ciehanski/go-wikimedia

or from the CLI:

go doc github.com/ciehanski/go-wikimedia

Usage

package main

import (
	"fmt"
	"log"
	"net/http"
	"net/url"
	
	"github.com/ciehanski/go-wikimedia"
)

func main() {
    wiki, err := wikimedia.New(wikimedia.Options{
    	Client:    http.DefaultClient,
    	URL:       "https://en.wikipedia.org/w/api.php",
    	UserAgent: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 " +
    		"(KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36",
    })
    if err != nil {
    	log.Fatal(err.Error())
    }
    resp, err := wiki.Query(url.Values{
        "action":      {"query"},
        "prop":        {"extracts"},
        "titles":      {"Osmosis|Procrastination"},
        "exsentences": {"5"},
        "explaintext": {"1"},
        "original":    {"source"},
    })
    if err != nil {
    	log.Fatalf("Error executing query: %s", err.Error())
    }
    for _, v := range resp.Query.Pages {
    	fmt.Println(v.Title, "-", v.Extract)
    }
}

Contributing

Please feel free to contribute and submit any PRs to this project.

License

MIT

Documentation

Overview

Package wikimedia is an interface to the Wikimedia (Wikipedia, Wiktionary, etc.) API built in Go.

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"`
	Thumbnail Thumbnail `json:"thumbnail"`
	Original  Original  `json:"original"`
}

ApiPage model struct as defined by any Wikimedia API JSON response.

type ApiQuery

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

ApiQuery model struct as defined by any Wikimedia API JSON response.

type ApiQueryContinue

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

ApiQueryContinue model struct as defined by any Wikimedia API JSON response.

type ApiQueryContinueSearch

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

ApiQueryContinueSearch model struct as defined by any Wikimedia API JSON response.

type ApiResponse

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

ApiResponse model struct as defined by any Wikimedia API JSON 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"`
}

ApiSearch model struct as defined by any Wikimedia API JSON response.

type ApiSearchInfo

type ApiSearchInfo struct {
	TotalHits int `json:"source"`
}

ApiSearchInfo model struct as defined by any Wikimedia API JSON response.

type Options

type Options struct {
	// Full URL of the Wikimedia API, e.g. url.Parse("https://en.wikipedia.org/w/api.php")
	URL string
	// HTTP client to use (defaults to http.DefaultClient)
	Client *http.Client
	// User-Agent header to provide
	UserAgent string
}

Options is a collection of configurable options for the Wikimedia client.

type Original

type Original struct {
	Source string `json:"source"`
}

Original model struct as defined by any Wikimedia API JSON response.

type Thumbnail

type Thumbnail struct {
	Source string `json:"source"`
}

Thumbnail model struct as defined by any Wikimedia API JSON response.

type Wikimedia

type Wikimedia struct {
	Options Options
}

Wikimedia is an API client struct.

func New

func New(options ...Options) (*Wikimedia, error)

New initializes a Wikimedia object that queries the specified API URL, e.g. http://en.wikipedia.org/w/api.php or http://da.wiktionary.org/w/api.php. Returns a pointer to a Wikimedia struct and an error.

func (*Wikimedia) Query

func (wiki *Wikimedia) Query(query url.Values) (*ApiResponse, error)

Query quires the Wikimedia API using the user-specified query. See https://en.wikipedia.org/w/api.php for a reference. Returns a pointer to an ApiResponse and an error.

Jump to

Keyboard shortcuts

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