qmanager.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/usr/bin/env python3
  2. from bot import IRCClient
  3. import queue
  4. import shlex
  5. import asyncore
  6. import datetime
  7. from time import strftime,time
  8. class qManager:
  9. tasks = []
  10. last_id=0
  11. def new_dl(self,string):
  12. self.new_task(string,IRCClient(string,"","BOOK",logging=False))
  13. def new_search(self,keywords,format):
  14. self.new_task(keywords,IRCClient(keywords,format,"SEARCH",logging=False))
  15. def new_task(self,query,t):
  16. nt = task(query,t,self.last_id)
  17. self.last_id+=1
  18. self.tasks.append(nt)
  19. def task_status(self):
  20. ret=[]
  21. for t in self.tasks:
  22. elapsed= time()-t.START_TIME
  23. ret.append({"ID": t.ID,
  24. "STATUS": t.get_status(),
  25. "OUT": t.get_output(),
  26. "ELAPSED": int(elapsed),
  27. "QUERY": t.QUERY,
  28. "TYPE": t.get_type()})
  29. return ret
  30. class task:
  31. CUR_STATUS=""
  32. OUTPUT=""
  33. ID=0
  34. CLIENT=None
  35. START_TIME=None
  36. QUERY=""
  37. def __init__(self,query,c,id):
  38. self.QUERY=query
  39. self.CLIENT=c
  40. self.ID=id
  41. self.START_TIME=time()
  42. pass
  43. def get_status(self):
  44. self.OUTPUT=self.CLIENT.OUTPUT
  45. return self.CLIENT.STATUS
  46. def get_type(self):
  47. return self.CLIENT.TYPE
  48. def get_output(self):
  49. return self.OUTPUT
  50. #CLI INTERFACE
  51. if __name__ == "__main__":
  52. q = qManager()
  53. while True:
  54. line=input()
  55. words=shlex.split(line)
  56. print(q.task_status())
  57. if len(words)<2:
  58. continue
  59. comm=words[0]
  60. if comm.lower()=="quit":
  61. break
  62. if comm.lower()=="search":
  63. if len(words)<3:
  64. continue
  65. q.new_search(words[1],words[2])
  66. continue
  67. if comm.lower()=="dl":
  68. q.new_dl(words[1])
  69. continue
  70. print("search <'multiple keywords'> <format>|dl <line>|quit")