-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
35 lines (29 loc) · 1.06 KB
/
app.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
from flask import Flask, jsonify, request
from flask_cors import CORS
import requests
app = Flask(__name__)
CORS(app)
UNSPLASH_ACCESS_KEY = '' # Replace with your Unsplash API key
@app.route('/api/images', methods=['GET'])
def get_images():
query = request.args.get('query', '')
if not query:
return jsonify({"error": "Query parameter is required"}), 400
response = requests.get(
'https://api.unsplash.com/search/photos',
params={'query': query, 'client_id': UNSPLASH_ACCESS_KEY}
)
data = response.json()
images = []
if 'results' in data:
for result in data['results']:
images.append({
'id': result['id'],
'url': result['urls']['small'],
'description': result['description'] or result['alt_description'],
'user': result['user']['name'],
'profile_image': result['user']['profile_image']['small'],
})
return jsonify(images)
if __name__ == '__main__':
app.run(debug=True)