main.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import webbrowser
  2. import click
  3. import flask
  4. from slackviewer.app import app
  5. from slackviewer.archive import extract_archive
  6. from slackviewer.reader import Reader
  7. from slackviewer.utils.click import envvar, flag_ennvar
  8. def configure_app(app, archive, channels, no_sidebar, no_external_references, debug):
  9. app.debug = debug
  10. app.no_sidebar = no_sidebar
  11. app.no_external_references = no_external_references
  12. if app.debug:
  13. print("WARNING: DEBUG MODE IS ENABLED!")
  14. app.config["PROPAGATE_EXCEPTIONS"] = True
  15. path = extract_archive(archive)
  16. reader = Reader(path)
  17. top = flask._app_ctx_stack
  18. top.channels = reader.compile_channels(channels)
  19. top.groups = reader.compile_groups()
  20. top.dms = reader.compile_dm_messages()
  21. top.dm_users = reader.compile_dm_users()
  22. top.mpims = reader.compile_mpim_messages()
  23. top.mpim_users = reader.compile_mpim_users()
  24. @click.command()
  25. @click.option('-p', '--port', default=envvar('SEV_PORT', '5000'),
  26. type=click.INT, help="Host port to serve your content on")
  27. @click.option("-z", "--archive", type=click.Path(), required=True,
  28. default=envvar('SEV_ARCHIVE', ''),
  29. help="Path to your Slack export archive (.zip file or directory)")
  30. @click.option('-I', '--ip', default=envvar('SEV_IP', 'localhost'),
  31. type=click.STRING, help="Host IP to serve your content on")
  32. @click.option('--no-browser', is_flag=True,
  33. default=flag_ennvar("SEV_NO_BROWSER"),
  34. help="If you do not want a browser to open "
  35. "automatically, set this.")
  36. @click.option('--channels', type=click.STRING,
  37. default=envvar("SEV_CHANNELS", None),
  38. help="A comma separated list of channels to parse.")
  39. @click.option('--no-sidebar', is_flag=True,
  40. default=flag_ennvar("SEV_NO_SIDEBAR"),
  41. help="Removes the sidebar.")
  42. @click.option('--no-external-references', is_flag=True,
  43. default=flag_ennvar("SEV_NO_EXTERNAL_REFERENCES"),
  44. help="Removes all references to external css/js/images.")
  45. @click.option('--test', is_flag=True, default=flag_ennvar("SEV_TEST"),
  46. help="Runs in 'test' mode, i.e., this will do an archive extract, but will not start the server,"
  47. " and immediately quit.")
  48. @click.option('--debug', is_flag=True, default=flag_ennvar("FLASK_DEBUG"))
  49. def main(port, archive, ip, no_browser, channels, no_sidebar, no_external_references, test, debug):
  50. if not archive:
  51. raise ValueError("Empty path provided for archive")
  52. configure_app(app, archive, channels, no_sidebar, no_external_references, debug)
  53. if not no_browser and not test:
  54. webbrowser.open("http://{}:{}".format(ip, port))
  55. if not test:
  56. app.run(
  57. host=ip,
  58. port=port
  59. )