Skip to content

Commit

Permalink
added newsapi function
Browse files Browse the repository at this point in the history
  • Loading branch information
bdelucia committed Oct 6, 2024
1 parent 853f7d3 commit 8ea18d8
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,35 @@
openai_api_key = os.getenv("OPENAI_API_KEY")
openai.api_key = openai_api_key
openweather_api_key = os.getenv("OPENWEATHER_API_KEY")
news_api_key = os.getenv("NEWS_API_KEY")

def get_weather_data(city):
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={openweather_api_key}&units=imperial"
response = requests.get(url)
return response.json()

def get_news():
url = (
'https://newsapi.org/v2/top-headlines?'
'country=us&'
'category=technology&'
f'apiKey={news_api_key}'
)
response = requests.get(url)
news_data = response.json()

# Extract the articles
articles = []
if news_data["status"] == "ok":
for article in news_data["articles"]:
articles.append({
"title": article["title"],
"description": article["description"],
"url": article["url"]
})

return articles


def get_random_fun_fact_prompt():
prompts = [
Expand Down Expand Up @@ -51,4 +74,5 @@ def home():
weather_data = get_weather_data("85308")
rounded_temp = math.ceil(weather_data["main"]["temp"]) # Rounds temperature up to whole number for better presentation
prompt = get_random_fun_fact_prompt()
return render_template("index.html", funFact = chat_gpt(prompt), weather_data=weather_data, rounded_temp=rounded_temp)
news_articles = get_news()
return render_template("index.html", funFact = chat_gpt(prompt), weather_data=weather_data, rounded_temp=rounded_temp, news_articles=news_articles)

0 comments on commit 8ea18d8

Please sign in to comment.