A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

143 linhas
4.7 KiB

  1. # -*- coding: utf-8 -*-
  2. import re
  3. from os.path import expanduser
  4. import oursql
  5. from classes import BaseTask
  6. import config
  7. import wiki
  8. class Task(BaseTask):
  9. """A task to generate statistics for WikiProject Articles for Creation.
  10. Statistics are stored in a MySQL database ("u_earwig_afc_statistics")
  11. accessed with oursql. Statistics are updated live while watching the recent
  12. changes IRC feed and saved 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.cfg = cfg = config.tasks.get(self.name, {})
  19. # Set some wiki-related attributes:
  20. self.pagename = cfg.get("page", "Template:AFC statistics")
  21. default_summary = "Updating statistics for [[WP:WPAFC|WikiProject Articles for creation]]."
  22. self.summary = self.make_summary(cfg.get("summary", default_summary))
  23. # Templates used in chart generation:
  24. templates = cfg.get("templates", {})
  25. self.tl_header = templates.get("header", "AFC statistics/header")
  26. self.tl_row = templates.get("row", "AFC statistics/row")
  27. self.tl_footer = templates.get("footer", "AFC statistics/footer")
  28. # Establish a connection with our SQL database:
  29. kwargs = cfg.get("sql", {})
  30. kwargs["read_default_file"] = expanduser("~/.my.cnf")
  31. self.conn = oursql.connect(**kwargs)
  32. def run(self, **kwargs):
  33. self.site = wiki.get_site()
  34. action = kwargs.get("action")
  35. if not action:
  36. return
  37. if action == "save":
  38. self.save()
  39. return
  40. page = kwargs.get("page")
  41. if page:
  42. methods = {
  43. "edit": self.process_edit,
  44. "move": self.process_move,
  45. "delete": self.process_delete,
  46. "restore": self.process_restore,
  47. }
  48. method = methods.get(action)
  49. if method:
  50. method(page)
  51. def save(self):
  52. self.check_integrity()
  53. if self.shutoff_enabled():
  54. return
  55. statistics = self.compile_charts()
  56. page = self.site.get_page(self.pagename)
  57. text = page.get()
  58. newtext = re.sub("(<!-- stat begin -->)(.*?)(<!-- stat end -->)",
  59. statistics.join(("\\1", "\\3")), text,
  60. flags=re.DOTALL)
  61. if newtext == text:
  62. return # Don't edit the page if we're not adding anything
  63. newtext = re.sub("(<!-- sig begin -->)(.*?)(<!-- sig end -->)",
  64. "\\1~~~ at ~~~~~\\3", newtext)
  65. page.edit(newtext, self.summary, minor=True, bot=True)
  66. def compile_charts(self):
  67. stats = ""
  68. with self.conn.cursor() as cursor:
  69. cursor.execute("SELECT * FROM chart")
  70. for chart in cursor:
  71. stats += self.compile_chart(chart) + "\n"
  72. return stats[:-1] # Drop the last newline
  73. def compile_chart(self, chart_info):
  74. chart_id, chart_title, special_title = chart_info
  75. chart = "|".join((self.tl_header, chart_title))
  76. if special_title:
  77. chart += "".join(("|", special_title))
  78. chart = "".join(("{{", chart, "}}"))
  79. query = "SELECT * FROM page JOIN row ON page_id = row_id WHERE row_chart = ?"
  80. with self.conn.cursor(oursql.DictCursor) as cursor:
  81. cursor.execute(query, (chart_id,))
  82. for page in cursor:
  83. chart += "\n" + self.compile_chart_row(page)
  84. chart += "".join(("\n{{", self.tl_footer, "}}"))
  85. return chart
  86. def compile_chart_row(self, page):
  87. row = "{0}|s={page_status}|t={page_title}|h={page_short}|z={page_size}|"
  88. row += "cr={page_create_user}|cd={page_create_time}|ci={page_create_oldid}|"
  89. row += "mr={page_modify_user}|md={page_modify_time}|mi={page_modify_oldid}|"
  90. page["page_create_time"] = self.format_time(page["page_create_time"])
  91. page["page_modify_time"] = self.format_time(page["page_modify_time"])
  92. if page["page_special_user"]:
  93. row += "sr={page_special_user}|sd={page_special_time}|si={page_special_oldid}|"
  94. page["page_special_time"] = self.format_time(page["page_special_time"])
  95. if page["page_notes"]:
  96. row += "n=1{page_notes}"
  97. return "".join(("{{", row.format(self.tl_row, **page), "}}"))
  98. def format_time(self, timestamp):
  99. return timestamp.strftime("%H:%M, %d %B %Y")
  100. def check_integrity(self):
  101. pass
  102. def process_edit(self, page):
  103. pass
  104. def process_move(self, page):
  105. pass
  106. def process_delete(self, page):
  107. pass
  108. def process_restore(self, page):
  109. pass