From 0b6c7d4ae77e66e50b44571e081ea1f60c7ae2dd Mon Sep 17 00:00:00 2001 From: fleinen <30873875+fleinen@users.noreply.github.com> Date: Thu, 21 Nov 2024 21:40:50 +0100 Subject: [PATCH] add option to define own quote prompts (#12) --- .env.example | 3 ++- README.md | 2 ++ src/weekly_strava_stats/cli/commands.py | 10 ++++--- .../utils/message_builder.py | 27 ++++++++++--------- 4 files changed, 24 insertions(+), 18 deletions(-) diff --git a/.env.example b/.env.example index 5472ee2..056402b 100644 --- a/.env.example +++ b/.env.example @@ -2,4 +2,5 @@ STRAVA_WIDGET_URL="" STRAVA_CLUB_URL="" SLACK_BOT_TOKEN="" SLACK_CHANNEL="" -GROQ_API_KEY="" \ No newline at end of file +GROQ_API_KEY="" +QUOTE_PROMPTS_PATH="" \ No newline at end of file diff --git a/README.md b/README.md index 0025a09..c5064ad 100644 --- a/README.md +++ b/README.md @@ -80,5 +80,7 @@ There will be a quote at the end of every message. If a Groq API key is provided You can generte an API key [here](https://console.groq.com/keys). The key must be set as an environment variable with the key `GROQ_API_KEY` which can be set in the `.env` file. +By default, the predefined prompts from the file `src/weeklt_strava_stats/data/quote_prompts.csv` will be used. If you want to use your own prompts, you can set the environment variable `QUOTE_PROMPTS_PATH` in the `.env` file. + ### Optional: Add a link to the Strava Club If you want to include a link to the Strava club in the message, you can set the environment variable `STRAVA_CLUB_URL` in the `.env` file. \ No newline at end of file diff --git a/src/weekly_strava_stats/cli/commands.py b/src/weekly_strava_stats/cli/commands.py index aeb86a8..b51d38e 100644 --- a/src/weekly_strava_stats/cli/commands.py +++ b/src/weekly_strava_stats/cli/commands.py @@ -1,5 +1,5 @@ -import logging import os +import logging import click from dotenv import load_dotenv @@ -74,9 +74,11 @@ def post_last_weeks_stats(): logging.error("No stats found for last week") return - groq_api_key = os.getenv('GROQ_API_KEY') - strava_club_url = os.getenv('STRAVA_CLUB_URL') - message_builder = MessageBuilder(groq_api_key, strava_club_url) + message_builder = MessageBuilder( + groq_api_key=os.getenv('GROQ_API_KEY'), + quote_prompts_path=os.getenv('QUOTE_PROMPTS_PATH'), + club_url=os.getenv('STRAVA_CLUB_URL') + ) message = message_builder.build(last_weeks_stats, compare_stats) slack_connector = SlackChannelConnector(slack_token, slack_channel) diff --git a/src/weekly_strava_stats/utils/message_builder.py b/src/weekly_strava_stats/utils/message_builder.py index b1ed8ec..7332cb7 100644 --- a/src/weekly_strava_stats/utils/message_builder.py +++ b/src/weekly_strava_stats/utils/message_builder.py @@ -1,3 +1,4 @@ +from pathlib import Path import random from typing import Optional from weekly_strava_stats.storage.week_stats import WeekStats @@ -7,17 +8,22 @@ class MessageBuilder: - def __init__(self, groq_api_key: Optional[str], club_url: Optional[str] = None): + def __init__(self, groq_api_key: Optional[str], quote_prompts_path: Optional[Path] = None, club_url: Optional[str] = None): """ - Initialize the MessageBuilder with the given GROQ API key. + Initialize the MessageBuilder. Args: - groq_api_key (Optional[str]): The API key for accessing the GROQ service. Can be None if not required. - club_url (Optional[str], optional): The link to the Strava club. Defaults to None. + groq_api_key (str, optional): The API key for accessing the GROQ service. Can be None if not required. + quote_prompts_path (Path, optional): The path to the quote prompts file. If not provided, the default prompts are used. Defaults to None. + club_url (str, optional): The link to the Strava club. Defaults to None. """ self.groq_api_key = groq_api_key self.club_url = club_url + data_path = resources.files(weekly_strava_stats).joinpath("data") + self.quote_prompts_path = quote_prompts_path if quote_prompts_path else data_path / "quote_prompts.csv" + self.default_prompt_path = data_path / "quotes.txt" + def build(self, stats: WeekStats, last_week_stats: Optional[WeekStats] = None) -> str: """ Builds a message string containing the current week's statistics and a quote. @@ -118,7 +124,7 @@ def get_quote(self, original_message: str) -> str: str: The retrieved or generated quote. """ if not self.groq_api_key: - return MessageBuilder.get_default_quote() + return self.get_default_quote() return MessageBuilder.get_generated_quote(self, original_message) def get_generated_quote(self, original_message: str) -> str: @@ -129,8 +135,7 @@ def get_generated_quote(self, original_message: str) -> str: Returns: str: A generated quote formatted with the author's name and the quote in italics. """ - with resources.path(weekly_strava_stats, 'data') as data_dir: - quotes_path = data_dir / "quote_prompts.csv" + quotes_path = self.quote_prompts_path with open(quotes_path, 'r') as quotes_file: # read without header (first line) @@ -156,18 +161,14 @@ def get_generated_quote(self, original_message: str) -> str: message = f"{author}: _{fake_quote}_" return message - @staticmethod - def get_default_quote() -> str: + def get_default_quote(self) -> str: """ Retrieves a random quote from a predefined list of quotes stored in a text file. Returns: str: A randomly selected quote from the quotes file. """ - with resources.path(weekly_strava_stats, 'data') as data_dir: - quotes_path = data_dir / "quotes.txt" - - with open(quotes_path, 'r') as quotes_file: + with open(self.default_quote_path, 'r') as quotes_file: default_quotes = quotes_file.readlines() return f"_{random.choice(default_quotes)}_"