77 lines
1.8 KiB
Go
77 lines
1.8 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"`
|
|
Continue struct {
|
|
RvContinue string `json:"rvcontinue"`
|
|
Continue string `json:"continue"`
|
|
} `json:"continue"`
|
|
Query struct {
|
|
Pages map[string]struct {
|
|
Pageid int `json:"pageid"`
|
|
Ns int `json:"ns"`
|
|
Title string `json:"title"`
|
|
Revisions []Revision `json:"revisions"`
|
|
} `json:"pages"`
|
|
} `json:"query"`
|
|
}
|
|
|
|
type Revision struct {
|
|
Revid int `json:"revid"`
|
|
Parentid int `json:"parentid"`
|
|
User string `json:"user"`
|
|
Userid int `json:"userid"`
|
|
Timestamp string `json:"timestamp"`
|
|
Comment string `json:"comment"`
|
|
}
|
|
|
|
func main() {
|
|
params := url.Values{}
|
|
params.Set("action", "query")
|
|
params.Set("format", "json")
|
|
params.Set("prop", "revisions")
|
|
params.Set("titles", "Zinc")
|
|
params.Set("rvlimit", "max")
|
|
params.Set("rvprop", "timestamp|user|comment|ids")
|
|
|
|
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("Revisions for '%s':\n", page.Title)
|
|
for _, revision := range page.Revisions {
|
|
fmt.Printf("- ID: %d, Parent ID: %d, User: %s, UserID: %d, Timestamp: %s, Comment: %s\n",
|
|
revision.Revid, revision.Parentid, revision.User, revision.Userid, revision.Timestamp, revision.Comment)
|
|
}
|
|
}
|
|
}
|