From 7bae98f0e95fc0a4f0193a3f8f296e69e839210f Mon Sep 17 00:00:00 2001 From: Anderson Nelson Date: Sun, 22 Dec 2024 18:08:55 -0500 Subject: [PATCH] Improve posting to Twitter and BlueSky platforms --- main.py | 10 ++++++---- nearquake/post_manager.py | 17 +++++++++++++++++ nearquake/utils/__init__.py | 3 --- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/main.py b/main.py index 621e218..35bb476 100644 --- a/main.py +++ b/main.py @@ -16,7 +16,7 @@ UploadEarthQuakeLocation, get_date_range_summary, ) -from nearquake.post_manager import post_to_all_platforms +from nearquake.post_manager import post_to_all_platforms, save_tweet_to_db from nearquake.open_ai_client import generate_response from nearquake.utils import convert_datetime, format_earthquake_alert from nearquake.utils.db_sessions import DbSessionManager @@ -100,6 +100,7 @@ message=message, ) post_to_all_platforms(tweet_text) + save_tweet_to_db(tweet_text, conn) if args.weekly: run.upload(url=generate_time_period_url("week")) @@ -124,6 +125,7 @@ message=message, ) post_to_all_platforms(tweet_text) + save_tweet_to_db(tweet_text, conn) if args.monthly: run.upload(url=generate_time_period_url("month")) @@ -148,6 +150,7 @@ message=message, ) post_to_all_platforms(tweet_text) + save_tweet_to_db(tweet_text, conn) if args.initialize: create_database(url=POSTGRES_CONNECTION_URL, schema=["earthquake", "tweet"]) @@ -156,11 +159,10 @@ prompt = random.choice(CHAT_PROMPT) message = generate_response(prompt=prompt) - tweet_text = format_earthquake_alert( - post_type="fact", message=message, prompt=prompt - ) + tweet_text = format_earthquake_alert(post_type="fact", message=message) post_to_all_platforms(tweet_text) + save_tweet_to_db(tweet_text, conn) if args.backfill: start_date = input("Type Start Date:") diff --git a/nearquake/post_manager.py b/nearquake/post_manager.py index 14c14a2..10b540c 100644 --- a/nearquake/post_manager.py +++ b/nearquake/post_manager.py @@ -2,6 +2,8 @@ from abc import ABC, abstractmethod import tweepy from atproto import Client + +from nearquake.app.db import Post from nearquake.config import TWITTER_AUTHENTICATION, BLUESKY_PASSWORD, BLUESKY_USER_NAME _logger = logging.getLogger(__name__) @@ -53,6 +55,21 @@ def post(self, post_text: str) -> bool: return False +def save_tweet_to_db(tweet_text: dict, conn) -> bool: + """ + Save the posted tweet data into the database. + :param tweet_data: The content of the tweet to be saved. + :param conn: Database connection object. + """ + try: + conn.insert(Post(**tweet_text)) + _logger.info(f"Tweet saved to database: {tweet_text}") + return True + except Exception as e: + _logger.error(f"Failed to save tweet to database {tweet_text}. Error: {e}") + return False + + def post_to_all_platforms(post_text: str) -> dict: platforms = [TwitterPost(), BlueSkyPost()] for platform in platforms: diff --git a/nearquake/utils/__init__.py b/nearquake/utils/__init__.py index 3ff67b2..fb6092b 100644 --- a/nearquake/utils/__init__.py +++ b/nearquake/utils/__init__.py @@ -287,7 +287,6 @@ def format_earthquake_alert( duration: timedelta = None, id_event: str = None, message: str = None, - prompt: str = None, ) -> dict: """ Formats an alert for an earthquake event or fact. @@ -297,7 +296,6 @@ def format_earthquake_alert( :param id_event: Unique identifier for the earthquake event. :param post_type: Type of post, either 'event' or 'fact'. :param message: Message content for fact-type posts. - :param prompt: Optional prompt for additional user interaction in fact-type posts. :return: A dictionary formatted as an alert or fact post. """ @@ -315,7 +313,6 @@ def format_earthquake_alert( "post": message, "ts_upload_utc": ts_upload_utc, "id_event": None, - "prompt": prompt, "post_type": post_type, } else: