added small error handling

This commit is contained in:
Linus E
2022-04-17 10:49:42 +02:00
parent 1095c63788
commit acdd46780c
2 changed files with 44 additions and 3 deletions

View File

@@ -20,9 +20,13 @@ load_dotenv()
# Init
api_key = os.getenv('NEWS_API_KEY')
newsapi = NewsApiClient(api_key=api_key)
source_json = requests.get(f"https://newsapi.org/v2/top-headlines/sources?apiKey={api_key}&language=en").json()
sources = source_json["sources"]
str_sources = ",".join([source["id"] for source in sources])
try:
source_json = requests.get(f"https://newsapi.org/v2/top-headlines/sources?apiKey={api_key}&language=en").json()
sources = source_json["sources"]
str_sources = ",".join([source["id"] for source in sources])
except KeyError:
print("Error: Could not get sources")
sys.exit(1)
def get_all_news_by_keyword(keyword, from_date="2000-01-01"):