messages.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. from flask import Flask, render_template, make_response
  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. # cache = Cache(app, config={"CACHE_TYPE": "redis", "CACHE_REDIS_HOST": "olympus"})
  19. import jammin
  20. bases = {
  21. "FSXNET-General": "msgs/fsx_gen",
  22. "FSXNET-BBS": "msgs/fsx_bbs",
  23. "FSXNET-BOT": "msgs/fsx_bot",
  24. "FSXNET-Magicka": "msgs/fsx_mag",
  25. "HappyNet-General": "msgs/hpy_gen",
  26. }
  27. # bases = {"FSX_BOT": "fsx_bot"}
  28. # @cache.memoize(timeout=5 * 60, key_prefix="messages")
  29. @cache.memoize(timeout=5 * 60)
  30. def get_messages(base):
  31. messages = jammin.get_messages(base)
  32. messages.reverse()
  33. return messages
  34. @cache.memoize(timeout=60)
  35. def get_message(base, msgno):
  36. message = jammin.read_message(base, msgno)
  37. return message
  38. @app.errorhandler(404)
  39. def not_found(e):
  40. return render_template("404.html")
  41. @app.route(base_path + "/list")
  42. def list_bases():
  43. return render_template(
  44. "list.html", bases=bases, base_path=base_path, title="Message Areas"
  45. )
  46. # return 'Here would be a listing of message bases'
  47. @app.route(base_path + "/clear")
  48. def clear_cache():
  49. cache.clear()
  50. return "Cache Cleared. Back to hitting refresh!"
  51. @app.route(base_path + "/messages/<area>")
  52. def display_messages(area):
  53. if area not in bases:
  54. return render_template(
  55. "missing-area.html", base_path=base_path, title="Missing Area"
  56. )
  57. # messages = jammin.get_messages(bases[area])
  58. messages = get_messages(bases[area])
  59. # messages.reverse() # cached.reverse()
  60. page = request.args.get(get_page_parameter(), type=int, default=1)
  61. # get_page_arg defaults to page 1, per_page of 10
  62. PER_PAGE = 50
  63. total = len(messages)
  64. pagination = Pagination(
  65. page=page,
  66. total=total,
  67. css_framework="foundation",
  68. record_name="messages",
  69. per_page=PER_PAGE,
  70. )
  71. page, per_page, offset = get_page_args()
  72. start = (page - 1) * PER_PAGE
  73. end = start + PER_PAGE
  74. # messages = messages[(page-1) * PER_PAGE:offset+PER_PAGE]
  75. messages = messages[start:end]
  76. return render_template(
  77. "messages.html",
  78. messages=messages,
  79. area=area,
  80. pagination=pagination,
  81. base_path=base_path,
  82. title="Messages for " + bases[area],
  83. )
  84. @cache.memoize(timeout=60)
  85. def ansi_to_png(raw_ansi_bytes, idx):
  86. pid = os.getppid()
  87. ansifile = "{0}-{1}.ans".format(idx, pid)
  88. pngfile = "{0}-{1}.png".format(idx, pid)
  89. with open(ansifile, "wb") as fp:
  90. fp.write(raw_ansi_bytes)
  91. subprocess.run(["./ansilove", "-d", "-o", pngfile, ansifile])
  92. with open(pngfile, "rb") as fp:
  93. png = fp.read()
  94. os.unlink(ansifile)
  95. os.unlink(pngfile)
  96. return png
  97. def ansi_to_png64(raw_ansi_bytes, idx):
  98. png = ansi_to_png(raw_ansi_bytes, idx)
  99. return base64.b64encode(png).decode("utf-8")
  100. @app.route(base_path + "/image/<area>/<int:msgno>.png")
  101. def display_ansi(area, msgno):
  102. if area not in bases:
  103. return "RATS", 404
  104. message = get_message(bases[area], msgno)
  105. if not message:
  106. return "RATS", 404
  107. if not "text" in message:
  108. return "RATS", 404
  109. png = ansi_to_png(message["bytes"].replace(b"\r", b"\n"), msgno)
  110. response = make_response(png)
  111. response.headers.set("Content-Type", "image/png")
  112. return response
  113. # <img alt="My Image" src="data:image/png;base64,
  114. @app.route(base_path + "/read/<area>/<int:msgno>")
  115. def display_message(area, msgno):
  116. if area not in bases:
  117. return render_template(
  118. "missing-area.html", base_path=base_path, title="Missing Area"
  119. )
  120. # message = jammin.read_message(bases[area], msgno)
  121. message = get_message(bases[area], msgno)
  122. if not message:
  123. return render_template(
  124. "missing-message.html",
  125. base_path=base_path,
  126. area=area,
  127. title="Missing Message",
  128. )
  129. if "text" in message:
  130. if "\x1b" in message["text"]:
  131. # Ok, the message contains ANSI CODES -- Convert
  132. message["png"] = True
  133. # message["png"] = ansi_to_png64(
  134. # message["bytes"].replace(b"\r", b"\n"), msgno
  135. # )
  136. else:
  137. message["text"] = message["text"].replace("\r", "\n") # <br >\n")
  138. return render_template(
  139. "message.html",
  140. message=message,
  141. area=area,
  142. msgnumber=msgno,
  143. base_path=base_path,
  144. title="Message {0}".format(msgno),
  145. )