messages.py 5.4 KB

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