Files
napotd/main.py
deltreey 6e85a8fa09
All checks were successful
Build napotd / Build-And-Push (push) Successful in 23s
use urljoin instead for the image url
2024-03-14 20:03:40 -05:00

27 lines
668 B
Python

from urllib.parse import urljoin
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 urljoin(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)