二手产品经理

二手产品经理

THIS IS RENO

API - 94 days - Learn Python online for 100 days.

Record#

  1. Today I learned how to use two APIs. The first one is for getting news from the news source, and the second one is for summarizing text from OpenAI.
  2. The first step was relatively easy to write. After previous training, I am familiar with Flask and API, so writing it was simple.
  3. The second step encountered difficulties because it was not easy to obtain the key for OpenAI. I briefly looked into it, and it seems manageable, but I will not 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)


Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.