diff --git a/db/database.py b/db/database.py index 72775a2..ea2b5a6 100644 --- a/db/database.py +++ b/db/database.py @@ -21,6 +21,10 @@ class Database: if cursor.description: return cursor.fetchall() return [] + + def execute_many(self, query: str, params_list: list[tuple]): + with self.connection.cursor(cursor_factory=RealDictCursor) as cursor: + cursor.executemany(query, params_list) def close(self): self.connection.close() \ No newline at end of file diff --git a/server/app.py b/server/app.py index f775559..e2134b2 100644 --- a/server/app.py +++ b/server/app.py @@ -7,11 +7,17 @@ db = Database(db_name='ethnograph', user='ethnograph_user', password='ethnograph reddit_connector = RedditConnector() -@app.route('/get_reddit_posts//', methods=['GET']) +@app.route('/fetch_reddit//', methods=['GET']) def index(search, limit = 10): posts = reddit_connector.search_posts(search, limit=limit) - return {"posts": [post.__dict__ for post in posts]} + db.execute_many( + """INSERT INTO ethnograph.posts (title, content, author_username, created_utc) + VALUES (%s, %s, %s, to_timestamp(%s));""", + [(post.title, post.content, post.author, post.timestamp) for post in posts] + ) + + return {"status": "success", "inserted_posts": len(posts)} if __name__ == "__main__": app.run(debug=True) \ No newline at end of file