qmanager.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. "PROGRESS": t.get_progress(),
  29. "OUT": t.get_output(),
  30. "ELAPSED": int(elapsed),
  31. "QUERY": t.QUERY,
  32. "EXTRA": t.get_extra(),
  33. "TYPE": t.get_type()})
  34. return ret
  35. class task:
  36. CUR_STATUS=""
  37. OUTPUT=""
  38. ID=0
  39. CLIENT=None
  40. START_TIME=None
  41. QUERY=""
  42. def __init__(self,query,c,id):
  43. self.QUERY=query
  44. self.CLIENT=c
  45. self.ID=id
  46. self.START_TIME=time()
  47. pass
  48. def get_progress(self):
  49. return self.CLIENT.PROGRESS;
  50. def get_extra(self):
  51. return self.CLIENT.EXTRA_OUTPUT;
  52. def get_status(self):
  53. if time() - self.START_TIME > 600:
  54. self.CLIENT.do_timeout()
  55. self.OUTPUT=self.CLIENT.OUTPUT
  56. return self.CLIENT.STATUS
  57. def get_type(self):
  58. return self.CLIENT.TYPE
  59. def get_output(self):
  60. return self.OUTPUT
  61. #BASIC CLI INTERFACE
  62. if __name__ == "__main__":
  63. q = qManager()
  64. while True:
  65. line=input()
  66. words=shlex.split(line)
  67. print(q.task_status())
  68. if len(words)<2:
  69. continue
  70. comm=words[0]
  71. if comm.lower()=="quit":
  72. break
  73. if comm.lower()=="search":
  74. if len(words)<3:
  75. continue
  76. q.new_search(words[1],words[2])
  77. continue
  78. if comm.lower()=="dl":
  79. q.new_dl(words[1])
  80. continue
  81. print("search <'multiple keywords'> <format>|dl <line>|quit")