-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetchImage.py
51 lines (39 loc) · 1.3 KB
/
fetchImage.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import requests
import streamlit as st
def fetchImage(query):
#pexels api key
apiKey = ""
#pexels url
url = "https://api.pexels.com/v1/search" #search endpoint as we search for an image based on a query
#req/resp header to pass info
headers = {
'Authorization' : apiKey #spelling should be "authorization"
}
params = {
'query': query,
'per_page': 1
}
response = requests.get(url, headers=headers, params=params)
if response.status_code == 200:
#request successful
data = response.json()
images = data.get('photos', []) #'photos' not 'images'
# print("images: ", images)
if images:
#only need the image url to fetch it
imageUrl = images[0]['src']['original']
print("imageUrl: ", imageUrl)
return imageUrl
else:
# print("No images found!")
st.write("No images found for the given topic")
else:
# print("error: ", {response.status_code}, {response.text})
st.write(f"error: {response.status_code}, {response.text}")
return None
# #test images
# query = "Airplane"
# #imageUrl for image on Pexels
# imageUrl = fetchImage(query)
# if imageUrl:
# print(f"image url for '{query}' : {imageUrl}")