91 lines
2.7 KiB
Python
91 lines
2.7 KiB
Python
import os
|
|
import logging
|
|
import time
|
|
import sys
|
|
from dotenv import load_dotenv
|
|
from utils.econews import (
|
|
test_db_connection,
|
|
ensure_table_exists,
|
|
fetch_general_news,
|
|
fetch_fmp_articles
|
|
)
|
|
|
|
# Load environment variables
|
|
load_dotenv()
|
|
|
|
# Logging configuration
|
|
logging.basicConfig(
|
|
level=logging.INFO,
|
|
format='%(asctime)s - %(levelname)s - %(message)s'
|
|
)
|
|
|
|
def update_news():
|
|
"""Main function to update economic news from all sources"""
|
|
try:
|
|
# Test database connection
|
|
test_db_connection()
|
|
|
|
# Ensure table exists with correct schema
|
|
ensure_table_exists()
|
|
|
|
# Initialize success flags
|
|
general_news_success = False
|
|
fmp_articles_success = False
|
|
|
|
# Fetch general news (multiple pages)
|
|
try:
|
|
page = 0
|
|
while fetch_general_news(page):
|
|
page += 1
|
|
if page >= 10: # Limit to 10 pages to avoid excessive API calls
|
|
break
|
|
time.sleep(1) # Respect API rate limits
|
|
general_news_success = True
|
|
except Exception as e:
|
|
logging.error(f"Error fetching general news: {str(e)}")
|
|
|
|
# Fetch FMP articles
|
|
try:
|
|
if fetch_fmp_articles(0, 100): # Get latest 100 articles
|
|
fmp_articles_success = True
|
|
except Exception as e:
|
|
logging.error(f"Error fetching FMP articles: {str(e)}")
|
|
|
|
# Log overall status
|
|
if general_news_success or fmp_articles_success:
|
|
logging.info("Economic news update completed with some successes.")
|
|
if not general_news_success:
|
|
logging.warning("General news update failed.")
|
|
if not fmp_articles_success:
|
|
logging.warning("FMP articles update failed.")
|
|
else:
|
|
raise Exception("Both general news and FMP articles updates failed.")
|
|
|
|
except Exception as e:
|
|
logging.error(f"Error in update_news: {str(e)}")
|
|
raise
|
|
|
|
def main():
|
|
"""Main entry point with robust error handling"""
|
|
max_retries = 3
|
|
retry_delay = 60 # seconds
|
|
|
|
for attempt in range(max_retries):
|
|
try:
|
|
update_news()
|
|
return 0 # Success
|
|
except KeyboardInterrupt:
|
|
logging.info("Script interrupted by user")
|
|
return 1
|
|
except Exception as e:
|
|
logging.error(f"Attempt {attempt + 1} failed: {str(e)}")
|
|
if attempt < max_retries - 1:
|
|
logging.info(f"Retrying in {retry_delay} seconds...")
|
|
time.sleep(retry_delay)
|
|
else:
|
|
logging.error("All retry attempts failed")
|
|
return 1
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|