fuzzyworld

quantum-inspiration / files / wiki.go

wiki.go

Blame: the commit that last changed each line · file · history

e58f6fff 2023-09-06 Pavel Safronov1package main
2
3import (
4 "encoding/json"
5 "fmt"
6)
7
0c14bc4f 2023-09-06 Pavel Safronov8type WikiPage struct {
9 Title string
10 Url string
e58f6fff 2023-09-06 Pavel Safronov11}
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
0c14bc4f 2023-09-06 Pavel Safronov24func getRandomWikipediaPages(count int) (pages []WikiPage, err error) {
e58f6fff 2023-09-06 Pavel Safronov25 url := fmt.Sprintf("https://en.wikipedia.org/w/api.php?format=json&action=query&generator=random&grnnamespace=0&grnlimit=%d", count)
0c14bc4f 2023-09-06 Pavel Safronov26 body, err := getCachedUrl(url, "wikipedia-randoms", "2006-01-02-15")
e58f6fff 2023-09-06 Pavel Safronov27 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 {
62dbc0bc 2025-08-31 Pavel Safronov34 return nil, fmt.Errorf("error unmarshalling Wikipedia JSON: '%s' %v", body, err)
e58f6fff 2023-09-06 Pavel Safronov35 }
36
0c14bc4f 2023-09-06 Pavel Safronov37 pages = []WikiPage{}
e58f6fff 2023-09-06 Pavel Safronov38 for pageID := range wikiResponse.Query.Pages {
39 title := wikiResponse.Query.Pages[pageID].Title
a60fde4f 2023-09-06 Pavel Safronov40 url := fmt.Sprintf("http://en.m.wikipedia.org/?curid=%s", pageID)
0c14bc4f 2023-09-06 Pavel Safronov41 pages = append(pages, WikiPage{Title: title, Url: url})
e58f6fff 2023-09-06 Pavel Safronov42 }
43
0c14bc4f 2023-09-06 Pavel Safronov44 return pages, nil
e58f6fff 2023-09-06 Pavel Safronov45}