#!/usr/bin/env python3

import datetime
import socket

_SERVERS = [
       ("661.org",70),
       ("aussies.space",70),
       ("box.matto.nl",70),
       ("colorfield.space",70),
       ("devio.us",70),
       ("dome.circumlunar.space",70),
       ("first-monitor.ethz.ch",70),
       ("fuckup.solutions",70),
       ("gopher.club",70),
       ("grex.org",70),
       ("heavysquare.com",70),
       ("dante.pglaf.org",70),
       ("sdf.org",70),
       ("radiofreqs.space",70),
       ("rawtext.club",70),
       ("telefisk.org",70),
       ("thunix.net",70),
       ("tilde.center",70),
       ("tildecow.com",70),
       ("tilde.institute",70),
       ("tilde.land",70),
       ("tilde.town",70),
       ("tilde.team",70),
       ("tellus.strangled.net",70),
       ("typed-hole.org",70),
       ("vger.cloud", 70),
       ("zaibatsu.circumlunar.space",70),
   ]

def get_server_stats(address):
   host, port = address
   s = socket.create_connection((host, port))
   s.settimeout(60.0) # Grex is slooooow
   s.sendall(b"/server-status\r\n")
   fp = s.makefile()
   stats = { "host": host}
   for line in fp:
       key, value = line.split(":",1)
       key = key.strip()
       value = value.strip()
       try:
           stats[key] = int(value)
       except ValueError:
           stats[key] = value
   return stats

def poll_servers(servers):
   stats = []
   for address in servers:
       try:
           s = get_server_stats(address)
           if "Uptime" in s:
               stats.append(s)
       except Exception as e:
#            print("Error for %s: %s" % (address[0], str(e)))
           pass
   return stats

def main():
   stats = poll_servers(_SERVERS)
   stats.sort(key=lambda s:int(s["Uptime"]), reverse=True)
   print("")
   print("THE GREAT".center(32+12+12+12))
   print("GOPHERNICUS SERVER".center(32+12+12+12))
   print("UPTIME LEADERBOARD".center(32+12+12+12))
   print("")
   print(("Updated %s" % datetime.date.today().strftime("%b %d %Y")).center(32+12+12+12))
   print("")
   print("HOST".ljust(32) + "UPTIME".ljust(12) + "REQS/DAY".ljust(12) + "    kB/DAY".ljust(12))
   print("-"*(32+12+12+12))
   total_days = 0
   total_accesses = 0
   total_kbytes = 0
   for s in stats:
       days = s["Uptime"]/(60*60*24)
       accesses = s["Total Accesses"]/days
       kbytes = s["Total kBytes"]/days
       total_days += days
       total_accesses += accesses
       total_kbytes += kbytes
       print(s["host"].ljust(32)
               + ("%d days" % days).ljust(12)
               + "{:,}".format(int(accesses)).rjust(8) + "    "
               + "{:,}".format(int(kbytes)).rjust(10)
               )
   print("")
   print("TOTAL".ljust(32)
               + ("%d days" % total_days).ljust(12)
               + "{:,}".format(int(total_accesses)).rjust(8) + "    "
               + "{:,}".format(int(total_kbytes)).rjust(10)
               )
   print("")
   print("Is your server missing?  Email <[email protected]>".center(32+12+12+12))
   print("")

if __name__ == "__main__":
   main()