A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

base_task.py 3.2 KiB

il y a 13 ans
il y a 13 ans
il y a 13 ans
il y a 13 ans
il y a 13 ans
il y a 13 ans
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # -*- coding: utf-8 -*-
  2. import logging
  3. import config
  4. import wiki
  5. class BaseTask(object):
  6. """A base class for bot tasks that edit Wikipedia."""
  7. name = None
  8. number = 0
  9. def __init__(self):
  10. """Constructor for new tasks.
  11. This is called once immediately after the task class is loaded by
  12. the task manager (in tasks._load_task()).
  13. """
  14. pass
  15. def _setup_logger(self):
  16. """Set up a basic module-level logger."""
  17. self.logger = logging.getLogger(".".join(("tasks", self.name)))
  18. self.logger.setLevel(logging.DEBUG)
  19. def run(self, **kwargs):
  20. """Main entry point to run a given task.
  21. This is called directly by tasks.start() and is the main way to make a
  22. task do stuff. kwargs will be any keyword arguments passed to start()
  23. which are entirely optional.
  24. The same task instance is preserved between runs, so you can
  25. theoretically store data in self (e.g.
  26. start('mytask', action='store', data='foo')) and then use it later
  27. (e.g. start('mytask', action='save')).
  28. """
  29. pass
  30. def make_summary(self, comment):
  31. """Makes an edit summary by filling in variables in a config value.
  32. config.wiki["summary"] is used, where $2 is replaced by the main
  33. summary body, given as a method arg, and $1 is replaced by the task
  34. number.
  35. If the config value is not found, we just return the arg as-is.
  36. """
  37. try:
  38. summary = config.wiki["summary"]
  39. except KeyError:
  40. return comment
  41. return summary.replace("$1", str(self.number)).replace("$2", comment)
  42. def shutoff_enabled(self, site=None):
  43. """Returns whether on-wiki shutoff is enabled for this task.
  44. We check a certain page for certain content. This is determined by
  45. our config file: config.wiki["shutoff"]["page"] is used as the title,
  46. with $1 replaced by our username and $2 replaced by the task number,
  47. and config.wiki["shutoff"]["disabled"] is used as the content.
  48. If the page has that content or the page does not exist, then shutoff
  49. is "disabled", meaning the bot is supposed to run normally, and we
  50. return False. If the page's content is something other than what we
  51. expect, shutoff is enabled, and we return True.
  52. If a site is not provided, we'll try to use self.site if it's set.
  53. Otherwise, we'll use our default site.
  54. """
  55. if not site:
  56. try:
  57. site = self.site
  58. except AttributeError:
  59. site = wiki.get_site()
  60. try:
  61. cfg = config.wiki["shutoff"]
  62. except KeyError:
  63. return False
  64. title = cfg.get("page", "User:$1/Shutoff/Task $2")
  65. username = site.get_user().name()
  66. title = title.replace("$1", username).replace("$2", str(self.number))
  67. page = site.get_page(title)
  68. try:
  69. content = page.get()
  70. except wiki.PageNotFoundError:
  71. return False
  72. if content == cfg.get("disabled", "run"):
  73. return False
  74. self.logger.warn("Emergency task shutoff has been enabled!")
  75. return True