From 9e52420196454f75aebb4f74d853bc208f5d3763 Mon Sep 17 00:00:00 2001 From: Anderson Nelson Date: Sun, 22 Dec 2024 19:05:54 -0500 Subject: [PATCH] Blue sky update (#222) --- main.py | 15 +++++---------- nearquake/data_processor.py | 4 ++-- nearquake/post_manager.py | 21 +++++++++++++++++---- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/main.py b/main.py index 7465c32..42878c5 100644 --- a/main.py +++ b/main.py @@ -16,8 +16,7 @@ UploadEarthQuakeLocation, get_date_range_summary, ) - -from nearquake.post_manager import post_to_all_platforms, save_tweet_to_db +from nearquake.post_manager import post_and_save_tweet 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,8 +99,7 @@ post_type="fact", message=message, ) - post_to_all_platforms(tweet_text=tweet_text.get("post")) - save_tweet_to_db(tweet_text, conn) + post_and_save_tweet(tweet_text, conn) if args.weekly: run.upload(url=generate_time_period_url("week")) @@ -125,8 +123,7 @@ post_type="fact", message=message, ) - post_to_all_platforms(tweet_text=tweet_text.get("post")) - save_tweet_to_db(tweet_text, conn) + post_and_save_tweet(tweet_text, conn) if args.monthly: run.upload(url=generate_time_period_url("month")) @@ -150,8 +147,7 @@ post_type="fact", message=message, ) - post_to_all_platforms(tweet_text=tweet_text.get("post")) - save_tweet_to_db(tweet_text, conn) + post_and_save_tweet(tweet_text, conn) if args.initialize: create_database(url=POSTGRES_CONNECTION_URL, schema=["earthquake", "tweet"]) @@ -162,8 +158,7 @@ tweet_text = format_earthquake_alert(post_type="fact", message=message) - post_to_all_platforms(tweet_text=tweet_text.get("post")) - save_tweet_to_db(tweet_text, conn) + post_and_save_tweet(tweet_text, conn) if args.backfill: start_date = input("Type Start Date:") diff --git a/nearquake/data_processor.py b/nearquake/data_processor.py index 2883b32..08d9f67 100644 --- a/nearquake/data_processor.py +++ b/nearquake/data_processor.py @@ -16,7 +16,7 @@ generate_coordinate_lookup_detail_url, generate_time_range_url, ) -from nearquake.post_manager import post_to_all_platforms +from nearquake.post_manager import post_and_save_tweet from nearquake.utils import ( convert_timestamp_to_utc, fetch_json_data_from_url, @@ -319,7 +319,7 @@ def upload(self): ) try: - post_to_all_platforms(post_text=tweet_text) + post_and_save_tweet(tweet_text, self.conn) except Exception as e: _logger.error( diff --git a/nearquake/post_manager.py b/nearquake/post_manager.py index 9f45f3a..f198c83 100644 --- a/nearquake/post_manager.py +++ b/nearquake/post_manager.py @@ -70,7 +70,20 @@ def save_tweet_to_db(tweet_text: dict, conn) -> bool: return False -def post_to_all_platforms(post_text: str) -> dict: - platforms = [TwitterPost(), BlueSkyPost()] - for platform in platforms: - platform.post(post_text) +_PLATFORM = [TwitterPost(), BlueSkyPost()] + + +def post_to_all_platforms(text: str) -> dict: + for platform in _PLATFORM: + platform.post(text) + + +def post_and_save_tweet(text: dict, conn) -> None: + """ + Post tweet to all platforms and save to database + + :param tweet_text: Tweet content + :param conn: Database connection + """ + post_to_all_platforms(text=text.get("post")) + save_tweet_to_db(text, conn)