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.

71 regels
2.2 KiB

  1. # -*- coding: utf-8 -*-
  2. ## Imports
  3. import re
  4. from config.irc_config import *
  5. from irc.connection import Connection
  6. from irc.rc import RC
  7. global frontend_conn
  8. def get_connection():
  9. connection = Connection(WATCHER_HOST, WATCHER_PORT, NICK, IDENT, REALNAME)
  10. return connection
  11. def main(connection, f_conn):
  12. global frontend_conn
  13. frontend_conn = f_conn
  14. connection.connect()
  15. read_buffer = str()
  16. while 1:
  17. try:
  18. read_buffer = read_buffer + connection.get()
  19. except RuntimeError: # socket broke
  20. return
  21. lines = read_buffer.split("\n")
  22. read_buffer = lines.pop()
  23. for line in lines:
  24. line = line.strip().split()
  25. if line[1] == "PRIVMSG":
  26. chan = line[2]
  27. if chan != WATCHER_CHAN: # if we're getting a msg from another channel, ignore it
  28. continue
  29. msg = ' '.join(line[3:])[1:]
  30. rc = RC(msg) # create a new RC object to store this change's data
  31. rc.parse()
  32. check(rc)
  33. if line[0] == "PING": # If we are pinged, pong back to the server
  34. connection.send("PONG %s" % line[1])
  35. if line[1] == "376": # Join the recent changes channel when we've finished starting up
  36. connection.join(WATCHER_CHAN)
  37. def report(msg, chans):
  38. """send a message to a list of report channels on our front-end server"""
  39. for chan in chans:
  40. frontend_conn.say(chan, msg)
  41. def check(rc):
  42. """check if we're supposed to report this message anywhere"""
  43. page_name = rc.page.lower()
  44. pretty_msg = rc.pretty()
  45. if "!earwigbot" in rc.msg.lower():
  46. report(pretty_msg, chans=BOT_CHANS)
  47. if re.match("wikipedia( talk)?:(wikiproject )?articles for creation", page_name):
  48. report(pretty_msg, chans=AFC_CHANS)
  49. elif re.match("wikipedia( talk)?:files for upload", page_name):
  50. report(pretty_msg, chans=AFC_CHANS)
  51. elif page_name.startswith("template:afc submission"):
  52. report(pretty_msg, chans=AFC_CHANS)
  53. if rc.flags == "delete" and re.match("deleted \"\[\[wikipedia( talk)?:(wikiproject )?articles for creation", rc.comment.lower()):
  54. report(pretty_msg, chans=AFC_CHANS)