forked from leonkuperman/llm_demo_news
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnews_fetcher.py
29 lines (23 loc) · 1.25 KB
/
news_fetcher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# news_fetcher.py
import finnhub
from db_setup import get_db_connection, get_last_id
import asyncio
from logger_config import get_logger
logger = get_logger(__name__)
async def fetch_and_store_articles(finnhub_api_key):
finnhub_client = finnhub.Client(api_key=finnhub_api_key)
try:
last_id = get_last_id() # Get the latest ID from the database
new_articles = finnhub_client.general_news('general', min_id=last_id)
with get_db_connection() as conn:
for article in new_articles:
conn.execute('''INSERT OR IGNORE INTO articles
(finnhub_id, category, datetime, headline, image, related, source, summary, url)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)''',
(article['id'], article['category'], article['datetime'], article['headline'],
article['image'], article['related'], article['source'], article['summary'],
article['url']))
conn.commit() # Ensure all changes are saved
logger.info(f"Fetched and stored {len(new_articles)} new articles.")
except Exception as e:
logger.error(f"Error fetching or storing articles: {e}")