Skip to content
Working with APIs in Python
import osAPI_KEY_METEOSOURCE = os.environ['API_KEY_METEOSOURCE']
API_KEY_NEWSAPI = os.environ['API_KEY_NEWSAPI']
API_KEY_OPENAI = os.environ['API_KEY_OPENAI']import requestsnewsapi_url_parameters = {
'apiKey': API_KEY_NEWSAPI,
'country': 'US'
}import requests
response = requests.get('https://newsapi.org/v2/top-headlines',
params=newsapi_url_parameters)
#response status_code
if response.status_code == 200:
response_data = response.json()
headlines_articles = [
{'title': article['title'], 'description': article['description']}
for article in response_data['articles']
]
print(headlines_articles)
for article in response_data['articles']:
if article['title'] != '[Removed]':
print(article['title'])
else:
print(response.text)
raise SystemExit('Something went wrong!')Weather API setup
meteosource_headers = {
'X-API-Key': API_KEY_METEOSOURCE
}meteosource_findplaces_url_parameters = {
'text': 'Berlin'
}response = requests.get('https://www.meteosource.com/api/v1/free/find_places',
params=meteosource_findplaces_url_parameters,
headers = meteosource_headers)if response.status_code == 200:
response_data = response.json()
place_id = response_data[0]['place_id']
print(place_id)
else:
print(f"Error: {response.status_code} - {response.text}") # Updated to print the error details
raise SystemExit('Something went wrong, please inspect the response')#create a dictionary for the url parameters
meteosource_point_url_parameters = {
'place_id': place_id,
'sections': 'daily'
}response = requests.get('https://www.meteosource.com/api/v1/free/point',
headers = meteosource_headers,
params=meteosource_point_url_parameters
)if response.status_code == 200:
response_data = response.json()
weather_forecast = response_data['daily']['data'][0]['summary']
print(weather_forecast)
else:
print(f"Error: {response.status_code} - {response.text}") # Updated to print the error details
raise SystemExit('Something went wrong, please inspect the response')Create System Message variable
system_message = '''
You are an AI assistant tasked with generating a 'Morning Update' text that’s engaging and enjoyable for the user to listen to while having their morning coffee. The update should be about 2-5 minutes long, incorporating both a weather forecast and top 10 news headlines in a way that feels conversational, lively, and fits a specific tone (such as funny, serious, sarcastic, or motivational). Do not output anything else than the text, don't include any markup, lists, or other structural elements. The text will be sent to a text-to-speech API to generate an MP3, so make sure the output contains nothing that should not be read out loud.
Structure the monologue as follows:
1. Greeting: Start with a warm and welcoming greeting.
2. Weather Summary: Describe the day’s weather, infusing the chosen tone (e.g., funny, serious, etc.) to make it engaging.
3. News Headlines: Present each headline in the chosen tone, followed by a summary of the headline to give the listener a deeper insight into the headline.
4. Closing: Wrap up with a concluding remark that leaves the reader with a smile, positive thought, or playful nudge.
Be creative in how you incorporate the tone and style, ensuring that the text is engaging and enjoyable to listen to.
'''