ybtools

package module
v2.0.0-...-90d7533 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 5, 2024 License: GPL-3.0 Imports: 14 Imported by: 0

README

Yapperbot Tools

A set of imports used by other Wikipedia bots I've created. These probably won't be of much use to anyone else, but you're welcome to them in accordance with the license if you like!

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultMaxlag mwclient.Maxlag = mwclient.Maxlag{
	On:      true,
	Timeout: "5",
	Retries: 3,
}

DefaultMaxlag is a Maxlag representing sensible defaults for any non-urgent task being run by a Yapperbot.

View Source
var NoMaxlag mwclient.Maxlag = mwclient.Maxlag{
	On:      true,
	Timeout: "5",
	Retries: 3,
}

NoMaxlag is a Maxlag for urgent tasks, where maxlag must be disabled.

Functions

func BotAllowed

func BotAllowed(pageContent string) bool

BotAllowed take a page content and determines if the botUser is allowed to edit the page per the applicable templates.

func CanEdit

func CanEdit() bool

CanEdit checks if the task has been killed, and then checks if the bot is edit limited. This *must* be used for all edits apart from those which only affect the bot's own userspace, or those which must run even if the task is killed (e.g. updating JSON files which describe the run which was just done).

func CreateAndAuthenticateClient

func CreateAndAuthenticateClient(maxlag mwclient.Maxlag) *mwclient.Client

CreateAndAuthenticateClient uses the details already passed into ybtools in setup.go to return a fully-authenticated mwclient

func EditLimit

func EditLimit() bool

EditLimit can be called to increment the current edit count Returns true if allowed to edit or false if not Remember to call SaveEditLimit at the end of the program if using this!

func FetchWikitext

func FetchWikitext(pageID string) (content string, err error)

FetchWikitext takes a pageId and gets the wikitext of that page. The default functionality in the library does not work for this in my experience; it just returns an empty string for some reason. So we're rolling our own!

func FetchWikitextFromTitle

func FetchWikitextFromTitle(pageTitle string) (content string, err error)

FetchWikitextFromTitle takes a title and gets the wikitext of that page.

func FetchWikitextFromTitleWithTimestamps

func FetchWikitextFromTitleWithTimestamps(pageTitle string) (content string, revtimestamp string, curtimestamp string, err error)

FetchWikitextFromTitleWithTimestamps takes a title and gets the wikitext of that page, also returning the revision timestamp and the current timestamp.

func FetchWikitextWithTimestamps

func FetchWikitextWithTimestamps(pageID string) (content string, revtimestamp string, curtimestamp string, err error)

FetchWikitextWithTimestamps takes a pageId and gets the wikitext of that page, also returning the revision timestamp and the current timestamp.

func ForPageInQuery

func ForPageInQuery(parameters params.Values, callback PageInQueryCallback)

ForPageInQuery takes parameters and a callback function. It then queries using the parameters it is given, and calls the callback function for every page in the query response.

func GetCategorisationTimestampFromPage

func GetCategorisationTimestampFromPage(page *jason.Object, category string) (timestamp string)

GetCategorisationTimestampFromPage takes a page, and gets the timestamp at which the page was categorised. All the errors in this function are Fatal, because frankly, if something's gone wrong with the timestamp reading, we're not really going to be able to run the algorithm correctly anyway.

func GetContentFromPage

func GetContentFromPage(page *jason.Object) (content string, err error)

GetContentFromPage turns a *jason.Object for a Page into the main slot content, and/or an applicable error.

Yes, there is a function to do this already in the library (GetPagesByName). No, I don't want to use it. Why? We've already got the page content here - making another request to get it again is wasteful when we could just locally parse what we already have.

func GetMainSlotFromRevision

func GetMainSlotFromRevision(revision *jason.Object) (string, error)

GetMainSlotFromRevision fetches the main slot content, given a revision object formatted per the MediaWiki Action API JSON.

func GetPagesFromQuery

func GetPagesFromQuery(resp *jason.Object) []*jason.Object

GetPagesFromQuery takes a query and returns an array of Pages. Convenience wrapper for GetThingFromQuery.

func GetThingFromQuery

func GetThingFromQuery(resp *jason.Object, thing string) ([]*jason.Object, error)

GetThingFromQuery takes a query and a key that's being looked for, and returns the inner thing array.

func LoadJSONFromPageID

func LoadJSONFromPageID(pageID string) *jason.Object

LoadJSONFromPageID takes a pageID, then loads and deserializes the contained JSON. It returns the deserialised JSON in a jason.Object pointer.

func LoadJSONFromPageTitle

func LoadJSONFromPageTitle(pageTitle string) *jason.Object

LoadJSONFromPageTitle takes a title string, then loads and deserializes the contained JSON. It returns the deserialised JSON in a jason.Object pointer.

func NoMaxlagDo

func NoMaxlagDo(f NoMaxlagFunction, w *mwclient.Client) error

NoMaxlagDo takes a function which returns an error (or nil), and an mwclient pointer, than executes that function with no maxlag. It returns the same return as the NoMaxlagFunction it's passed.

func PanicErr

func PanicErr(v ...interface{})

PanicErr panics the program with a specified message, also sending a message to the tool inbox on Toolforge explaining the issue.

func ParseTaskConfig

func ParseTaskConfig(cobj interface{})

ParseTaskConfig takes in a pointer to a config object and populates the config object with the task configuration

func SaveEditLimit

func SaveEditLimit()

SaveEditLimit saves the current edit limit to the edit limit file, assuming that there is an edit limit usage to save This function must be called at the end of the program for edit limiting to work

func SerializeToJSON

func SerializeToJSON(serializable interface{}) string

SerializeToJSON takes in any serializable object and returns the serialized JSON string

func SetupBot

func SetupBot(s BotSettings)

SetupBot sets the bot name, ready for future calls to BotAllowed.

Types

type BotSettings

type BotSettings struct {
	TaskName string
	BotUser  string
}

BotSettings is a struct storing all the information about the bot needed to make the tools library work.

type NoMaxlagFunction

type NoMaxlagFunction func() error

NoMaxlagFunction is the definition of a function accepted by NoMaxlagDo; it's just a function with no parameters that returns an error.

type PageInQueryCallback

type PageInQueryCallback func(pageTitle, pageContent, revTS, curTS string)

PageInQueryCallback is a function used as a callback for ForPageInQuery.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL