Record#
- Today I learned how to use two APIs, 1. Get news from news, 2. Summarize using openai.
- Step 1 was relatively easy to write. After previous training, I am familiar with flask and APIs, so it was simple to write overall.
- Step 2 encountered difficulties because it's not easy to get the openai key. I took a quick look and it doesn't seem difficult, but I won't write it for now.
CODE#
main.py#
import requests, json, os
from flask import Flask, request
newskey = os.environ['newskey']
country = "us"
url = f"https://newsapi.org/v2/top-headlines?country={country}&apiKey={newskey}"
app = Flask(__name__)
@app.route("/")
def index():
result = requests.get(url)
data = result.json()
text = ""
#print(json.dumps(data, indent=2))
for articles in data["articles"]:
text1 = f"""
<p>{articles["title"]}</p>
<p>{articles["author"]}</p>
<p>{articles["description"]}</p>
<hr />
"""
text += text1
f = open("index.html", "r")
page = f.read()
f.close()
page = page.replace("{newslist}", text)
return page
app.run(host='0.0.0.0', port=81)