Skip to content

Commit

Permalink
Improve posting to Twitter and BlueSky platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
dachosen1 committed Dec 22, 2024
1 parent 628123e commit 7bae98f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
10 changes: 6 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"))
Expand All @@ -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"))
Expand All @@ -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"])
Expand All @@ -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:")
Expand Down
17 changes: 17 additions & 0 deletions nearquake/post_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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:
Expand Down
3 changes: 0 additions & 3 deletions nearquake/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
"""

Expand All @@ -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:
Expand Down

0 comments on commit 7bae98f

Please sign in to comment.