qmanager.py 4.4 KB

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