api redirect for nasa astronomy picture of the day, working with dockerfile

This commit is contained in:
2024-03-04 17:00:52 -06:00
commit 10bfdfbbeb
4 changed files with 295 additions and 0 deletions

24
main.py Normal file
View File

@@ -0,0 +1,24 @@
from bs4 import BeautifulSoup
from requests import get
from starlette.responses import RedirectResponse
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return RedirectResponse(main())
def main():
url = "https://apod.nasa.gov/apod/"
response = get(url)
html = response.text
soup = BeautifulSoup(html, features="html.parser")
img = soup.find('img')
image_url = img['src']
return url + image_url
# with open('img.png', 'wb') as out_file:
# response = get(url + image_url, stream=True)
# for chunk in response:
# out_file.write(chunk)