123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- package main
-
- import (
- "cgt.name/pkg/go-mwclient"
- "cgt.name/pkg/go-mwclient/params"
- "errors"
- "fmt"
- "strings"
- )
-
- func clientLogin(w *mwclient.Client, username, password string) error {
- token, err := w.GetToken("login")
- if err != nil {
- return err
- }
- v := params.Values{
- "action": "login",
- "lgname": username,
- "lgpassword": password,
- "lgtoken": token,
- }
- resp, err := w.Post(v)
- if err != nil {
- if strings.Index(err.Error(), "see action=clientlogin.") == -1 {
- return err
- }
- }
- lgResult, err := resp.GetString("login", "result")
- if err != nil {
- return fmt.Errorf("invalid API response: unable to assert login result to string")
- }
- if lgResult != "Success" {
- apierr := mwclient.APIError{Code: lgResult}
- if reason, err := resp.GetString("login", "reason"); err == nil {
- apierr.Info = reason
- }
- return apierr
- }
- return nil
- }
-
- type WikiClient struct {
- apiUrl string
- userName string
- password string
- }
-
- func CreateWikiClient(apiUrl string, userName string, password string) *WikiClient {
- return &WikiClient{apiUrl, userName, password}
- }
-
- func (wc *WikiClient) GetWikiPageContent(title string) (content string, err error) {
- w, err := mwclient.New(wc.apiUrl, "lamperiWikibot")
- if err != nil {
- return "", err
- }
- // Log in.
- err = clientLogin(w, wc.userName, wc.password)
- if err != nil {
- return "", err
- }
-
- // Specify parameters to send.
- parameters := map[string]string{
- "action": "query",
- "format": "json",
- "titles": title,
- "prop": "revisions",
- "rvprop": "content",
- }
-
- // Make the request.
- resp, err := w.Get(parameters)
- if err != nil {
- return "", err
- }
-
- pages, err := resp.GetObjectArray("query", "pages")
- if err != nil {
- return "", err
- }
- if len(pages) == 0 {
- return "", errors.New("Not pages in page JSON")
- }
- title, err = pages[0].GetString("title")
- if err != nil {
- return "", err
- }
- revisions, err := pages[0].GetObjectArray("revisions")
- if err != nil {
- return "", err
- }
- content, err = revisions[0].GetString("content")
- if err != nil {
- return "", err
- }
-
- return content, nil
- }
-
- func (wc *WikiClient) GetWikiPageSections(title string) ([]struct {
- title string
- index string
- }, error) {
- w, err := mwclient.New(wc.apiUrl, "lamperiWikibot")
- if err != nil {
- return nil, err
- }
- // Log in.
- err = clientLogin(w, wc.userName, wc.password)
- if err != nil {
- return nil, err
- }
-
- // Specify parameters to send.
- parameters := map[string]string{
- "action": "parse",
- "format": "json",
- "page": title,
- "prop": "sections",
- }
-
- // Make the request.
- resp, err := w.Get(parameters)
- if err != nil {
- return nil, err
- }
-
- sections, err := resp.GetObjectArray("parse", "sections")
- if err != nil {
- return nil, err
- }
- ret := make([]struct {
- title string
- index string
- }, 0, len(sections))
- for _, section := range sections {
- title, err := section.GetString("line")
- if err != nil {
- return nil, err
- }
- index, err := section.GetString("index")
- if err != nil {
- return nil, err
- }
- ret = append(ret, struct {
- title string
- index string
- }{title, index})
- }
- return ret, nil
- }
-
- func (wc *WikiClient) GetWikiPageSectionText(title string, sectionIndex string) (string, error) {
- w, err := mwclient.New(wc.apiUrl, "lamperiWikibot")
- if err != nil {
- return "", err
- }
- // Log in.
- err = clientLogin(w, wc.userName, wc.password)
- if err != nil {
- return "", err
- }
-
- // Specify parameters to send.
- parameters := map[string]string{
- "action": "parse",
- "format": "json",
- "page": title,
- "section": sectionIndex,
- "prop": "wikitext",
- }
-
- // Make the request.
- resp, err := w.Get(parameters)
- if err != nil {
- return "", err
- }
-
- wikiText, err := resp.GetString("parse", "wikitext")
- if err != nil {
- return "", err
- }
- return wikiText, nil
- }
-
- func (wc *WikiClient) EditWikiPageSection(title string, sectionIndex string, content string, summary string) (bool, error) {
- w, err := mwclient.New(wc.apiUrl, "lamperiWikibot")
- if err != nil {
- return false, err
- }
- // Log in.
- err = clientLogin(w, wc.userName, wc.password)
- if err != nil {
- return false, err
- }
-
- editToken, err := w.GetToken("csrf")
-
- // Specify parameters to send.
- parameters := map[string]string{
- "action": "edit",
- "title": title,
- "section": sectionIndex,
- "summary": summary,
- "text": content,
- "nocreate": "true",
- "bot": "true",
- "token": editToken,
- }
-
- // Make the request.
- resp, err := w.Post(parameters)
- if err != nil {
- return false, err
- }
-
- result, err := resp.GetString("edit", "result")
- if err != nil {
- return false, err
- }
-
- return result == "Success", nil
- }
|