messages.py 5.0 KB

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