messages.py 3.8 KB

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