qmanager.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #!/usr/bin/env python3
  2. """Manage a list of book-jobs,
  3. return a description list for the website"""
  4. import shlex
  5. import re
  6. from time import time
  7. from bot import IRCClient
  8. class qManager:
  9. """Main module class"""
  10. tasks = []
  11. last_id = 0
  12. PATH = ""
  13. def __init__(self, path="/tmp/"):
  14. """Init the manager. Set output path"""
  15. self.PATH = path
  16. def new_dl(self, string):
  17. """Download a book"""
  18. irc_client = IRCClient(
  19. string,
  20. "",
  21. "BOOK",
  22. logging=False,
  23. path=self.PATH)
  24. self.new_task(string, irc_client)
  25. def new_search(self, keywords, fmt):
  26. """Search for a book"""
  27. irc_client = IRCClient(
  28. keywords,
  29. fmt,
  30. "SEARCH",
  31. logging=False,
  32. path=self.PATH)
  33. self.new_task(keywords, irc_client)
  34. def new_task(self, query, t):
  35. """ Add a new 'task' to the list """
  36. nt = task(query, t, self.last_id)
  37. self.last_id += 1
  38. self.tasks.append(nt)
  39. def task_status(self):
  40. """ Return a dict with the status of each 'task' """
  41. ret = []
  42. for t in self.tasks:
  43. elapsed = time() - t.START_TIME
  44. ret.append({"ID": t.ID,
  45. "STATUS": t.get_status(),
  46. "PROGRESS": t.get_progress(),
  47. "OUT": t.get_output(),
  48. "ELAPSED": int(elapsed),
  49. "QUERY": t.QUERY,
  50. "EXTRA": t.get_extra(),
  51. "TYPE": t.get_type()})
  52. return ret
  53. class task:
  54. """ Task class. Only returns a status"""
  55. CUR_STATUS = ""
  56. OUTPUT = ""
  57. ID = 0
  58. CLIENT = None
  59. START_TIME = None
  60. QUERY = ""
  61. def __init__(self, query, c, _id):
  62. """Initialize the task"""
  63. self.QUERY = query
  64. self.CLIENT = c
  65. self.ID = _id
  66. self.START_TIME = time()
  67. def get_progress(self):
  68. """ Return progress."""
  69. return self.CLIENT.PROGRESS
  70. def get_extra(self):
  71. """ Return extra output."""
  72. return self.CLIENT.EXTRA_OUTPUT
  73. def get_status(self):
  74. """ Return status. If timed out since last call, stop task """
  75. if time() - self.START_TIME > 600:
  76. self.CLIENT.do_timeout()
  77. self.OUTPUT = self.CLIENT.OUTPUT
  78. return self.CLIENT.STATUS
  79. def get_type(self):
  80. """ Return type """
  81. return self.CLIENT.TYPE
  82. def get_output(self):
  83. """Return output:
  84. If it's just a string, return it (Books).
  85. If it's a list, parse it and return a representative dict.
  86. Should cache this or something, doesn't make sense to
  87. calculate on each call.
  88. """
  89. if isinstance(self.OUTPUT, str) or self.OUTPUT is None:
  90. return self.OUTPUT
  91. TAGS_R = re.compile(r'[\[(].*?[\])]|\.rar|v\d.*?\s')
  92. books = self.OUTPUT
  93. ret = []
  94. for book in books:
  95. book = book.replace("---", "")
  96. groups = re.search(
  97. r"(?P<BOT>^!\w+)(?P<BOOK>.*?)(?P<INFO>::INFO.*)?$", book)
  98. if groups is None:
  99. #print("[No groups] %s" % book)
  100. return books
  101. groups = groups.groupdict()
  102. # remove tags from book
  103. groups["BOOK"] = re.sub(TAGS_R, "", groups["BOOK"])
  104. groups["TAGS"] = [r.strip("()[]")
  105. for r in re.findall(TAGS_R, book)]
  106. groups["TEXT"] = book
  107. ret.append(groups)
  108. return ret
  109. # BASIC CLI INTERFACE
  110. if __name__ == "__main__":
  111. q = qManager()
  112. while True:
  113. line = input()
  114. words = shlex.split(line)
  115. print(q.task_status())
  116. if len(words) < 2:
  117. continue
  118. comm = words[0]
  119. if comm.lower() == "quit":
  120. break
  121. if comm.lower() == "search":
  122. if len(words) < 3:
  123. continue
  124. q.new_search(words[1], words[2])
  125. continue
  126. if comm.lower() == "dl":
  127. q.new_dl(words[1])
  128. continue
  129. print("search <'multiple keywords'> <format>|dl <line>|quit")