-
Notifications
You must be signed in to change notification settings - Fork 0
/
api-server.py
90 lines (72 loc) · 2.64 KB
/
api-server.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
'''
Server Script to initialize personal IMDb_API .
The API can be accessed using GET or POST methods :
1. For POST Method use endpoint /imdb-api-post
2. For GET Method use endpoint /imdb-api-get
'''
from IMDb_API import SearchApi
from flask import Flask,render_template,request,jsonify
from flask_cors import CORS
# CONFIG
app = Flask(__name__)
CORS(app)
# ROUTES
@app.route('/')
@app.route('/index')
def index():
return render_template('index.html')
@app.route('/test-api')
def api_test():
return render_template('api_test.html')
# GET METHOD ENDPOINT
@app.route('/imdb-api-get',methods = ['GET'])
def imdb_api_get():
# Building API Params
try:
title=request.args.get('title','')
title_type=request.args.get('title_type','')
release_date=request.args.get('release_date','')
user_rating=request.args.get('user_rating','')
genres=request.args.get('genres','')
colors=request.args.get('colors','')
keywords=request.args.get('keywords','')
plot=request.args.get('plot','')
adult=request.args.get('adult','')
count=request.args.get('count','')
if title=='' and keywords=='' and plot=='':
return jsonify({"error":"Title/Keyword/Plot is required"}),400
# Generating Response
response = jsonify(SearchApi(title,title_type,release_date,user_rating,genres,colors,keywords,plot,adult,count))
if response != 'ERROR':
return response
return jsonify({"error":"SERVER ERROR"}),500
except Exception as e:
print("GET-ERROR:",e)
return jsonify({"error":"Malformed Request"}),400
# POST METHOD ENDPOINT
@app.route('/imdb-api-post',methods = ['POST'])
def imdb_api_post():
# Building API Params
try:
title=request.get_json().get('title','')
title_type=request.get_json().get('title_type','')
release_date=request.get_json().get('release_date','')
user_rating=request.get_json().get('user_rating','')
genres=request.get_json().get('genres','')
colors=request.get_json().get('colors','')
keywords=request.get_json().get('keywords','')
plot=request.get_json().get('plot','')
adult=request.get_json().get('adult','')
count=request.get_json().get('count','')
if title=='' and keywords=='' and plot=='':
return jsonify({"error":"Title/Keyword/Plot is required"}),400
# Generating Response
response = jsonify(SearchApi(title,title_type,release_date,user_rating,genres,colors,keywords,plot,adult,count))
if response != 'ERROR':
return response
return jsonify({"error":"SERVER ERROR"}),500
except Exception as e:
print("POST-ERROR:",e)
return jsonify({"error":"Malformed Request"}),400
if __name__ == '__main__':
app.run()