wp2git/gowikipedia/getVersion.go

69 lines
2.1 KiB
Go

package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
)
const wikipediaAPI = "https://en.wikipedia.org/w/api.php"
type QueryResponse struct {
Batchcomplete string `json:"batchcomplete"`
Query struct {
Pages map[string]struct {
Pageid int `json:"pageid"`
Ns int `json:"ns"`
Title string `json:"title"`
Content string `json:"content"`
} `json:"pages"`
} `json:"query"`
}
// - ID: 824303964, Parent ID: 824303909, User: 204.122.110.237, UserID: 0, Timestamp: 2018-02-06T15:26:48Z, Comment: /* Physical properties */
// - ID: 824303909, Parent ID: 824303854, User: CLCStudent, UserID: 0, Timestamp: 2018-02-06T15:26:21Z, Comment: Reverted 1 edit by [[Special:Contributions/204.122.110.237|204.122.110.237]] ([[User talk:204.122.110.237|talk]]) to last revision by Onel5969. ([[WP:TW|TW]])
// - ID: 824303854, Parent ID: 824287944, User: 204.122.110.237, UserID: 0, Timestamp: 2018-02-06T15:26:01Z, Comment: /* Zinc(II) compounds */
// - ID: 824287944, Parent ID: 821850427, User: Onel5969, UserID: 0, Timestamp: 2018-02-06T13:21:44Z, Comment: Disambiguating links to [[Poppy]] (link changed to [[Poppy (flower)]]) using [[User:Qwertyytrewqqwerty/DisamAssist|DisamAssist]].
func main() {
params := url.Values{}
params.Set("action", "query")
params.Set("format", "json")
params.Set("prop", "revisions")
params.Set("titles", "Zinc")
params.Set("rvprop", "content")
params.Set("rvslots", "main")
params.Set("rvlimit", "1")
params.Set("revids", "824287944")
url := fmt.Sprintf("%s?%s", wikipediaAPI, params.Encode())
resp, err := http.Get(url)
if err != nil {
fmt.Printf("Error making request: %v\n", err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("Error reading response: %v\n", err)
return
}
var queryResponse QueryResponse
err = json.Unmarshal(body, &queryResponse)
if err != nil {
fmt.Printf("Error unmarshalling JSON: %v\n", err)
return
}
for _, page := range queryResponse.Query.Pages {
fmt.Printf("Content for '%s' (revision ID: 825282998):\n\n", page.Title)
fmt.Println(page.Content)
}
}