app.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/usr/bin/env python3
  2. from telnetlib import Telnet
  3. import toml
  4. from flask import Flask, jsonify, render_template_string
  5. app = Flask(__name__)
  6. def get_users():
  7. with open("secret.toml") as infile:
  8. cfg = toml.load(infile)
  9. login = ("login %s %s\n" % (cfg["user"], cfg["pass"])).encode("utf-8")
  10. with Telnet(cfg["host"], cfg["port"], 5) as tn:
  11. tn.write(login)
  12. tn.write(b"use 1 -virtual\n")
  13. tn.write(b"clientlist\n")
  14. tn.write(b"whoami\n")
  15. tn.write(b"quit\n")
  16. response = tn.read_until(b"virtualserver_status").decode("ascii")
  17. return [x.split("=", 1)[1] for x in response.split()
  18. if x.startswith("client_nickname") and "serveradmin" not in x]
  19. @app.route("/")
  20. def get_status():
  21. return jsonify({"users": get_users()})
  22. @app.route("/page")
  23. def get_status_page():
  24. users = get_users()
  25. if len(users) == 0:
  26. text = "No one in teamspeak!"
  27. elif len(users) == 1:
  28. text = f"Only {users[0]}"
  29. else:
  30. text = "I see the following people: " + ", ".join(users)
  31. return render_template_string("""
  32. <!doctype html>
  33. <title>Teamspeak Server Status</title>
  34. <style>
  35. body {
  36. margin: 0px 0px 0px 0px;
  37. padding: 0px 0px 0px 0px;
  38. font-family: verdana, arial, helvetica, sans-serif;
  39. color: #ccc;
  40. background-color: #333;
  41. }
  42. h1 {
  43. font-size: 24px;
  44. line-height: 44px;
  45. font-weight: bold;
  46. margin-top: 0;
  47. margin-bottom: 0;
  48. }
  49. </style>
  50. {% if scrolling %}
  51. <marquee direction="up" style="height 600px" behavior="alternate">
  52. <marquee direction="right" style="width 600px" behavior="alternate">
  53. {% endif %}
  54. <div class="page">
  55. <h1>TeamSpeak Server Status</h1>
  56. {{ r }}
  57. </div>
  58. {% if scrolling %}
  59. </marquee>
  60. </marquee>
  61. {% endif %}
  62. """, scrolling=True, r=text)
  63. if __name__ == "__main__":
  64. app.run("0.0.0.0", 5000, debug=True, threaded=True)