A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

85 行
3.0 KiB

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