messages.py 3.5 KB

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