Skip to content

Commit

Permalink
allow for the option to pass in output dir
Browse files Browse the repository at this point in the history
  • Loading branch information
hodgesmr committed Dec 4, 2022
1 parent acb1794 commit 9afd8f4
Showing 1 changed file with 29 additions and 19 deletions.
48 changes: 29 additions & 19 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import argparse
import os
import sys
import tempfile
import webbrowser
from pathlib import Path
from typing import TYPE_CHECKING

from jinja2 import Environment, FileSystemLoader
Expand All @@ -19,16 +18,12 @@
from thresholds import Threshold


def render_and_open_digest(context: dict) -> None:
def render_digest(context: dict, output_dir: Path) -> None:
environment = Environment(loader=FileSystemLoader("templates/"))
template = environment.get_template("digest.html.jinja")
output_html = template.render(context)

with tempfile.NamedTemporaryFile("w", delete=False, suffix=".html") as out_file:
final_url = f"file://{out_file.name}"
out_file.write(output_html)

webbrowser.open(final_url)
output_file_path = output_dir / 'index.html'
output_file_path.write_text(output_html)


def run(
Expand All @@ -38,6 +33,7 @@ def run(
mastodon_token: str,
mastodon_base_url: str,
mastodon_username: str,
output_dir: Path,
) -> None:

print(f"Building digest from the past {hours} hours...")
Expand All @@ -54,22 +50,26 @@ def run(
threshold_posts = threshold.posts_meeting_criteria(posts, scorer)
threshold_boosts = threshold.posts_meeting_criteria(boosts, scorer)

# 3. Build and show the digest
render_and_open_digest(
# 3. Build the digest
render_digest(
context={
"hours": hours,
"posts": threshold_posts,
"boosts": threshold_boosts,
"mastodon_base_url": mastodon_base_url,
}
},
output_dir=output_dir,
)


if __name__ == "__main__":
scorers = get_scorers()
thresholds = get_thresholds()

arg_parser = argparse.ArgumentParser(prog="mastodon_digest")
arg_parser = argparse.ArgumentParser(
prog="mastodon_digest",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
arg_parser.add_argument(
"-n",
choices=range(1, 25),
Expand All @@ -83,8 +83,7 @@ def run(
choices=list(scorers.keys()),
default="SimpleWeighted",
dest="scorer",
help="""Which post scoring criteria to use.
SimpleWeighted is the default.
help="""Which post scoring criteria to use.
Simple scorers take a geometric mean of boosts and favs.
Extended scorers include reply counts in the geometric mean.
Weighted scorers multiply the score by an inverse sqaure root
Expand All @@ -96,15 +95,25 @@ def run(
choices=list(thresholds.keys()),
default="normal",
dest="threshold",
help="""Which post threshold criteria to use.
Normal is the default.
lax = 90th percentile
normal = 95th percentile
help="""Which post threshold criteria to use.
lax = 90th percentile,
normal = 95th percentile,
strict = 98th percentile
""",
)
arg_parser.add_argument(
"-o",
default="./render/",
dest="output_dir",
help="Output directory for the rendered digest",
required=False,
)
args = arg_parser.parse_args()

output_dir = Path(args.output_dir)
if not output_dir.exists() or not output_dir.is_dir():
sys.exit(f"Output directory not found: {args.output_dir}")

mastodon_token = os.getenv("MASTODON_TOKEN")
mastodon_base_url = os.getenv("MASTODON_BASE_URL")
mastodon_username = os.getenv("MASTODON_USERNAME")
Expand All @@ -123,4 +132,5 @@ def run(
mastodon_token,
mastodon_base_url,
mastodon_username,
output_dir,
)

0 comments on commit 9afd8f4

Please sign in to comment.