messages.py 2.8 KB

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