qmanager.py 2.1 KB

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