bot.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/usr/bin/env python3.7
  2. import logging
  3. import shlex
  4. import readline
  5. import os
  6. from collections.abc import Iterable
  7. from bookclient import BookClient
  8. histfile = os.path.join(os.path.expanduser("~"), ".book_history")
  9. def usage():
  10. print("USAGE:")
  11. print("\t SEARCH <BOOK>")
  12. print("\t BOOK <BOT COMMAND>")
  13. def cb(arg):
  14. print("Called callback!")
  15. if isinstance(arg, Iterable):
  16. for item in arg:
  17. print(item)
  18. else:
  19. print(arg)
  20. def main():
  21. b = BookClient(cb)
  22. while True:
  23. line = input('> ').strip().lower()
  24. try:
  25. split = shlex.split(line)
  26. except Exception as e:
  27. print(e)
  28. continue
  29. if len(split) == 0:
  30. continue
  31. mode = split[0]
  32. query = " ".join(split[1:])
  33. b.request(mode, query)
  34. if __name__ == "__main__":
  35. try:
  36. readline.read_history_file(histfile)
  37. # default history len is -1 (infinite), which may grow unruly
  38. readline.set_history_length(10000)
  39. except FileNotFoundError:
  40. pass
  41. try:
  42. main()
  43. except KeyboardInterrupt:
  44. print("\nBye")
  45. except EOFError:
  46. print("\nBye")
  47. readline.write_history_file(histfile)