A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

12 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. EarwigBot
  2. =========
  3. EarwigBot_ is a Python bot that edits Wikipedia_ and interacts over IRC_.
  4. This README provides a basic overview of how to install and setup the bot;
  5. more detailed information is located in the ``docs/`` directory
  6. (`available online_`).
  7. History
  8. -------
  9. Development began, based on `Pywikibot`_, in early 2009. Approval for its
  10. first task, a `copyright violation detector`_, was carried out in May, and the
  11. bot has been running consistently ever since. It currently handles
  12. `several ongoing tasks`_ ranging from statistics generation to category
  13. cleanup, and on-demand tasks such as WikiProject template tagging. Since it
  14. started running, the bot has made over 300,000 edits.
  15. The current version of its codebase began development in April 2011, moving
  16. away from Pywikibot to a custom framework.
  17. Installation
  18. ------------
  19. This package contains the core ``earwigbot``, abstracted to be usable and
  20. customizable by anyone running a bot on a MediaWiki site. Since it is modular,
  21. the IRC components can be disabled if desired. IRC commands and bot tasks
  22. specific to `my instance of EarwigBot`_ that I don't feel the average user
  23. will need are available from the repository `earwigbot-plugins`_.
  24. Latest release
  25. ~~~~~~~~~~~~~~
  26. EarwigBot is available from the `Python Package Index`_, so you can install
  27. the latest release with:
  28. pip install earwigbot
  29. There are a few sets of optional dependencies:
  30. - ``crypto``: Allows encrypting bot passwords and secrets in the config
  31. - ``sql``: Allows interfacing with MediaWiki databases (e.g. on Toolforge_)
  32. - ``copyvios``: Includes parsing libraries for checking copyright violations
  33. - ``dev``: Installs development dependencies (e.g. test runners)
  34. For example, to install all non-dev dependencies:
  35. pip install 'earwigbot[crypto,sql,copyvios]'
  36. Errors while pip is installing dependencies may be due to missing header
  37. files. For example, on Ubuntu, see `this StackOverflow post`_.
  38. Development version
  39. ~~~~~~~~~~~~~~~~~~~
  40. You can install the development version of the bot::
  41. git clone https://github.com/earwig/earwigbot.git
  42. cd earwigbot
  43. python3 -m venv venv
  44. . venv/bin/activate
  45. pip install -e '.[crypto,sql,copyvios,dev]'
  46. To run the bot's unit tests, run ``pytest`` (requires the ``dev``
  47. dependencies). Coverage is currently rather incomplete.
  48. Setup
  49. -----
  50. The bot stores its data in a "working directory", including its config file
  51. and databases. This is also the location where you will place custom IRC
  52. commands and bot tasks, which will be explained later. It doesn't matter where
  53. this directory is, as long as the bot can write to it.
  54. Start the bot with ``earwigbot path/to/working/dir``, or just ``earwigbot`` if
  55. the working directory is the current directory. It will notice that no
  56. ``config.yml`` file exists and take you through the setup process.
  57. There is currently no way to edit the ``config.yml`` file from within the bot
  58. after it has been created, but you should be able to make any necessary
  59. changes yourself.
  60. After setup, the bot will start. This means it will connect to the IRC servers
  61. it has been configured for, schedule bot tasks to run at specific times, and
  62. then wait for instructions (as commands on IRC). For a list of commands, say
  63. "``!help``" (commands are messages prefixed with an exclamation mark).
  64. You can stop the bot at any time with Control+C, same as you stop a normal
  65. Python program, and it will try to exit safely. You can also use the
  66. "``!quit``" command on IRC.
  67. Customizing
  68. -----------
  69. The bot's working directory contains a ``commands`` subdirectory and a
  70. ``tasks`` subdirectory. Custom IRC commands can be placed in the former,
  71. whereas custom wiki bot tasks go into the latter. Developing custom modules is
  72. explained below, and in more detail through the bot's documentation_ or in the
  73. ``docs/`` dir.
  74. Note that custom commands will override built-in commands and tasks with the
  75. same name.
  76. ``Bot`` and ``BotConfig``
  77. ~~~~~~~~~~~~~~~~~~~~~~~~~
  78. `earwigbot.bot.Bot`_ is EarwigBot's main class. You don't have to instantiate
  79. this yourself, but it's good to be familiar with its attributes and methods,
  80. because it is the main way to communicate with other parts of the bot. A
  81. ``Bot`` object is accessible as an attribute of commands and tasks (i.e.,
  82. ``self.bot``).
  83. `earwigbot.config.BotConfig`_ stores configuration information for the bot.
  84. Its docstring explains what each attribute is used for, but essentially each
  85. "node" (one of ``config.components``, ``wiki``, ``irc``, ``commands``,
  86. ``tasks``, and ``metadata``) maps to a section of the bot's ``config.yml``
  87. file. For example, if ``config.yml`` includes something like::
  88. irc:
  89. frontend:
  90. nick: MyAwesomeBot
  91. channels:
  92. - "##earwigbot"
  93. - "#channel"
  94. - "#other-channel"
  95. then ``config.irc["frontend"]["nick"]`` will be ``"MyAwesomeBot"`` and
  96. ``config.irc["frontend"]["channels"]`` will be ``["##earwigbot", "#channel",
  97. "#other-channel"]``.
  98. Custom IRC commands
  99. ~~~~~~~~~~~~~~~~~~~
  100. Custom commands are subclasses of `earwigbot.commands.Command`_ that override
  101. ``Command``'s ``process()`` (and optionally ``check()``, ``setup()``, or
  102. ``unload()``) methods.
  103. The bot has a wide selection of built-in commands and plugins to act as sample
  104. code and/or to give ideas. Start with test_, and then check out chanops_ and
  105. afc_status_ for some more complicated scripts.
  106. Custom bot tasks
  107. ~~~~~~~~~~~~~~~~
  108. Custom tasks are subclasses of `earwigbot.tasks.Task`_ that override
  109. ``Task``'s ``run()`` (and optionally ``setup()`` or ``unload()``) methods.
  110. See the built-in wikiproject_tagger_ task for a relatively straightforward
  111. task, or the afc_statistics_ plugin for a more complicated one.
  112. The Wiki Toolset
  113. ----------------
  114. EarwigBot's answer to the Pywikibot_ is the Wiki Toolset (``earwigbot.wiki``),
  115. which you will mainly access through ``bot.wiki``.
  116. ``bot.wiki`` provides three methods for the management of Sites:
  117. ``get_site()``, ``add_site()``, and ``remove_site()``. Sites are objects that
  118. simply represent a MediaWiki site. A single instance of EarwigBot (i.e. a
  119. single *working directory*) is expected to relate to a single site or group of
  120. sites using the same login info (like all WMF wikis with CentralAuth).
  121. Load your default site (the one that you picked during setup) with
  122. ``site = bot.wiki.get_site()``.
  123. Not all aspects of the toolset are covered in the docs. Explore `its code and
  124. docstrings`_ to learn how to use it in a more hands-on fashion. For reference,
  125. ``bot.wiki`` is an instance of ``earwigbot.wiki.SitesDB`` tied to the
  126. ``sites.db`` file in the bot's working directory.
  127. .. _EarwigBot: https://en.wikipedia.org/wiki/User:EarwigBot
  128. .. _Wikipedia: https://en.wikipedia.org/
  129. .. _IRC: https://en.wikipedia.org/wiki/Internet_Relay_Chat
  130. .. _available online: https://pythonhosted.org/earwigbot/
  131. .. _Pywikibot: https://www.mediawiki.org/wiki/Manual:Pywikibot
  132. .. _copyright violation detector: https://en.wikipedia.org/wiki/Wikipedia:Bots/Requests_for_approval/EarwigBot_1
  133. .. _several ongoing tasks: https://en.wikipedia.org/wiki/User:EarwigBot#Tasks
  134. .. _my instance of EarwigBot: https://en.wikipedia.org/wiki/User:EarwigBot
  135. .. _earwigbot-plugins: https://github.com/earwig/earwigbot-plugins
  136. .. _Python Package Index: https://pypi.python.org/pypi/earwigbot
  137. .. _Toolforge: https://wikitech.wikimedia.org/wiki/Portal:Toolforge
  138. .. _this StackOverflow post: https://stackoverflow.com/questions/6504810/how-to-install-lxml-on-ubuntu/6504860#6504860
  139. .. _documentation: https://pythonhosted.org/earwigbot/
  140. .. _earwigbot.bot.Bot: https://github.com/earwig/earwigbot/blob/main/earwigbot/bot.py
  141. .. _earwigbot.config.BotConfig: https://github.com/earwig/earwigbot/blob/main/earwigbot/config.py
  142. .. _earwigbot.commands.Command: https://github.com/earwig/earwigbot/blob/main/earwigbot/commands/__init__.py
  143. .. _test: https://github.com/earwig/earwigbot/blob/main/earwigbot/commands/test.py
  144. .. _chanops: https://github.com/earwig/earwigbot/blob/main/earwigbot/commands/chanops.py
  145. .. _afc_status: https://github.com/earwig/earwigbot-plugins/blob/main/commands/afc_status.py
  146. .. _earwigbot.tasks.Task: https://github.com/earwig/earwigbot/blob/main/earwigbot/tasks/__init__.py
  147. .. _wikiproject_tagger: https://github.com/earwig/earwigbot/blob/main/earwigbot/tasks/wikiproject_tagger.py
  148. .. _afc_statistics: https://github.com/earwig/earwigbot-plugins/blob/main/tasks/afc_statistics.py
  149. .. _its code and docstrings: https://github.com/earwig/earwigbot/tree/main/earwigbot/wiki