gowiki

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2023 License: MIT Imports: 7 Imported by: 14

README

Go-wiki

go report card MIT license GoDoc

This is a Golang Wikipedia API wrapper - The Golang module that makes it easy to access and parse data from Wikipedia. You can use this module to crawl data for your data warehouse or use it for your "Know-it-all" AI Chatbot.

Contents

Instalation

To install Go-Wiki package, you need to install Go and set your Go workspace first.

  1. You first need Go installed, then you can use the below Go command to install Go-wiki.
go get -u github.com/trietmn/go-wiki
  1. Import it in your code.
import "github.com/trietmn/go-wiki"

Documentation

You can read the documentation at: https://pkg.go.dev/github.com/trietmn/go-wiki

I will update a full tutorial article on some popular blog as soon as possiple.

Quick start

# assume the following codes in example.go file
$ cat example.go
package main

import (
    "fmt"
    "github.com/trietmn/go-wiki"
)

func main() {
    // Search for the Wikipedia page title
    search_result, _, err := gowiki.Search("Why is the sky blue", 3, false)
    if err != nil {
        fmt.Println(err)
    }
    fmt.Printf("This is your search result: %v\n", search_result)

    // Get the page
    page, err := gowiki.GetPage("Rafael Nadal", -1, false, true)
    if err != nil {
        fmt.Println(err)
    }

    // Get the content of the page
    content, err := page.GetContent()
    if err != nil {
        fmt.Println(err)
    }
    fmt.Printf("This is the page content: %v\n", content)
}
# run example.go
$ go run example.go

Functions Examples

Note: The functions below are functions that you would usually use. Read the document to see all the functions.

search_result, suggestion, err := gowiki.Search("Why is the sky blue", 3, true)
if err != nil {
    fmt.Println(err)
}
fmt.Printf("Search result: %v\n", search_result)
fmt.Printf("Suggestion: %v\n", suggestion)
2. GetPage (There are multiple page methods in the Wikipedia Page Methods)
page, err := gowiki.GetPage("Rafael Nadal", -1, false, true)
if err != nil {
    fmt.Println(err)
}
// Then now you can use the page methods
3. Suggest
suggestion, err := gowiki.Suggest("nadal")
if err != nil {
    fmt.Println(err)
}
fmt.Printf("Suggestion: %v\n", suggestion)
4. Geosearch
res, err := gowiki.GeoSearch(40.67693, 117.23193, -1, "", -1)
if err != nil {
    fmt.Println(err)
}
fmt.Printf("Geosearch result: %v\n", res)
5. GetRandom
res, err := gowiki.GetRandom(5)
if err != nil {
    fmt.Println(err)
}
fmt.Printf("Random titles: %v\n", res)
6. Summary
res, err := gowiki.Summary("Rafael Nadal", 5, -1, false, true)
if err != nil {
    fmt.Println(err)
}
fmt.Printf("Summary: %v\n", res)

Wikipedia Page Methods

Methods Description Example
Equal Check if 2 pages are equal to each other page1.Equal(page2)
GetContent Get the page content page.GetContent()
GetHTML Get the page HTML page.GetHTML()
GetRevisionID Get revid field of a page page.GetRevisionID()
GetParentID Get parentid field of a page page.GetParentID()
GetSummary Get the summary of the page page.GetSummary()
GetImagesURL Get all of the image URL appear in the page page.GetImageURL()
GetCoordinate Get the page coordinate if exist page.GetCoordinate()
GetReference Get all of the extenal links in the page page.GetReference()
GetLink Get all the titles of Wikipedia page links on a page page.GetLink()
GetCategory Get all of the categories of a page page.GetCategory()
GetSectionList Get all of the sections of the page page.GetSectionList()
GetSection Get the content of a specific section in the page page.GetSection("History")

License

MIT licensed. See the LICENSE file for full details.

About me

Connect with me on Linkedin: https://www.linkedin.com/in/triet-m-nguyen-a94b4b20b/

Credit

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GeoSearch

func GeoSearch(latitude float32, longitude float32, radius float32, title string, limit int) ([]string, error)

Do a wikipedia geo search for `latitude` and `longitude` using HTTP API described in http://www.mediawiki.org/wiki/Extension:GeoData

Arguments:

* latitude: Latitude of the searched place

* longitude: longitude of the searched place

* title(optional): The title of an article to search for. Use "" to use the default setting

* limit(optional): The maximum number of results returned. Use -1 to use the default setting

* radius(optional): Search radius in meters. The value must be between 10 and 10000. Use -1 to use the default setting

Return:

* A slice of geosearch titles

* Error

func GetAvailableLanguage

func GetAvailableLanguage() (map[string]string, error)

List all the currently supported language prefixes (usually ISO language code). Can be inputted to `set_lang` to change the Mediawiki that `wikipedia` requests results from.

Returns: Map of <prefix>: <local_lang_name> pairs.

func GetPage

func GetPage(title string, pageid int, suggest bool, redirect bool) (page.WikipediaPage, error)

Get a WikipediaPage object for the page with title `title` or the pageid `pageid` (mutually exclusive).

Keyword arguments:

* title: The title of the page to load

* pageid: The numeric pageid of the page to load

* auto_suggest: Let Wikipedia find a valid page title for the query. Default should be False

* redirect: Allow redirection. Default should be True

Return:

* A WikipediaPage object

* Error

func GetRandom

func GetRandom(limit int) ([]string, error)

Get a list of random Wikipedia article titles.

**Note:: Random only gets articles from namespace 0, meaning no Category, User talk, or other meta-Wikipedia pages.

Keyword arguments:

* limit: The number of random pages returned (max of 10)

func Search(_input string, limit int, suggest bool) ([]string, string, error)

Do a Wikipedia search for `query`.

Keyword arguments:

* _input: The query used to search Ex:"Who invented the lightbulb"

* limit: The maxmimum number of results returned. Use -1 to use default setting

* suggest: If True, return results and suggestion (if any) in a tuple. Fasle is defalt

Return:

* A slice of Wikipedia titles from the search engine

* Suggestion if `suggest` is being set True

* Error

func SetCacheDuration

func SetCacheDuration(x time.Duration)

Change the max duration of the request responses exist in the Cache

func SetLanguage

func SetLanguage(lang string)

Change the language of the API being requested. Set `prefix` to one of the two letter prefixes found on the list of all Wikipedia <http://meta.wikimedia.org/wiki/List_of_Wikipedias>. Then clear all of the cache

func SetMaxCacheMemory

func SetMaxCacheMemory(n int)

Change the max number of the request responses stored in the Cache

func SetURL added in v1.0.1

func SetURL(url string)

Change the language of the API being requested. Set `prefix` to one of the two letter prefixes found on the list of all Wikipedia <http://meta.wikimedia.org/wiki/List_of_Wikipedias>. Then clear all of the cache

func SetUserAgent

func SetUserAgent(user string)

Change the user-agent that you use to crawl Wikipedia data

func Suggest

func Suggest(_input string) (string, error)

Get a Wikipedia search suggestion for `_input`.

Returns a string or "" if no suggestion was found.

func Summary

func Summary(title string, numsentence int, numchar int, suggest bool, redirect bool) (string, error)

Return a string summary of a page

**Note:: This is a convenience wrapper - auto_suggest and redirect are enabled by default\

Keyword arguments:

* title: Title of the page you want to get the summary

* numsentence: If set, return the first `numsentence` sentences (can be no greater than 10).

* numchar: If set, return only the first `numchar` characters (actual text returned may be slightly longer).

* auto_suggest: Let Wikipedia find a valid page title for the query. Default is False

* redirect: Allow redirection without raising RedirectError. Defalt is True

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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