A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot

registration.py 1.7 KiB

13 년 전
13 년 전
13 년 전
13 년 전
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # -*- coding: utf-8 -*-
  2. import time
  3. from classes import BaseCommand
  4. import wiki
  5. class Command(BaseCommand):
  6. """Return when a user registered."""
  7. name = "registration"
  8. def check(self, data):
  9. commands = ["registration", "age"]
  10. if data.is_command and data.command in commands:
  11. return True
  12. return False
  13. def process(self, data):
  14. if not data.args:
  15. name = data.nick
  16. else:
  17. name = ' '.join(data.args)
  18. site = wiki.get_site()
  19. site._maxlag = None
  20. user = site.get_user(name)
  21. try:
  22. reg = user.registration()
  23. except wiki.UserNotFoundError:
  24. msg = "the user \x0302{0}\x0301 does not exist."
  25. self.connection.reply(data, msg.format(name))
  26. return
  27. date = time.strftime("%b %d, %Y at %H:%M:%S UTC", reg)
  28. age = self.get_diff(time.mktime(reg), time.mktime(time.gmtime()))
  29. g = user.gender()
  30. if g == "male":
  31. gender = "He's"
  32. elif g == "female":
  33. gender = "She's"
  34. else:
  35. gender = "They're"
  36. msg = "\x0302{0}\x0301 registered on {1}. {2} {3} old."
  37. self.connection.reply(data, msg.format(name, date, gender, age))
  38. def get_diff(self, t1, t2):
  39. parts = {"years": 31536000, "days": 86400, "hours": 3600,
  40. "minutes": 60, "seconds": 1}
  41. msg = []
  42. order = sorted(parts.items(), key=lambda x: x[1], reverse=True)
  43. for key, value in order:
  44. num = 0
  45. while t2 - t1 > value:
  46. t1 += value
  47. num += 1
  48. if num or (not num and msg):
  49. msg.append(" ".join((str(num), key)))
  50. return ", ".join(msg)