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 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. The Wiki Toolset
  2. ================
  3. EarwigBot's answer to the `Pywikipedia framework`_ is the Wiki Toolset
  4. (:py:mod:`earwigbot.wiki`), which you will mainly access through
  5. :py:attr:`bot.wiki <earwigbot.bot.Bot.wiki>`.
  6. :py:attr:`bot.wiki <earwigbot.bot.Bot.wiki>` provides three methods for the
  7. management of Sites - :py:meth:`~earwigbot.wiki.SiteDB.get_site`,
  8. :py:meth:`~earwigbot.wiki.SiteDB.add_site`, and
  9. :py:meth:`~earwigbot.wiki.SiteDB.remove_site`. Sites are objects that simply
  10. represent a MediaWiki site. A single instance of EarwigBot (i.e. a single
  11. *working directory*) is expected to relate to a single site or group of sites
  12. using the same login info (like all WMF wikis with CentralAuth).
  13. Load your default site (the one that you picked during setup) with
  14. ``site = bot.wiki.get_site()``.
  15. Dealing with other sites
  16. ~~~~~~~~~~~~~~~~~~~~~~~~
  17. *Skip this section if you're only working with one site.*
  18. If a site is *already known to the bot* (meaning that it is stored in the
  19. :file:`sites.db` file, which includes just your default wiki at first), you can
  20. load a site with ``site = bot.wiki.get_site(name)``, where ``name`` might be
  21. ``"enwiki"`` or ``"frwiktionary"`` (you can also do
  22. ``site = bot.wiki.get_site(project="wikipedia", lang="en")``). Recall that not
  23. giving any arguments to ``get_site()`` will return the default site.
  24. :py:meth:`~earwigbot.wiki.SiteDB.add_site` is used to add new sites to the sites
  25. database. It may be called with similar arguments as
  26. :py:meth:`~earwigbot.wiki.SiteDB.get_site`, but the difference is important.
  27. :py:meth:`~earwigbot.wiki.SiteDB.get_site` only needs enough information to
  28. identify the site in its database, which is usually just its name; the database
  29. stores all other necessary connection info. With
  30. :py:meth:`~earwigbot.wiki.SiteDB.add_site`, you need to provide enough connection
  31. info so the toolset can successfully access the site's API/SQL databases and
  32. store that information for later. That might not be much; for WMF wikis, you
  33. can usually use code like this::
  34. project, lang = "wikipedia", "es"
  35. try:
  36. site = bot.wiki.get_site(project=project, lang=lang)
  37. except earwigbot.SiteNotFoundError:
  38. # Load site info from http://es.wikipedia.org/w/api.php:
  39. site = bot.wiki.add_site(project=project, lang=lang)
  40. This works because EarwigBot assumes that the URL for the site is
  41. ``"//{lang}.{project}.org"`` and the API is at ``/w/api.php``; this might
  42. change if you're dealing with non-WMF wikis, where the code might look
  43. something more like::
  44. project, lang = "mywiki", "it"
  45. try:
  46. site = bot.wiki.get_site(project=project, lang=lang)
  47. except earwigbot.SiteNotFoundError:
  48. # Load site info from http://mysite.net/mywiki/it/s/api.php:
  49. base_url = "http://mysite.net/" + project + "/" + lang
  50. db_name = lang + project + "_p"
  51. sql = {host: "sql.mysite.net", db: db_name}
  52. site = bot.wiki.add_site(base_url=base_url, script_path="/s", sql=sql)
  53. :py:meth:`~earwigbot.wiki.SiteDB.remove_site` does the opposite of
  54. :py:meth:`~earwigbot.wiki.SiteDB.add_site`: give it a site's name or a
  55. project/lang pair like :py:meth:`~earwigbot.wiki.SiteDB.get_site` takes, and
  56. it'll remove that site from the sites database.
  57. Sites
  58. ~~~~~
  59. :py:class:`earwigbot.wiki.Site` objects provide the following attributes:
  60. - :py:attr:`~earwigbot.wiki.Site.name`: the site's name (or "wikiid"), like
  61. ``"enwiki"``
  62. - :py:attr:`~earwigbot.wiki.Site.project`: the site's project name, like
  63. ``"wikipedia"``
  64. - :py:attr:`~earwigbot.wiki.Site.lang`: the site's language code, like ``"en"``
  65. - :py:attr:`~earwigbot.wiki.Site.domain`: the site's web domain, like
  66. ``"en.wikipedia.org"``
  67. and the following methods:
  68. - :py:meth:`api_query(**kwargs) <earwigbot.wiki.Site.api_query>`: does an API
  69. query with the given keyword arguments as params
  70. - :py:meth:`sql_query(query, params=(), ...) <earwigbot.wiki.Site.sql_query>`:
  71. does an SQL query and yields its results (as a generator)
  72. - :py:meth:`~earwigbot.wiki.Site.get_replag`: returns the estimated database
  73. replication lag (if we have the site's SQL connection info)
  74. - :py:meth:`namespace_id_to_name(id, all=False)
  75. <earwigbot.wiki.Site.namespace_id_to_name>`: given a namespace ID, returns
  76. the primary associated namespace name (or a list of all names when ``all`` is
  77. ``True``)
  78. - :py:meth:`namespace_name_to_id(name)
  79. <earwigbot.wiki.Site.namespace_name_to_id>`: given a namespace name, returns
  80. the associated namespace ID
  81. - :py:meth:`get_page(title, follow_redirects=False)
  82. <earwigbot.wiki.Site.get_page>`: returns a ``Page`` object for the given
  83. title (or a :py:class:`~earwigbot.wiki.Category` object if the page's
  84. namespace is "``Category:``")
  85. - :py:meth:`get_category(catname, follow_redirects=False)
  86. <earwigbot.wiki.Site.get_category>`: returns a ``Category`` object for the
  87. given title (sans namespace)
  88. - :py:meth:`get_user(username) <earwigbot.wiki.Site.get_user>`: returns a
  89. :py:class:`~earwigbot.wiki.User` object for the given username
  90. Pages (and Categories)
  91. ~~~~~~~~~~~~~~~~~~~~~~
  92. Create :py:class:`earwigbot.wiki.Page` objects with
  93. :py:meth:`site.get_page(title) <earwigbot.wiki.Site.get_page>`,
  94. :py:meth:`page.toggle_talk() <earwigbot.wiki.Page.toggle_talk>`,
  95. :py:meth:`user.get_userpage() <earwigbot.wiki.User.get_userpage>`, or
  96. :py:meth:`user.get_talkpage() <earwigbot.wiki.User.get_talkpage>`. They provide
  97. the following attributes:
  98. - :py:attr:`~earwigbot.wiki.Page.title`: the page's title, or pagename
  99. - :py:attr:`~earwigbot.wiki.Page.exists`: whether the page exists
  100. - :py:attr:`~earwigbot.wiki.Page.pageid`: an integer ID representing the page
  101. - :py:attr:`~earwigbot.wiki.Page.url`: the page's URL
  102. - :py:attr:`~earwigbot.wiki.Page.namespace`: the page's namespace as an integer
  103. - :py:attr:`~earwigbot.wiki.Page.protection`: the page's current protection
  104. status
  105. - :py:attr:`~earwigbot.wiki.Page.is_talkpage`: ``True`` if the page is a
  106. talkpage, else ``False``
  107. - :py:attr:`~earwigbot.wiki.Page.is_redirect`: ``True`` if the page is a
  108. redirect, else ``False``
  109. and the following methods:
  110. - :py:meth:`~earwigbot.wiki.Page.reload`: forcibly reload the page's attributes
  111. (emphasis on *reload* - this is only necessary if there is reason to believe
  112. they have changed)
  113. - :py:meth:`toggle_talk(...) <earwigbot.wiki.Page.toggle_talk>`: returns a
  114. content page's talk page, or vice versa
  115. - :py:meth:`~earwigbot.wiki.Page.get`: returns page content
  116. - :py:meth:`~earwigbot.wiki.Page.get_redirect_target`: if the page is a
  117. redirect, returns its destination
  118. - :py:meth:`~earwigbot.wiki.Page.get_creator`: returns a
  119. :py:class:`~earwigbot.wiki.User` object representing the first user to edit
  120. the page
  121. - :py:meth:`edit(text, summary, minor=False, bot=True, force=False)
  122. <earwigbot.wiki.Page.edit>`: replaces the page's content with ``text`` or
  123. creates a new page
  124. - :py:meth:`add_section(text, title, minor=False, bot=True, force=False)
  125. <earwigbot.wiki.Page.add_section>`: adds a new section named ``title`` at the
  126. bottom of the page
  127. - :py:meth:`copyvio_check(...)
  128. <earwigbot.wiki.copyvios.CopyvioMixin.copyvio_check>`: checks the page for
  129. copyright violations
  130. - :py:meth:`copyvio_compare(url, ...)
  131. <earwigbot.wiki.copyvios.CopyvioMixin.copyvio_compare>`: checks the page like
  132. :py:meth:`~earwigbot.wiki.copyvios.CopyvioMixin.copyvio_check`, but
  133. against a specific URL
  134. Additionally, :py:class:`~earwigbot.wiki.Category` objects (created with
  135. :py:meth:`site.get_category(name) <earwigbot.wiki.Site.get_category>` or
  136. :py:meth:`site.get_page(title) <earwigbot.wiki.Site.get_page>` where ``title``
  137. is in the ``Category:`` namespace) provide the following additional method:
  138. - :py:meth:`get_members(use_sql=False, limit=None)
  139. <earwigbot.wiki.Category.get_members>`: returns a list of page titles in the
  140. category (limit is ``50`` by default if using the API)
  141. Users
  142. ~~~~~
  143. Create :py:class:`earwigbot.wiki.User` objects with
  144. :py:meth:`site.get_user(name) <earwigbot.wiki.Site.get_user>` or
  145. :py:meth:`page.get_creator() <earwigbot.wiki.Page.get_creator>`. They provide
  146. the following attributes:
  147. - :py:attr:`~earwigbot.wiki.User.name`: the user's username
  148. - :py:attr:`~earwigbot.wiki.User.exists`: ``True`` if the user exists, or
  149. ``False`` if they do not
  150. - :py:attr:`~earwigbot.wiki.User.userid`: an integer ID representing the user
  151. - :py:attr:`~earwigbot.wiki.User.blockinfo`: information about any current
  152. blocks on the user (``False`` if no block, or a dict of
  153. ``{"by": blocking_user, "reason": block_reason,
  154. "expiry": block_expire_time}``)
  155. - :py:attr:`~earwigbot.wiki.User.groups`: a list of the user's groups
  156. - :py:attr:`~earwigbot.wiki.User.rights`: a list of the user's rights
  157. - :py:attr:`~earwigbot.wiki.User.editcount`: the number of edits made by the
  158. user
  159. - :py:attr:`~earwigbot.wiki.User.registration`: the time the user registered as
  160. a :py:obj:`time.struct_time`
  161. - :py:attr:`~earwigbot.wiki.User.emailable`: ``True`` if you can email the
  162. user, ``False`` if you cannot
  163. - :py:attr:`~earwigbot.wiki.User.gender`: the user's gender (``"male"``,
  164. ``"female"``, or ``"unknown"``)
  165. and the following methods:
  166. - :py:meth:`~earwigbot.wiki.User.reload`: forcibly reload the user's attributes
  167. (emphasis on *reload* - this is only necessary if there is reason to believe
  168. they have changed)
  169. - :py:meth:`~earwigbot.wiki.User.get_userpage`: returns a
  170. :py:class:`~earwigbot.wiki.Page` object representing the user's userpage
  171. - :py:meth:`~earwigbot.wiki.User.get_talkpage`: returns a
  172. :py:class:`~earwigbot.wiki.Page` object representing the user's talkpage
  173. Additional features
  174. ~~~~~~~~~~~~~~~~~~~
  175. Not all aspects of the toolset are covered here. Explore `its code and
  176. docstrings`_ to learn how to use it in a more hands-on fashion. For reference,
  177. :py:attr:`bot.wiki <earwigbot.bot.Bot.wiki>` is an instance of
  178. :py:class:`earwigbot.wiki.SitesDB` tied to the :file:`sites.db` file in the
  179. bot's working directory.
  180. .. _Pywikipedia framework: http://pywikipediabot.sourceforge.net/
  181. .. _its code and docstrings: https://github.com/earwig/earwigbot/tree/develop/earwigbot/wiki