Pywikipediabot使用手冊

From Meta, a Wikimedia project coordination wiki
This is an archived version of this page, as edited by Alexsh (talk | contribs) at 03:00, 17 June 2008 (→‎權限). It may differ significantly from the current version.

In other languages: de - en - fr - hu - it - ja - ko - nl - pl - pt - ru - sv - zh-hant


Python Wikipediabot Framework (pywikipedia)是一組針對維基百科或維基媒體計劃等以MediaWiki架設的網站的自動執行更新工具。本頁將提供如何使用本軟體的一般資訊。

初始設定

PyWikipediaBot是使用Python撰寫而成的程式,您需要安裝最新版的Python程式到您的電腦中才可以執行Pywikipediabot。部份程式雖可以在Python 2.3版執行,但目前沒有計劃相容舊版本的Python。

ActivePython

ActivePython可以於任何的平臺上運作(Unix, Linux, Mac, Windows),它也可以執行Pywikipediabot的程式碼。

  • windows系統:連結到這裡下載並安裝ActivePython
  • Unix系統:一般來說,Python會預設安裝在大部份的Unix版本,因此不需要下載(除非您系統的Python版本太舊才需要)
  • Macintosh,Python已包含在Mac OS X中

下載

使用這個網站可以很輕易的下載到最近的Pywikipediabot版本。過去的舊版本可以在Sourceforge找到。下載並解壓縮這些檔案即可,不需要再做其他安裝手續。

Macintosh電腦請參閱此教學。Pywikipediabot的檔案可以在這裡找到。[1]

透過SVN下載

您可以使 用 SVN ([1] subversion.tigris.org)取得即時更新的Pywikipediabot資料。如果您使用Windows系統,我們建議您使用TortoiseSVN。Macintosh電腦可以參閱這個網頁

這樣將會在您目前的目錄下建立一個名為“pywikipedia”的目錄。

使用非命令列工具的使用者,請使用您的工具輸入這個位置:http://svn.wikimedia.org/svnroot/pywikipedia/trunk/pywikipedia/

設定

您不需要去安裝就可以使用。在解壓縮或是從SVN下載之後就可以執行。

在使用之前,您需要在您的pywikipedia目錄中建立一個名為user-config.py您可以自行建立並按照以下說明輸入設定,也可以執行generate_user_files.py並依照指示操作,它會幫助您完成這些程序。

要注意的是PyWikipediaBot只能使用預設的Monobook外觀。為維持Pywikipediabot的運作順暢,請不要在帳號的參數設置中更改這個設定。

設定(以下以Wikipedia為例)

開啟文字編輯器(Windows下的Notepad或是unix下的ee),將user-config.py存在pywikipedia的目錄下。

請在user-config.py增加以下兩行資料:

程式碼 解釋
mylang = 'xx'

xx是您準備執行的計劃的語言,例如en指的是英語,ja是日語,zh是漢語。[2]

usernames['wikipedia']['en'] = u'ExampleBot'
[3]

您的user-config.py檔案需要設定您的機器人用戶名稱。

在這個例子中,用戶是在英語維基百科上執行工作,且已建立一個名為"ExampleBot"的用戶。

(選項)

usernames['wikipedia']['de'] = u'BeispielBot'
usernames['wikipedia']['en'] = u'ExampleBot'
usernames['wiktionary']['de'] = u'BeispielBot'

(選項) 如果您要在一個以上的維基百科上執行,您需要增加數個用戶名稱。

輸入完成後請儲存user-config.py

其他MediaWiki網站設定

請增加以下三行程式碼到user-config.py

程式碼 解釋
mylang = 'xx'

xx是您準備執行的計劃的語言,例如en指的是英語,ja是日語,zh是漢語。[2]

 family = 'sitename'

"Sitename"是您要工作的網站名稱。

目前,這個選項可以在維基辭典、維基新聞、維基教科書、維基語錄等非Wikipedia維基百科的計劃,像Wikitravel。(所有清單可在pywikipedia/families目錄下找到)

 usernames['memoryalpha']['en'] = u'ExampleBot'

您的user-config.py檔案需要設定您的機器人用戶名稱。

在這個例子中,用戶是在英語Memory Alpha上執行工作,且已建立一個名為"ExampleBot"的用戶。

(以下只限維基共享資源)

mylang='commons'
family='commons'
usernames['commons']['commons']='UserBot'

(以下只限維基共享資源)

如果您要在維基共享資源或是元維基類的網站執行機器人,請mylang跟family都指定相同的family名稱

輸入完成後請儲存user-config.py

如果您的網站沒有在families目錄中,您需要自行建立您的fmailies.py,請參閱Pywikipedia bot on non-Wikimedia projects.

非維基媒體計劃的設定

請參閱Pywikipedia bot on non-wikimedia projects

權限

請確定您的機器人已經被該維基站的社群認可,每個計劃都有極大的不同。

請先使用你的瀏覽器,建立一個帳號密碼給這個機器人使用,名稱最好能讓人了解這是一個機器人帳號,以及是哪個用戶在操作它。通常的做法是拿您自己的用戶名稱後面加上"bot"這個字,但也可以有好幾種的組合存在。

在英文維基百科,機器人只能在被en:Wikipedia:Bots/Requests for approval通過並認可後才可以執行。

請求機器人標識

如果您經常的使用機器人,它的編輯會佔掉整個最近更改頁面,為避免這個問題,您可以向您執行的計劃申請標識,這樣這個用戶的編輯資訊不會顯示在最近更改頁面裡。

標識機器人用戶的動作由行政員執行,您可以在Requests for bot status提出您的或是其他人的機人帳號標識請求。不過請先在您執行機器人的本地社群中取得共識。英文維基百科的請求頁面是Requests for approvals。這樣讓機器人在一次程序中大量編輯頁面有正面的好處。S

Use

Select and run a bot script

Now we are ready to really start using the bot. You need to get to a textual interface to your Operating System.

On Windows this is done by opening the start menu, and clicking on 'Run'. You are asked to give the name of a program, type "cmd.exe".

  • Change the root to C: by typing chdir C:\
  • Type chdir \"name of the folder where pywikipedia bot has been downloaded" (For example: chdir \"pywikipedia" if the file is in the C: folder.)

On Mac, find Terminal.app in /Applications/Utilities.

On Linux or any other Unix, use any terminal application such as gnome-terminal, konsole, xterm, or simply the text-mode console.

Run the script login.py by typing "python login.py".

Python will then return:

Checked for running processes. 1 processes currently running, including the current process.
Password for user your_bot on your_site:en:

Use the password you used for the bot's login name. The bot can't work anonymously. Unless you change your password, you normally need to run this program only once, the bot usually does not get logged off.

The bots are in the main pywikipedia folder when downloaded. But it necessary, use the command cd to go to the directory where the bot files are saved.

Now run any of the bots here by typing "python botname.py" (If you are using Windows, you can leave out "python").

程式碼

Here is a list of the existing bots with links to their descriptions:

  主要的機器人程式   其他的機器人程式  



輔助程式

Command-line arguments

Although many bot scripts have their own command line arguments, which should be documented on their respective pages (or in their source code), all bots unless specifically stated to the contrary recognize the following command line arguments:

-help
Print a list of global bot arguments (this list), followed by bot-specific help if available.
-lang:xx
Set the language of the wiki you want to work on to language code xx, overriding the configuration in user-config.py.
-family:xyz
Set the family of the wiki you want to work on, e.g., wikipedia, wiktionary, wikitravel, ... This will override the configuration in user-config.py.
-log
Enable the logfile. Logs will be stored in the logs subdirectory.
-log:xyz
Enable the logfile, using xyz as the filename.
-nolog
Disable the logfile (if it's enabled by default).
-putthrottle:nn
Set the minimum time (in seconds) the bot will wait between saving pages. The default value is 10.

For example, python scriptname.py -family:wiktionary will run the "scriptname" bot on wiktionary articles, overriding the default family setting in your user configuration.

Bot mailing list

It can be useful to subscribe to the bot mailing list (see http://lists.wikimedia.org/mailman/listinfo/Pywikipedia-l). Every time a file of the bot software is changed, a mail is sent to the list, so you know when you need to update to the new version.

Update

For further updates chdir into 'pywikipedia' and type

$ svn update

On a Mac, you can write the following:

svn update pywikipedia

How to report a bug

When you report a bug please try to include:

  • PyWikipediaBot version in use. It's recommended to test if the bug is still present in latest SVN revision available.
  • Python version (python -V) and operating system you use (e.g. Windows, Linux, MacOS...)
  • A nice summary
  • Full description of the problem/report
  • How to reproduce bug full information (script, command line, family, and language used)
  • Output data

To submit a new bug visit the bug tracker provided by SourceForge.

Development

If you have a function you want to have a bot for that is not yet provided by one of the bots, you can ask one of the programmers to write it for you. Or even better, you can try to work on the bots yourself. Python is a nice language, and not hard to learn. We will welcome you.

Tips

Here and in wikipedia.py, there are some very basic tips for getting started writing your own bot:

  • be sure you've set up your user-config.py file (see above)
  • To gain access to the pywikipedia framework, use:
import wikipedia
  • to retrieve a page, use the following, where pageName is, e.g., "Wikipedia:Bots" or "India":
site = wikipedia.getSite()
page = wikipedia.Page(site, u"pageName")
text = page.get(get_redirect = True)
  • to update a page, use:
page.put(u"newText", u"Edit comment")
  • look at some of the pywikipedia files for other ideas -- basic.py is relatively easy to read even if you're new to pywikipedia.
  • you can find all available Page methods in the wikipedia.py file.
  • basic.py gives you a setup that can be used for many different bots, all you have to do is define the string editing on the page text.
  • To iterate over a set of pages, see pagegenerators.py for some objects that return a set of pages. An example use of the CategoryPageGenerator that does something for each page in the Category:Living people category:
import catlib
import pagegenerators
site = wikipedia.getSite()
cat = catlib.Category(site,'Category:Living people')
gen = pagegenerators.CategorizedPageGenerator(cat)
for page in gen:
  #Do something with the page object, for example:
  text = page.get()

Create a quick shortcut to run commands (Windows users)

How to make a quick shortcut to run commands (Windows users).

If you're installing Pywikipediabot in a folder such as "My Documents" it may be troublesome to use the "cd" command to go into the folder all the time to run the bots (For those who don't get what that means, this will help you a lot).

On Windows you can create a shortcut which will open the command box you can use to run bots easily. Just follow these simple steps to create one:

  1. Open up the folder pywikipedia is installed in, in a window.
  2. Under File > New select Shortcut.
  3. Type in "cmd.exe" and hit next.
  4. You can give a name to the shortcut here, just "Pywikipediabot" is good.
  5. In the address bar (The text bar above where your files are which tells you where you are) copy the path there.
  6. Right click on the new shortcut and hit properties and paste that path you copied into the "Start in" text field.
  7. Hit ok, and now you have a shortcut to open the command line to run bots from.

Contributing changes

If you changed the bot and want to send a patch to the maintainer,

  1. Update to the current version (it will merge your changes with the improvements already committed to the SVN Repository),
  2. Resolve any conflicts caused by the update (grep for "=====" ;-) and
  3. Type:
$ svn diff > svn.diff

Review the diff to ensure it only includes the changes you want to contribute. The lines at the beginning starting with "?" should be removed.

If you are in direct contact with a Pywikipediabot developer, you can send the file svn.diff to him, but preferably attach the patch to a ticket in the Pywikipedia bug tracking system.

Multiple accounts

It is a common need to run python wikipedia bot under different accounts (main and/or multiple bot accounts). It can be done in two ways.

Separate pywikipedia distributions

One can install completely separate instances of pywikipedia in different directories (1 for each account) and have diferent user-config.py files in each of them. However, when updating the installation via SVN, one needs to run svn update on each folder separately. Also, every installation takes some disk space, which might be a problem on accounts with limited quota.

One pywikipedia distribution with symbolic links

Let's assume user foo has a current SVN working copy of pywikipedia in /home/foo/pywikipedia. For each of the accounts, he creates a separate directory:

foo@bar:~$ mkdir foobot
foo@bar:~$ cd foobot

Pywikipedia needs then some symlinks to the main code tree created in the working directory:

foo@bar:~/foobot$ ln -s ~/pywikipedia/families
foo@bar:~/foobot$ ln -s ~/pywikipedia/userinterfaces

Then, user-config.py for this account must be created as described in Configuration section above.

Finally, the bot must be logged in the usual way:

foo@bar:~/foobot$ python ~/pywikipedia/login.py

The working directory is ready. The scripts will however require a slight modification to run (the path to the pywikipedia tree must be added to Python's path).

import sys, os
sys.path.append(os.environ['HOME'] + '/pywikipedia')
import wikipedia

That's all. Updating to the newest version of pywikipedia on all account at once is now a matter of running svn update only in the ~/pywikipedia directory.

Bot & Proxy

There is probably (not tested!) draft workaround described here.

Notes

  1. "Check out"是將這些檔案從儲存庫上複製一份並取去的意思。
  2. a b 如果您打算在一個以上的語言執行bot,請選擇最常用的那個語言,未來在執行時您可以使用-lang參數來切換執行語言。
  3. 用戶名稱前頭的'u'指的是讓字串轉成Unicode編碼。如果您的用戶名稱並非英數字母,這個'u'就非常重要。

See also

External links