From 82aff652507e4b6259b7c50dc5789b1cc10c45c6 Mon Sep 17 00:00:00 2001 From: Dhanush Reddy <29dhanushreddy@gmail.com> Date: Sun, 29 Dec 2024 19:44:46 +0000 Subject: [PATCH] Add database integration to store generated audio URLs --- main.py | 9 +++++---- upload.py | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/main.py b/main.py index 7151394..0a5cfa5 100644 --- a/main.py +++ b/main.py @@ -1,9 +1,10 @@ +import os from datetime import datetime, timedelta from generate_audio import generate_audio_from_text from llm import generate_reddit_news_summary from reddit import get_reddit_scraping_tasks -from upload import upload_file +from upload import add_to_db, upload_file subreddits = ["singularity", "LocalLLaMA", "homeautomation"] @@ -22,9 +23,7 @@ text = """======================================== Title: {title} Description: {description} - """.format( - title=title, description=description - ) + """.format(title=title, description=description) reddit_text += text print(text) print( @@ -50,4 +49,6 @@ upload_file(audio_path, "hackathon", audio_path) + add_to_db(f"{os.environ.get('AWS_ENDPOINT_URL_S3')}/hackathon/{audio_path}") + print(f"Reddit news summary for {subreddit} generated and uploaded.") diff --git a/upload.py b/upload.py index ec91bbe..d0f5ab6 100644 --- a/upload.py +++ b/upload.py @@ -2,6 +2,7 @@ import os import boto3 +import psycopg2 from botocore.exceptions import ClientError @@ -35,6 +36,17 @@ def upload_file(file_name, bucket, object_name=None, content_type="audio/mpeg"): return True +def add_to_db(url): + print(f"Adding {url} to the database") + conn = psycopg2.connect(os.environ.get("DATABASE_URL")) + cur = conn.cursor() + cur.execute("INSERT INTO summaries (url) VALUES (%s)", (url,)) + conn.commit() + cur.close() + conn.close() + print(f"{url} added to the database.") + + if __name__ == "__main__": file_name = "reddit_news_summary.mp3" bucket_name = "hackathon" @@ -46,3 +58,7 @@ def upload_file(file_name, bucket, object_name=None, content_type="audio/mpeg"): print(f"File URL: {file_url}") else: print("File upload failed.") + + url = "https://example.com" + add_to_db(url) + print("URL added to the database.")