A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

watcher_logic.py 2.9 KiB

před 13 roky
před 13 roky
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # -*- coding: utf-8 -*-
  2. """
  3. EarwigBot's IRC Watcher Logic
  4. This file contains (configurable!) rules that EarwigBot's watcher uses after it
  5. recieves an event from IRC.
  6. This should, ideally, be in config.json somehow, but Python code makes more
  7. sense for this sort of thing... so...
  8. """
  9. import re
  10. from wiki import task_manager as tasks
  11. afc_prefix = "wikipedia( talk)?:(wikiproject )?articles for creation"
  12. # compile some regexps used when finding specific events
  13. r_page = re.compile(afc_prefix)
  14. r_ffu = re.compile("wikipedia( talk)?:files for upload")
  15. r_move1 = re.compile("moved \[\[{}".format(afc_prefix))
  16. r_move2 = re.compile("moved \[\[(.*?)\]\] to \[\[{}".format(afc_prefix))
  17. r_moved_pages = re.compile("^moved \[\[(.*?)\]\] to \[\[(.*?)\]\]")
  18. r_delete = re.compile("deleted \"\[\[{}".format(afc_prefix))
  19. r_deleted_page = re.compile("^deleted \"\[\[(.*?)\]\]")
  20. r_restore = re.compile("restored \"\[\[{}".format(afc_prefix))
  21. r_restored_page = re.compile("^restored \"\[\[(.*?)\]\]")
  22. r_protect = re.compile("protected \"\[\[{}".format(afc_prefix))
  23. def process(rc):
  24. """Given an RC() object, return a list of channels to report this event to.
  25. Also, start any wiki bot tasks within this function if necessary."""
  26. chans = set() # channels to report this message to
  27. page_name = rc.page.lower()
  28. comment = rc.comment.lower()
  29. if "!earwigbot" in rc.msg.lower():
  30. chans.update(("##earwigbot", "#wikipedia-en-afc"))
  31. if r_page.search(page_name):
  32. tasks.start_task("afc_statistics", action="process_edit", page=rc.page)
  33. tasks.start_task("afc_copyvios", action="process_edit", page=rc.page)
  34. chans.add("#wikipedia-en-afc")
  35. elif r_ffu.match(page_name):
  36. chans.add("#wikipedia-en-afc")
  37. elif page_name.startswith("template:afc submission"):
  38. chans.add("#wikipedia-en-afc")
  39. elif rc.flags == "move" and (r_move1.match(comment) or
  40. r_move2.match(comment)):
  41. p = r_moved_pages.findall(rc.comment)[0]
  42. tasks.start_task("afc_statistics", action="process_move", pages=p)
  43. tasks.start_task("afc_copyvios", action="process_move", pages=p)
  44. chans.add("#wikipedia-en-afc")
  45. elif rc.flags == "delete" and r_delete.match(comment):
  46. p = r_deleted_page.findall(rc.comment)[0]
  47. tasks.start_task("afc_statistics", action="process_delete", page=p)
  48. tasks.start_task("afc_copyvios", action="process_delete", page=p)
  49. chans.add("#wikipedia-en-afc")
  50. elif rc.flags == "restore" and r_restore.match(comment):
  51. p = r_restored_page.findall(rc.comment)[0]
  52. tasks.start_task("afc_statistics", action="process_restore", page=p)
  53. tasks.start_task("afc_copyvios", action="process_restore", page=p)
  54. chans.add("#wikipedia-en-afc")
  55. elif rc.flags == "protect" and r_protect.match(comment):
  56. chans.add("#wikipedia-en-afc")
  57. return chans