fuzzyworld

quantum-inspiration / files / wiki.go

wiki.go

1.2 KB · history · blame

1package main
2
3import (
4 "encoding/json"
5 "fmt"
6)
7
8type WikiPage struct {
9 Title string
10 Url string
11}
12
13type WikipediaResponse struct {
14 BatchComplete string `json:"batchcomplete"`
15 Query struct {
16 Pages map[string]struct {
17 PageID int `json:"pageid"`
18 Ns int `json:"ns"`
19 Title string `json:"title"`
20 } `json:"pages"`
21 } `json:"query"`
22}
23
24func getRandomWikipediaPages(count int) (pages []WikiPage, err error) {
25 url := fmt.Sprintf("https://en.wikipedia.org/w/api.php?format=json&action=query&generator=random&grnnamespace=0&grnlimit=%d", count)
26 body, err := getCachedUrl(url, "wikipedia-randoms", "2006-01-02-15")
27 if err != nil {
28 return nil, fmt.Errorf("error fetching Wikipedia pages: %v", err)
29 }
30
31 var wikiResponse WikipediaResponse
32 err = json.Unmarshal(body, &wikiResponse)
33 if err != nil {
34 return nil, fmt.Errorf("error unmarshalling Wikipedia JSON: '%s' %v", body, err)
35 }
36
37 pages = []WikiPage{}
38 for pageID := range wikiResponse.Query.Pages {
39 title := wikiResponse.Query.Pages[pageID].Title
40 url := fmt.Sprintf("http://en.m.wikipedia.org/?curid=%s", pageID)
41 pages = append(pages, WikiPage{Title: title, Url: url})
42 }
43
44 return pages, nil
45}