monitor.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. from colorama import Fore, Back, Style
  2. import utils
  3. from api import ChanAPI
  4. class Monitor:
  5. def __init__(self, board):
  6. self.board = board
  7. self.newest = 0
  8. self.old = 0
  9. self.api = ChanAPI(self.board)
  10. def __print_details(self, no):
  11. ''' clean and format the to be printed post '''
  12. print(Fore.BLUE +f"=== New Thread Posted {self.board} thread no:{no} ===")
  13. print(Style.RESET_ALL)
  14. r = self.api.getThread(no)
  15. if "sub" in r["posts"][0]:
  16. title = utils.cleantrash(r["posts"][0]["sub"])
  17. print(title)
  18. if "com" in r["posts"][0]:
  19. comment = utils.cleantrash(r["posts"][0]["com"])
  20. print(comment)
  21. print("\n")
  22. def watch(self):
  23. ''' Find the newest thread and prints its details '''
  24. r = self.api.getCatalog()
  25. l = utils.getThreadList(r)
  26. l.sort()
  27. self.newest_thread = l[-1]
  28. if self.newest_thread > self.old:
  29. self.old = self.newest_thread
  30. self.newset_thread = l[0]
  31. self.__print_details(self.newest_thread)