|
Bots (short for robots) are external tools that access the wiki in any manner other than the usual web interface. MediaWiki has an application programming interface (API) for this purpose. This API is a protocol external tools can use to request data from a MediaWiki wiki or perform changes to it.
The term "bot" is somewhat confusing. It may refer to anything that accesses the API, from advanced global find-and-replace tools (such as AutoWikiBrowser, known as AWB) to frameworks that let users perform a broad variety of automated tasks (such as Pywikibot, known as PWB). AWB and PWB, mentioned above, are the most popular tools referred to as "bots". If you wish to use either (or both), visit the respective pages for details.
Most regular users shouldn't need to run bots. Moreover, running a bot may require programming knowledge. Automated changes to wikis should be coordinated with local admins or other staff.
Best practices
These practices are courteous to your fellow wiki editors and allow the bot operator to more easily track down and fix any errors made by their bot.
Bot user accounts
If you intend to automate a lot of changes, you should create a separate bot account. It's a good idea to name it so it's clear it's a bot, and that it's your bot. The easiest way to do so is to take your username and append "Bot" for the name of the bot account.
If you want to run a bot on a wiki, you should notify your wiki manager (or a bureaucrat, if there is one). They can add your bot to the bot user group.
Having a separate bot account is helpful for a few reasons. First, it lets the bot operator more easily recognize and revert their bot's edits if needed, without accidentally reverting any edits made by their primary account. Bot accounts can also be added to the bot user group. By default, API edits by bot users are hidden in recent changes, which reduces clutter on that page. Users can toggle showing bot edits in RC with the "Show/hide bots" option or the equivalent filter in the new RC interface. If recent changes are showing edits by bot accounts, any such edits will be marked with b
, like minor edits are marked with m
.
Edit summaries
Bots should be configured to leave summaries for the actions they perform. For example, the summary could say, "Replaced Template:Old with Template:New", or "Trimming trailing whitespace". Summaries generally help users see at a glance what changes are being made to pages, and for bot edits, summaries also help track down and revert errors.
Choosing between AutoWikiBrowser (AWB) or Pywikibot (PWB)
When getting started with automation, one of the first decisions you must make is which bot tool to use. For most users, the choice is between AutoWikiBrowser (AWB), and Pywikibot (PWB). While many people use both, depending on the task, each tool has its pros and cons.
AWB
Pros:
- Has a GUI instead of requiring to do everything through a command-line interface.
- Easier setup & lower barrier to entry as a result.
- Better at doing complex find-and-replace operations.
Cons:
- Limited selection of generators, so editing lists of pages with complex criteria sometimes requires several steps.
- AWB only has Windows executables, and needs Wine to run on Mac and Linux.
PWB
Pros:
- Can delete, move, and create pages easily instead of just editing.
- Once you are familiar with the command line interface & available options, it's fewer clicks to accomplish some of the tasks that both tools can do.
- Can use a wider variety of page list generators.
- Can be run on most operating systems without requiring extra tools.
Cons:
- More difficult to set up and more difficult to learn.
- Harder to preview changes before making them.
Custom bot development
Some advanced users choose to create their own bots and scripts to interact with the wiki. Such tools can be written in many languages, often JavaScript (to run in the browser) and Python (to work with PWB, or the mwclient and mwparserfromhell libraries). See the MediaWiki.org page on API clients for more information.
See also
- MediaWiki API documentation
- Creating a bot on Wikipedia
#api-and-scripting
channel on the Fandom/Gamepedia Discord server – if you want to write custom client code#bot-software
channel on the Fandom/Gamepedia Discord server – for users of existing tools such as AWB and PWB
PyWikiBot[]
这是为对PWB感兴趣的所写的,每步指引:
- 下载Python,至少python 3.x版本
- 检查Python有没有添加到你的系统路径。
;C:\Python3x
永久修改默认环境变量,请在开始菜单中搜索‘编辑系统环境变量’,或打开系统属性,高级系统设置,点击环境变量按钮。在对话框中,你可以增加或修改用户变量和系统变量。修改系统变量时,你需要对你的机器获得无限制的权限(比如管理员权限)
- 下载Request包,这是PWB所需要的python模组
- 从这里开始,你需要CMD,即windows控制台命令行。通过
python setup.py install
安装"request"模组。你可以通过cd
把当前路径修改为解压"request" setup.py时的路径
- 下载PWB并将其解压到任意位置(比如C:/Programs/PWB)
- 创建一个空的文件,命名为"dota2_family.py",放置在C:/Programs/PWB/pywikibot/families/目录下,并粘贴以下内容:
# -*- coding: utf-8 -*- from pywikibot import family from pywikibot.tools import deprecated class Family(family.Family): # noqa: D101 name = 'dota2' langs = { 'en': 'dota2.fandom.com', 'pt': 'dota2.fandom.com', 'ru': 'dota2.fandom.com', 'zh': 'dota2.fandom.com', } def scriptpath(self, code): return { 'en': '', 'pt': '/pt', 'ru': '/ru', 'zh': '/zh', }[code] def protocol(self, code): return 'HTTPS'
- 通过CMD运行PWB目录下的
python generate_user_files.py
,根据程序指引进行操作
- (可选)删除两个example文件
- 打开user-config.py并作出以下修改:这是我(Molldust)的修改,当然你可能不是管理员(sysop),也可能不需要这里列出的所有语言。根据自己需求修改即可:
# If you use either of these functions to define the family to work on by # default (the ‘family’ variable below), you must place the function call # before the definition of the ‘family’ variable. family = 'dota2' # The language code of the site we're working on. mylang = 'en' # The dictionary usernames should contain a username for each site where you # have a bot account. If you have a unique username for all languages of a # family , you can use '*' password_file = "user-password.py" usernames['dota2']['en'] = u'Username' usernames['dota2']['pt'] = u'Username' usernames['dota2']['ru'] = u'Username' usernames['dota2']['zh'] = u'Username' sysopnames['dota2']['en'] = u'Username' sysopnames['dota2']['pt'] = u'Username' sysopnames['dota2']['ru'] = u'Username' sysopnames['dota2']['zh'] = u'Username'
- 在这里设置新的机器人权限:Special:BotPasswords
- 创建一个新的密码文件,命名为
user-password.py
,放置在当前目录下,并粘贴以下内容。将<>内的内容替换为你在机器人权限中定义的:
("<用户名>", BotPassword("<机器人名称>", "<机器人密码>"))
- 所有步骤已经完成,你可以通过这条命令检查一下是否可用。这会列出所有包含"斯拉"的页面(比如,斯拉克和斯拉达):
python pwb.py listpages -format:3 -titleregex:"斯拉" -start:!
AutoWikiBot[]
mwclient[]
mwclient是一个更加轻量级版本的PyWikiBot。没有预先安装的脚本。
- 下载Python (版本 >2.7 即可)。
- 通过键入
pip3 install mwclient
(pip install mwclient
如果用的是Python 2.x)安装mwclient。 - 新建一个新的文本文件并另存为
.py
扩展名。 - 在你刚新建的文件中编写你的脚本。
- 通过键入
python3 path/to/your/file.py
(python path/to/your/file.py
Python version 2.x)运行。
说明[]
mwclient模块是基于Site()对象的。这个对象需要在你的脚本开始之前配置:
import mwclient site = mwclient.Site('dota2.gamepedia.com', path='/')
通过Site.login()方法验证你的账户。所需的机器人账号可以从Special:BotPasswords中创建。
site.login('bot_user_name', 'bot_password')
自此你就可以使用文档中提到的方法了。
示例[]
import mwclient site = mwclient.Site('dota2.gamepedia.com', path='/') site.login('bot_user_name', 'bot_password') page = site.Pages['Evil Geniuses'] text = page.text() text = text.replace('Arteezy', 'Fear') page.save(text, summary='Bot: Arteezy left the team.')
|