Advertisement
Guest User

Untitled

a guest
Dec 14th, 2018
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.89 KB | None | 0 0
  1. import curses
  2. import time
  3. import json
  4.  
  5. from redis import StrictRedis
  6.  
  7. redis_client = StrictRedis()
  8. redis_pubsub = redis_client.pubsub(ignore_subscribe_messages=True)
  9. statuses = {}
  10.  
  11. class TerminalColor:
  12.     HEADER = '\033[95m'
  13.     OKBLUE = '\033[94m'
  14.     OKGREEN = '\033[92m'
  15.     WARNING = '\033[93m'
  16.     FAIL = '\033[91m'
  17.     ENDC = '\033[0m'
  18.     BOLD = '\033[1m'
  19.     UNDERLINE = '\033[4m'
  20.  
  21. def on_warrior_message(message):
  22.     data = json.loads(message["data"])
  23.     status_key = "%s:%s" % (data["host"], data["port"])
  24.  
  25.     # Split the message
  26.     output_split = data["data"].split(" ")
  27.  
  28.     # Bad code to priortize uploads on the display.
  29.     if "%" not in data["data"] or "B/s" not in data["data"]:
  30.         if status_key in statuses and ("%" in statuses[status_key] and "B/s" in statuses[status_key]):
  31.             if "100%" not in statuses[status_key]:
  32.                 return
  33.  
  34.     # Change the color depending on status.
  35.     # XXX FUCKING NASTY
  36.     if "=404" in output_split[0]:
  37.         status_color = TerminalColor.FAIL
  38.     elif "=301" in output_split[0]:
  39.         status_color = TerminalColor.WARNING
  40.     elif "%" in data["data"] and "B/s" in data["data"]:
  41.         status_color = TerminalColor.OKBLUE
  42.     else:
  43.         status_color = ""
  44.  
  45.     statuses[status_key] = "{status_color}{host}:{port}\t {data}".format(
  46.         host=data["host"],
  47.         port=data["port"],
  48.         data=data["data"].strip(),
  49.         status_color=status_color,
  50.     )
  51.  
  52. def draw_statuses(console):
  53.     console.clear()
  54.     console.refresh()
  55.  
  56.     curses.start_color()
  57.     curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLACK)
  58.     curses.init_pair(2, curses.COLOR_CYAN, curses.COLOR_BLACK)
  59.     curses.init_pair(3, curses.COLOR_RED, curses.COLOR_BLACK)
  60.     curses.init_pair(4, curses.COLOR_YELLOW, curses.COLOR_BLACK)
  61.  
  62.     while True:
  63.         i = 1
  64.         console.addstr(0, 0, "Host\t\t\t URL")
  65.  
  66.         for host in sorted(statuses):
  67.             values = statuses[host]
  68.             color_pair = curses.color_pair(1)
  69.  
  70.             if values.startswith(TerminalColor.OKBLUE):
  71.                 values = values.lstrip(TerminalColor.OKBLUE)
  72.                 color_pair = curses.color_pair(2)
  73.             elif values.startswith(TerminalColor.FAIL):
  74.                 values = values.lstrip(TerminalColor.FAIL)
  75.                 color_pair = curses.color_pair(3)
  76.             elif values.startswith(TerminalColor.WARNING):
  77.                 values = values.lstrip(TerminalColor.WARNING)
  78.                 color_pair = curses.color_pair(4)
  79.  
  80.             console.addstr(i, 0, values, color_pair)
  81.             console.clrtoeol()
  82.             i += 1
  83.         console.refresh()
  84.         time.sleep(0.1)
  85.  
  86. redis_pubsub.subscribe(**{"tumblr:warrior": on_warrior_message})
  87. pubsub_thread = redis_pubsub.run_in_thread(sleep_time=0.001)
  88.  
  89. try:
  90.     curses.wrapper(draw_statuses)
  91. except KeyboardInterrupt:
  92.     pubsub_thread.stop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement