blob: cdec011378da22ee0122c0f5e7e096c335d479a9 (
plain)
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
30
31
|
import requests
import json
from time import sleep
from settings import getSettings
news = getSettings("news")
apiKey = news["apikey"]
countryCode = news["country"]
url = f"https://newsapi.org/v2/top-headlines?country={countryCode}&apiKey={apiKey}"
def getHeadlines():
resp = requests.get(url)
while resp.status_code != 200:
resp = requests.get(url)
sleep(10)
respData = json.loads(resp.text)
articles = respData["articles"]
output = ["Top 5 Headlines in the US:"]
for i in range(5):
article = articles[i]
title = article["title"]
output.append(title)
output.append("\n")
return output
|