A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

afc_statistics.py 2.5 KiB

13 yıl önce
13 yıl önce
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # -*- coding: utf-8 -*-
  2. import re
  3. from os import path
  4. from classes import BaseTask
  5. import config
  6. import wiki
  7. class Task(BaseTask):
  8. """A task to generate statistics for WikiProject Articles for Creation.
  9. Statistics are stored in the file indicated by self.filename,
  10. "statistics.txt" in the bot's root directory being the default. They are
  11. updated live while watching the recent changes IRC feed.
  12. The bot saves its statistics once an hour, on the hour, to self.pagename.
  13. In the live bot, this is "Template:AFC statistics".
  14. """
  15. name = "afc_statistics"
  16. number = 2
  17. def __init__(self):
  18. self.filename = path.join(config.root_dir, "statistics.txt")
  19. self.cfg = config.tasks.get(self.name, {})
  20. self.pagename = cfg.get("page", "Template:AFC statistics")
  21. default = "Updating statistics for [[WP:WPAFC|WikiProject Articles for creation]]."
  22. self.summary = self.make_summary(cfg.get("summary", default))
  23. def run(self, **kwargs):
  24. self.site = wiki.get_site()
  25. action = kwargs.get("action")
  26. if not action:
  27. return
  28. if action == "save":
  29. self.save()
  30. return
  31. page = kwargs.get("page")
  32. if page:
  33. methods = {
  34. "edit": self.process_edit,
  35. "move": self.process_move,
  36. "delete": self.process_delete,
  37. "restore": self.process_restore,
  38. }
  39. method = methods.get(action)
  40. if method:
  41. method(page)
  42. def save(self):
  43. if self.shutoff_enabled():
  44. return
  45. try:
  46. with open(self.filename) as fp:
  47. statistics = fp.read()
  48. except IOError:
  49. pass
  50. page = self.site.get_page(self.pagename)
  51. text = page.get()
  52. newtext = re.sub("(<!-- stat begin -->)(.*?)(<!-- stat end -->)",
  53. statistics.join(("\\1", "\\3")), text,
  54. flags=re.DOTALL)
  55. if newtext == text:
  56. return # Don't edit the page if we're not adding anything
  57. newtext = re.sub("(<!-- sig begin -->)(.*?)(<!-- sig end -->)",
  58. "\\1~~~ at ~~~~~\\3", newtext)
  59. page.edit(newtext, self.summary, minor=True)
  60. def process_edit(self, page):
  61. pass
  62. def process_move(self, page):
  63. pass
  64. def process_delete(self, page):
  65. pass
  66. def process_restore(self, page):
  67. pass