Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blue sky update #221

Merged
merged 5 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
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 @@ -99,7 +100,8 @@
post_type="fact",
message=message,
)
post_to_all_platforms(tweet_text)
post_to_all_platforms(tweet_text=tweet_text.get("post"))
save_tweet_to_db(tweet_text, conn)

if args.weekly:
run.upload(url=generate_time_period_url("week"))
Expand All @@ -123,7 +125,8 @@
post_type="fact",
message=message,
)
post_to_all_platforms(tweet_text)
post_to_all_platforms(tweet_text=tweet_text.get("post"))
save_tweet_to_db(tweet_text, conn)

if args.monthly:
run.upload(url=generate_time_period_url("month"))
Expand All @@ -147,7 +150,8 @@
post_type="fact",
message=message,
)
post_to_all_platforms(tweet_text)
post_to_all_platforms(tweet_text=tweet_text.get("post"))
save_tweet_to_db(tweet_text, conn)

if args.initialize:
create_database(url=POSTGRES_CONNECTION_URL, schema=["earthquake", "tweet"])
Expand All @@ -156,11 +160,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)
post_to_all_platforms(tweet_text=tweet_text.get("post"))
save_tweet_to_db(tweet_text, conn)

if args.backfill:
start_date = input("Type Start Date:")
Expand Down
19 changes: 18 additions & 1 deletion 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,7 +55,22 @@ 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:
platform.post(post_text.get("post"))
platform.post(post_text)
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
Loading