quantum-inspiration / files / wiki.go
wiki.go
e58f6fff 2023-09-06 Pavel Safronov | 1 | package main |
| 2 | ||
| 3 | import ( | |
| 4 | "encoding/json" | |
| 5 | "fmt" | |
| 6 | ) | |
| 7 | ||
0c14bc4f 2023-09-06 Pavel Safronov | 8 | type WikiPage struct { |
| 9 | Title string | |
| 10 | Url string | |
e58f6fff 2023-09-06 Pavel Safronov | 11 | } |
| 12 | ||
| 13 | type 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 Safronov | 24 | func getRandomWikipediaPages(count int) (pages []WikiPage, err error) { |
e58f6fff 2023-09-06 Pavel Safronov | 25 | 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 Safronov | 26 | body, err := getCachedUrl(url, "wikipedia-randoms", "2006-01-02-15") |
e58f6fff 2023-09-06 Pavel Safronov | 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 { | |
62dbc0bc 2025-08-31 Pavel Safronov | 34 | return nil, fmt.Errorf("error unmarshalling Wikipedia JSON: '%s' %v", body, err) |
e58f6fff 2023-09-06 Pavel Safronov | 35 | } |
| 36 | ||
0c14bc4f 2023-09-06 Pavel Safronov | 37 | pages = []WikiPage{} |
e58f6fff 2023-09-06 Pavel Safronov | 38 | for pageID := range wikiResponse.Query.Pages { |
| 39 | title := wikiResponse.Query.Pages[pageID].Title | |
a60fde4f 2023-09-06 Pavel Safronov | 40 | url := fmt.Sprintf("http://en.m.wikipedia.org/?curid=%s", pageID) |
0c14bc4f 2023-09-06 Pavel Safronov | 41 | pages = append(pages, WikiPage{Title: title, Url: url}) |
e58f6fff 2023-09-06 Pavel Safronov | 42 | } |
| 43 | ||
0c14bc4f 2023-09-06 Pavel Safronov | 44 | return pages, nil |
e58f6fff 2023-09-06 Pavel Safronov | 45 | } |