A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

71 рядки
2.7 KiB

  1. #! /usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Copyright (C) 2009, 2010, 2011 by Ben Kurtovic <ben.kurtovic@verizon.net>
  5. #
  6. # Permission is hereby granted, free of charge, to any person obtaining a copy
  7. # of this software and associated documentation files (the "Software"), to deal
  8. # in the Software without restriction, including without limitation the rights
  9. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. # copies of the Software, and to permit persons to whom the Software is
  11. # furnished to do so, subject to the following conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be included in
  14. # all copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. # SOFTWARE.
  23. """
  24. EarwigBot
  25. This is a thin wrapper for EarwigBot's main bot code, specified by bot_script.
  26. The wrapper will automatically restart the bot when it shuts down (from
  27. !restart, for example). It requests the bot's password at startup and reuses it
  28. every time the bot restarts internally, so you do not need to re-enter the
  29. password after using !restart.
  30. For information about the bot as a whole, see the attached README.md file (in
  31. markdown format!), the docs/ directory, and the LICENSE file for licensing
  32. information. EarwigBot is released under the MIT license.
  33. """
  34. from getpass import getpass
  35. from subprocess import Popen, PIPE
  36. from os import path
  37. from sys import executable
  38. from time import sleep
  39. import earwigbot
  40. bot_script = path.join(earwigbot.__path__[0], "runner.py")
  41. def main():
  42. print "EarwigBot v{0}\n".format(earwigbot.__version__)
  43. is_encrypted = earwigbot.config.config.load()
  44. if is_encrypted: # Passwords in the config file are encrypted
  45. key = getpass("Enter key to unencrypt bot passwords: ")
  46. else:
  47. key = None
  48. while 1:
  49. bot = Popen([executable, bot_script], stdin=PIPE)
  50. print >> bot.stdin, path.dirname(path.abspath(__file__))
  51. if is_encrypted:
  52. print >> bot.stdin, key
  53. return_code = bot.wait()
  54. if return_code == 1:
  55. exit() # Let critical exceptions in the subprocess cause us to
  56. # exit as well
  57. else:
  58. sleep(5) # Sleep between bot runs following a non-critical
  59. # subprocess exit
  60. if __name__ == "__main__":
  61. main()