-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
39 lines (31 loc) · 847 Bytes
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
import requests
from dotenv import load_dotenv
import os
import json
def configure():
load_dotenv()
app = FastAPI()
origins = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=False,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/photos/{keyword}")
def get_photos(keyword: str):
configure()
url = f"https://api.unsplash.com/search/photos?page=1&query=" + keyword + "&client_id=" + os.getenv("api_key")
response = requests.get(url)
new = response.json()
new_results = new["results"]
photo_array = []
for item in new_results:
photo_array.append((item["urls"]["raw"]))
if not photo_array:
return {"message": "No photos found"}
else:
return {"results": photo_array }