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.

86 lines
3.0 KiB

  1. # Copyright (C) 2009-2015 Ben Kurtovic <ben.kurtovic@gmail.com>
  2. #
  3. # Permission is hereby granted, free of charge, to any person obtaining a copy
  4. # of this software and associated documentation files (the "Software"), to deal
  5. # in the Software without restriction, including without limitation the rights
  6. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. # copies of the Software, and to permit persons to whom the Software is
  8. # furnished to do so, subject to the following conditions:
  9. #
  10. # The above copyright notice and this permission notice shall be included in
  11. # all copies or substantial portions of the Software.
  12. #
  13. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. # SOFTWARE.
  20. from datetime import datetime
  21. from time import mktime
  22. from earwigbot import exceptions
  23. from earwigbot.commands import Command
  24. class Registration(Command):
  25. """Return when a user registered."""
  26. name = "registration"
  27. commands = ["registration", "reg", "age"]
  28. def process(self, data):
  29. if not data.args:
  30. name = data.nick
  31. else:
  32. name = " ".join(data.args)
  33. site = self.bot.wiki.get_site()
  34. user = site.get_user(name)
  35. try:
  36. reg = user.registration
  37. except exceptions.UserNotFoundError:
  38. msg = "The user \x0302{0}\x0f does not exist."
  39. self.reply(data, msg.format(name))
  40. return
  41. dt = datetime.fromtimestamp(mktime(reg))
  42. date = dt.strftime("%b %d, %Y at %H:%M:%S UTC")
  43. age = self.get_age(dt)
  44. if user.gender == "male":
  45. gender = "He's"
  46. elif user.gender == "female":
  47. gender = "She's"
  48. else:
  49. gender = "They're" # Singular they?
  50. msg = "\x0302{0}\x0f registered on {1}. {2} {3} old."
  51. self.reply(data, msg.format(name, date, gender, age))
  52. def get_age(self, birth):
  53. msg = []
  54. def insert(unit, num):
  55. if not num:
  56. return
  57. msg.append("{} {}".format(num, unit if num == 1 else unit + "s"))
  58. now = datetime.utcnow()
  59. bd_passed = now.timetuple()[1:-3] < birth.timetuple()[1:-3]
  60. years = now.year - birth.year - bd_passed
  61. delta = now - birth.replace(year=birth.year + years)
  62. insert("year", years)
  63. insert("day", delta.days)
  64. seconds = delta.seconds
  65. units = [("hour", 3600), ("minute", 60), ("second", 1)]
  66. for unit, size in units:
  67. num = seconds / size
  68. seconds -= num * size
  69. insert(unit, num)
  70. return ", ".join(msg) if msg else "0 seconds"