Skip to content

Commit

Permalink
Implement Reddit auto posting and commenting
Browse files Browse the repository at this point in the history
  • Loading branch information
Casper-Guo committed Nov 25, 2024
1 parent 9639c51 commit 72d4e89
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# secrets
*.pem*
.password
*.log
praw.ini
aws_login.sh

# playground notebooks
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ build-backend = "setuptools.build_meta"
packages = ["f1_visualization"]

[tool.ruff]
include = ["f1_visualization/*.py", "app.py"]
include = ["f1_visualization/*.py", "./*.py"]
line-length = 96
indent-width = 4

Expand Down
110 changes: 110 additions & 0 deletions reddit_machine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
"""Automatically post to Reddit when new README graphics are made."""

import logging
import time

import fastf1 as f
import praw

from f1_visualization._consts import CURRENT_SEASON, ROOT_PATH
from f1_visualization.preprocess import get_last_round_number

logging.basicConfig(level=logging.INFO, format="%(levelname)s\t%(filename)s\t%(message)s")
logger = logging.getLogger(__name__)


VISUALS_PATH = ROOT_PATH / "Docs" / "visuals"


def main():
"""Submit posts and make one comment."""
reddit = praw.Reddit("armchair-strategist")
r_formula1 = reddit.subreddit("formula1")
r_f1technical = reddit.subreddit("f1technical")
formula1_flairs = r_formula1.flair.link_templates.user_selectable()
f1technical_flairs = r_f1technical.flair.link_templates.user_selectable()
formula1_flair_id = next(
flair for flair in formula1_flairs if "Statistics" in flair["flair_text"]
)["flair_template_id"]
f1technical_flair_id = next(
flair for flair in f1technical_flairs if "Strategy" in flair["flair_text"]
)["flair_template_id"]

session = f.get_session(CURRENT_SEASON, get_last_round_number(), "R")
session.load(telemetry=False, weather=False, messages=False)
event_name = f"{session.event['EventName']} - {session.name}"

images = [
{
"image_path": VISUALS_PATH / "strategy.png",
"caption": "Tyre strategy recap. Check out more at armchair-strategist.dev!",
},
{
"image_path": VISUALS_PATH / "podium_gap.png",
"caption": (
"Podium finishers' gaps to winners. "
"Check out more at armchair-strategist.dev!"
),
},
{
"image_path": VISUALS_PATH / "position.png",
"caption": "Race position history. Check out more at armchair-strategist.dev!",
},
{
"image_path": VISUALS_PATH / "laptime.png",
"caption": "Point finishers' lap times. Check out more at armchair-strategist.dev!",
},
{
"image_path": VISUALS_PATH / "team_pace.png",
"caption": "Team pace ranking. Check out more at armchair-strategist.dev!",
},
{
"image_path": VISUALS_PATH / "teammate_violin.png",
"caption": (
"Driver pace ranking (teammates vs teammates). "
"Check out more at armchair-strategist.dev!"
),
},
{
"image_path": VISUALS_PATH / "driver_pace.png",
"caption": (
"Driver pace ranking (finishing order). "
"Check out more at armchair-strategist.dev!"
),
},
]

formula1_post = r_formula1.submit_gallery(
title=f"{event_name} Strategy & Performance Recap",
images=images,
flair_id=formula1_flair_id,
)
formula1_post.reply(
(
"What other graphics do you want to see and "
"how can these existing graphics be improved, quesion."
)
)
logger.info("Finished posting to r/formula1")

time.sleep(5)

f1technical_post = r_f1technical.submit_gallery(
title=f"{event_name} Strategy & Performance Recap",
images=images,
flair_id=f1technical_flair_id,
)
f1technical_post.reply(
(
"Check out the interactive version of these graphics and more "
"at my [strategy dashboard](https://armchair-strategist.dev/)"
"\n\n"
"Please let me know if you have suggestions for improving these graphics "
"or ideas for other graphics!"
)
)
logger.info("Finished posting to r/f1technical")


if __name__ == "__main__":
main()

0 comments on commit 72d4e89

Please sign in to comment.