A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

58 righe
1.7 KiB

  1. # -*- coding: utf-8 -*-
  2. ## Imports
  3. from config.irc import *
  4. from config.watcher 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 check(rc):
  38. """check if we're supposed to report this message anywhere"""
  39. results = process(rc) # process the message in config/watcher.py, and get a list of channels to send it to
  40. if not results:
  41. return
  42. pretty = rc.get_pretty()
  43. for chan in results:
  44. frontend_conn.say(chan, pretty)