手册:Pywikibot/user-config.py

From mediawiki.org
This page is a translated version of the page Manual:Pywikibot/user-config.py and the translation is 78% complete.

設定

在開始執行Pywikibot之前,請先在pywikibot資料夾內建立user-config.py檔案。 這裡提供兩種建立的方式:

  1. 自行手動建立。 手動建立後,在檔案裡面至少需要設定2~3個變數。
  2. 透過下python pwb.py generate_user_files.py指令,來執行generate_user_files.py的程式,該程式會出現問題讓您輸入,您輸入的內容將會儲存在設定檔裡。

位置

Pywikibot將會依照下列的順序來尋找您的user-config:

  • 設定-dir:參數 (例如-dir:/foo/bar)
  • The content of the environment variable PYWIKIBOT_DIR
  • The current directory
  • The directory in which the pwb.py file is
  • The .pywikibot directory in the application data directory (Windows) or home directory (that directory gets automatically created)

(參見get_base_dir())

下列文章將描述如何修改設定檔。

開啟文字編輯器(例如「記事本」),將檔案名稱存成user-config.py,再將其放到pywikibot資料夾下。

若您的語言使用non-ASCII字元的話,您應該使用Unicode-aware的編輯器(例如Nodepad++)來開啟檔案,且儲存時請將文字的編碼設定成Unicode encoding without BOM marks。最後,別忘了在檔案的最前面加上:

# -*- coding: utf-8 -*-

as the first line.

將下列所列出來的程式碼加到user-config.py上:

代码 備註
family = 'wikipedia'
mylang = 'en'

"xx"表示您使用的語言代碼,例如'en'表示英文。[1] Family則是表示專案的名字。[2]

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

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

在這個範例當中,使用者使用英文,而他創了一個名為「ExampleBot」的機器人帳號。 [3]

(選填)

authenticate['*.wikipedia.org'] = ('<consumer_key>','<consumer_secret>', '<access_key>', '<access_secret>')

這裡使用更安全的 OAuth 取代基於密碼的身份驗證 (尤其當一個機器人可以被多位使用者操縱時,顯得更加安全) 請看Pywikibot/OAuth,裡面描述 OAuth 如何驗證的。

(選填)

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

若您使用多個Wikimedia的專案的話,您也可以增加多個使用者名稱。

(選填,很少用到)

console_encoding = 'utf-8'
textfile_encoding = 'unicode_escape'

If this is the encoding used by your system. Always try without first.

(選填)

sort_ignore_case = True

有些程式碼可能會需要用到排序,例如 solve_disambiguation.py 默认值为“False”。 如果設定成False或是沒設定的話,大寫的標題將被轉換為小寫;若設定成True的話,大寫則會被忽略。

(選填)

password_file = "user-password.py"

設定存放密碼的檔案 在該檔案裡,請使用下列的格式:

(code, family, username, password)
(family, username, password)
(username, password)

關於BotPasswords, 請參考Pywikibot/BotPasswords.

(Optional)
user_agent_description = "Description <contact@email.com>"
A free-form string that can be user to describe specific bot/tool, provide contact information, etc.

儲存user-config.py

user-config.py 範例

no.wikipedia 上的 EksempelBot 機器人

mylang = 'no'
usernames['wikipedia']['no'] = 'EksempelBot'

console_encoding = 'utf-8'
use_api_login = True

Commons 上的 ExampleBot 機器人

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

console_encoding = 'utf-8'
use_api_login = True

Wikidata 上的 ExampleBot 機器人

mylang = 'wikidata'
family = 'wikidata'
usernames['wikidata']['wikidata'] = 'ExampleBot'

console_encoding = 'utf-8'
use_api_login = True

Test wiki 上的 ExampleBot 機器人

mylang = 'test'
family = 'test'
usernames['test']['test'] = 'ExampleBot'

Test2 wiki 上的 ExampleBot 機器人

mylang = 'test2'
family = 'wikipedia'
usernames['wikipedia']['test2'] = 'ExampleBot'

de.wikipedia 和 de.wikibooks 上的 BeispielBot 機器人,且 de.wikipedia 是主要的 wiki

mylang = 'de'
usernames['wikipedia']['de'] = 'BeispielBot'
usernames['wikibooks']['de'] = 'BeispielBot'

console_encoding = 'utf-8'
use_api_login = True

非維基媒體基金會的維基網站

請看主條目 Pywikibot/Use on third-party wikis

參見

  • config2.py (載入和直譯 user-config.py 的模組的原始碼)


備註

  1. 如果您使用多個語言的話,請輸入最常使用的那一個。 您也可以在執行時透過加上參數-lang:zh來複寫已經設定好的語言
  2. Meta uses 'meta' for both language code and wiki family, Commons uses 'commons' for both, and Testwiki uses 'test' for both, the multilingual wikisource uses '-' for the language. You can override this on the command line by using -family:wikibooks.
  3. Note that on Linux/Unix hosts username capitalization matters! While logging in may not be an issue, testing the log in or attempting to use a bot will not use the correct cookie file and may result in anonymous access to the API. This can cause problems for private wikis that do not allow anonymous access or use third party authentication. Default usernames for mediawiki and those pulled via LDAP or other third party authentication schemes will have an uppercase character for the first letter, thus 'user' becomes 'User'.


If you need more help on setting up your Pywikibot visit the #pywikibot IRC channel connect or pywikibot@ mailing list.