messages.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. from flask import Flask, render_template
  2. from flask_paginate import Pagination, get_page_parameter, get_page_args
  3. from flask_caching import Cache
  4. from flask import request
  5. import pendulum
  6. import subprocess
  7. import base64
  8. import os
  9. base_path = "/messagebase"
  10. app = Flask(__name__, static_url_path=base_path + "/static")
  11. @app.template_filter("datefmt")
  12. def format_datetime(value):
  13. dt = pendulum.from_timestamp(value, tz=pendulum.tz.local_timezone())
  14. return dt.to_datetime_string()
  15. # Check Configuring Flask-Caching section for more details
  16. cache = Cache(app, config={"CACHE_TYPE": "filesystem", "CACHE_DIR": "cache"})
  17. import jammin
  18. bases = {"FSX_GEN": "fsx_bot", "FSX_BOT": "fsx_bot"}
  19. @cache.cached(timeout=5 * 60, key_prefix="messages")
  20. def get_messages(base):
  21. messages = jammin.get_messages(base)
  22. messages.reverse()
  23. return messages
  24. @app.route(base_path + "/list")
  25. def list_bases():
  26. return render_template(
  27. "list.html", bases=bases, base_path=base_path, title="Message Areas"
  28. )
  29. # return 'Here would be a listing of message bases'
  30. @app.route(base_path + "/messages/<area>")
  31. def display_messages(area):
  32. if area not in bases:
  33. return render_template(
  34. "missing-area.html", base_path=base_path, title="Missing Area"
  35. )
  36. # messages = jammin.get_messages(bases[area])
  37. messages = get_messages(bases[area])
  38. # messages.reverse() # cached.reverse()
  39. page = request.args.get(get_page_parameter(), type=int, default=1)
  40. # get_page_arg defaults to page 1, per_page of 10
  41. PER_PAGE = 50
  42. total = len(messages)
  43. pagination = Pagination(
  44. page=page,
  45. total=total,
  46. css_framework="foundation",
  47. record_name="messages",
  48. per_page=PER_PAGE,
  49. )
  50. page, per_page, offset = get_page_args()
  51. start = (page - 1) * PER_PAGE
  52. end = start + PER_PAGE
  53. # messages = messages[(page-1) * PER_PAGE:offset+PER_PAGE]
  54. messages = messages[start:end]
  55. return render_template(
  56. "messages.html",
  57. messages=messages,
  58. area=area,
  59. pagination=pagination,
  60. base_path=base_path,
  61. title="Messages for " + bases[area],
  62. )
  63. def ansi_to_png(raw_ansi_bytes, idx):
  64. pid = os.getppid()
  65. ansifile = "{0}-{1}.ans".format(idx, pid)
  66. pngfile = "{0}-{1}.png".format(idx, pid)
  67. with open(ansifile, "wb") as fp:
  68. fp.write(raw_ansi_bytes)
  69. subprocess.run(["./ansilove", "-d", "-o", pngfile, ansifile])
  70. with open(pngfile, "rb") as fp:
  71. png = fp.read()
  72. os.unlink(ansifile)
  73. os.unlink(pngfile)
  74. return base64.b64encode(png).decode("utf-8")
  75. # <img alt="My Image" src="data:image/png;base64,
  76. @app.route(base_path + "/read/<area>/<int:msgno>")
  77. def display_message(area, msgno):
  78. if area not in bases:
  79. return render_template(
  80. "missing-area.html", base_path=base_path, title="Missing Area"
  81. )
  82. message = jammin.read_message(bases[area], msgno)
  83. if "text" in message:
  84. if "\x1b" in message["text"]:
  85. # Ok, the message contains ANSI CODES -- Convert
  86. message["png"] = ansi_to_png(message["bytes"].replace(b"\r", b"\n"), msgno)
  87. else:
  88. message["text"] = message["text"].replace("\r", "\n") # <br >\n")
  89. return render_template(
  90. "message.html",
  91. message=message,
  92. area=area,
  93. msgnumber=msgno,
  94. base_path=base_path,
  95. title="Message {0}".format(msgno),
  96. )