qmanager.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/usr/bin/env python3
  2. from bot import IRCClient
  3. import queue
  4. import shlex
  5. import asyncore
  6. import datetime
  7. import re
  8. from time import strftime,time
  9. class qManager:
  10. tasks = []
  11. last_id=0
  12. PATH=""
  13. def __init__(self,path="/tmp/"):
  14. self.PATH=path
  15. def new_dl(self,string):
  16. self.new_task(string,IRCClient(string,"","BOOK",logging=False,path=self.PATH))
  17. def new_search(self,keywords,format):
  18. self.new_task(keywords,IRCClient(keywords,format,"SEARCH",logging=False,path=self.PATH))
  19. def new_task(self,query,t):
  20. nt = task(query,t,self.last_id)
  21. self.last_id+=1
  22. self.tasks.append(nt)
  23. def task_status(self):
  24. ret=[]
  25. for t in self.tasks:
  26. elapsed= time()-t.START_TIME
  27. ret.append({"ID": t.ID,
  28. "STATUS": t.get_status(),
  29. "PROGRESS": t.get_progress(),
  30. "OUT": t.get_output(),
  31. "ELAPSED": int(elapsed),
  32. "QUERY": t.QUERY,
  33. "EXTRA": t.get_extra(),
  34. "TYPE": t.get_type()})
  35. return ret
  36. class task:
  37. CUR_STATUS=""
  38. OUTPUT=""
  39. ID=0
  40. CLIENT=None
  41. START_TIME=None
  42. QUERY=""
  43. def __init__(self,query,c,id):
  44. self.QUERY=query
  45. self.CLIENT=c
  46. self.ID=id
  47. self.START_TIME=time()
  48. pass
  49. def get_progress(self):
  50. return self.CLIENT.PROGRESS;
  51. def get_extra(self):
  52. return self.CLIENT.EXTRA_OUTPUT;
  53. def get_status(self):
  54. if time() - self.START_TIME > 600:
  55. self.CLIENT.do_timeout()
  56. self.OUTPUT=self.CLIENT.OUTPUT
  57. return self.CLIENT.STATUS
  58. def get_type(self):
  59. return self.CLIENT.TYPE
  60. def get_output(self):
  61. if type(self.OUTPUT) == str or self.OUTPUT is None:
  62. return self.OUTPUT
  63. TAGS_R = re.compile('[\[(].*?[\])]|\.rar|v\d.*?\s')
  64. books = self.OUTPUT
  65. ret=[]
  66. for book in books:
  67. book = book.replace("---","")
  68. groups = re.search("(?P<BOT>^!\w+)(?P<BOOK>.*?)(?P<INFO>::INFO.*)?$",book)
  69. if groups is None:
  70. #print("[No groups] %s" % book)
  71. return books
  72. groups=groups.groupdict()
  73. groups["BOOK"]=re.sub(TAGS_R,"",groups["BOOK"]) #remove tags from book
  74. groups["TAGS"]=[ r.strip("()[]") for r in re.findall(TAGS_R,book) ]
  75. groups["TEXT"]=book
  76. ret.append(groups)
  77. return ret
  78. #BASIC CLI INTERFACE
  79. if __name__ == "__main__":
  80. q = qManager()
  81. while True:
  82. line=input()
  83. words=shlex.split(line)
  84. print(q.task_status())
  85. if len(words)<2:
  86. continue
  87. comm=words[0]
  88. if comm.lower()=="quit":
  89. break
  90. if comm.lower()=="search":
  91. if len(words)<3:
  92. continue
  93. q.new_search(words[1],words[2])
  94. continue
  95. if comm.lower()=="dl":
  96. q.new_dl(words[1])
  97. continue
  98. print("search <'multiple keywords'> <format>|dl <line>|quit")