summaryrefslogtreecommitdiff
path: root/news.py
diff options
context:
space:
mode:
authorJacob McDonnell <jacob@simplelittledream.com>2022-09-27 13:06:14 -0400
committerJacob McDonnell <jacob@simplelittledream.com>2022-09-27 13:06:14 -0400
commit380b7f432f93b82edd23e5c7921cb77c3479fbd3 (patch)
tree9989b2ad2ce58832862012101d17f8455c74cc10 /news.py
Initial Commit
Diffstat (limited to 'news.py')
-rw-r--r--news.py30
1 files changed, 30 insertions, 0 deletions
diff --git a/news.py b/news.py
new file mode 100644
index 0000000..28bb374
--- /dev/null
+++ b/news.py
@@ -0,0 +1,30 @@
+import requests
+import json
+from time import sleep
+from settings import news
+
+apiKey = "17d4d578091e47db9791e84d790391b5"
+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