messages.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. cache = Cache(app, config={"CACHE_TYPE": "redis", "CACHE_REDIS_HOST": "redis"})
  18. import jammin
  19. bases = {"FSX_General": "msgs/fsx_gen", "FSX_BBS": "msgs/fsx_bbs", "FSX_BOT": "msgs/fsx_bot", "FSX_Magicka": "msgs/fsx_mag", "HappyNet": "msgs/hpy_gen"}
  20. # @cache.memoize(timeout=5 * 60, key_prefix="messages")
  21. @cache.memoize(timeout=5 * 60)
  22. def get_messages(base):
  23. messages = jammin.get_messages(base)
  24. messages.reverse()
  25. return messages
  26. @cache.memoize(timeout=60)
  27. def get_message(base, msgno):
  28. message = jammin.read_message(base, msgno)
  29. return message
  30. @app.route(base_path + "/list")
  31. def list_bases():
  32. return render_template(
  33. "list.html", bases=bases, base_path=base_path, title="Message Areas"
  34. )
  35. # return 'Here would be a listing of message bases'
  36. @app.route(base_path + "/messages/<area>")
  37. def display_messages(area):
  38. if area not in bases:
  39. return render_template(
  40. "missing-area.html", base_path=base_path, title="Missing Area"
  41. )
  42. # messages = jammin.get_messages(bases[area])
  43. messages = get_messages(bases[area])
  44. # messages.reverse() # cached.reverse()
  45. page = request.args.get(get_page_parameter(), type=int, default=1)
  46. # get_page_arg defaults to page 1, per_page of 10
  47. PER_PAGE = 50
  48. total = len(messages)
  49. pagination = Pagination(
  50. page=page,
  51. total=total,
  52. css_framework="foundation",
  53. record_name="messages",
  54. per_page=PER_PAGE,
  55. )
  56. page, per_page, offset = get_page_args()
  57. start = (page - 1) * PER_PAGE
  58. end = start + PER_PAGE
  59. # messages = messages[(page-1) * PER_PAGE:offset+PER_PAGE]
  60. messages = messages[start:end]
  61. return render_template(
  62. "messages.html",
  63. messages=messages,
  64. area=area,
  65. pagination=pagination,
  66. base_path=base_path,
  67. title="Messages for " + bases[area],
  68. )
  69. def ansi_to_png(raw_ansi_bytes, idx):
  70. pid = os.getppid()
  71. ansifile = "{0}-{1}.ans".format(idx, pid)
  72. pngfile = "{0}-{1}.png".format(idx, pid)
  73. with open(ansifile, "wb") as fp:
  74. fp.write(raw_ansi_bytes)
  75. subprocess.run(["./ansilove", "-d", "-o", pngfile, ansifile])
  76. with open(pngfile, "rb") as fp:
  77. png = fp.read()
  78. os.unlink(ansifile)
  79. os.unlink(pngfile)
  80. return base64.b64encode(png).decode("utf-8")
  81. # <img alt="My Image" src="data:image/png;base64,
  82. @app.route(base_path + "/read/<area>/<int:msgno>")
  83. def display_message(area, msgno):
  84. if area not in bases:
  85. return render_template(
  86. "missing-area.html", base_path=base_path, title="Missing Area"
  87. )
  88. # message = jammin.read_message(bases[area], msgno)
  89. message = get_message(bases[area], msgno)
  90. if not message:
  91. return render_template(
  92. "missing-message.html", base_path=base_path, area=area, title="Missing Message"
  93. )
  94. if "text" in message:
  95. if "\x1b" in message["text"]:
  96. # Ok, the message contains ANSI CODES -- Convert
  97. message["png"] = ansi_to_png(message["bytes"].replace(b"\r", b"\n"), msgno)
  98. else:
  99. message["text"] = message["text"].replace("\r", "\n") # <br >\n")
  100. return render_template(
  101. "message.html",
  102. message=message,
  103. area=area,
  104. msgnumber=msgno,
  105. base_path=base_path,
  106. title="Message {0}".format(msgno),
  107. )