-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
executable file
·144 lines (111 loc) · 5.11 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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import datetime
import logging
from flask import Flask, jsonify, request
# from DBOperation.db import select_query, add_record, update_rating, update_genre
from DBOperation import logs
from DBOperation.db_sqlite3 import select_query, add_record, update_rating, update_genre
from OMDb.api import OMDB
logs.initialize_logger("flask-imdb-api")
app = Flask(__name__)
api_key = "API_KEY"
def isfloat(value):
try:
float(value)
return True
except ValueError:
return False
@app.route('/')
def index():
logging.debug("index")
return jsonify({'message': 'Welcome To Movie Data API'})
# endpoint to update
@app.route("/movie/<id>", methods=["PATCH"])
def update_data(id):
key = request.form['key']
value = request.form['value']
db_result = {'data': select_query("SELECT * FROM movie_details WHERE id='{0}' limit 1".format(id))}
if not db_result:
return jsonify({'message': 'No data found!'}), 404
if key != "rating" and key != "genre" or value == "":
return jsonify({'message': 'Bad Input!'}), 400
if key == "rating":
if not isfloat(value):
return jsonify({'message': 'Bad Input!'}), 400
update_rating(id, value)
if key == "genre":
update_genre(id, value)
db_result = {'data': select_query("SELECT * FROM movie_details WHERE id='{0}' limit 1".format(id))}
genre_result = select_query("SELECT genre FROM movie_genres WHERE id='{0}'".format(id))
db_result['data'][0]['genres'] = list(genre['genre'] for genre in genre_result)
db_result['time'] = datetime.datetime.now()
return jsonify(db_result)
# endpoint to get detail by title
@app.route("/movie/title", methods=["GET"])
def movie_detail_by_title():
title = request.args['title']
db_result = {'data': select_query("SELECT * FROM movie_details WHERE title LIKE'{0}' limit 1".format(title))}
if not db_result['data']:
obj = OMDB(apikey=api_key)
obj.get(title=title)
add_record(obj)
db_result = {'data': select_query("SELECT * FROM movie_details WHERE title LIKE'{0}' limit 1".format(title))}
if not db_result:
return jsonify({'message': 'No data found!'}), 404
print(db_result)
genre_result = select_query("SELECT genre FROM movie_genres WHERE id='{0}'".format(db_result['data'][0]['id']))
print(genre_result)
db_result['data'][0]['genres'] = list(genre['genre'] for genre in genre_result)
db_result['time'] = datetime.datetime.now()
return jsonify(db_result)
# endpoint to get detail by id
@app.route("/movie/<id>", methods=["GET"])
def movie_detail_by_id(id):
db_result = {'data': select_query("SELECT * FROM movie_details WHERE id='{0}' limit 1".format(id))}
if not db_result['data']:
return jsonify({'message': 'No data found in local!'}), 404
genre_result = select_query("SELECT genre FROM movie_genres WHERE id='{0}'".format(id))
db_result['data'][0]['genres'] = list(genre['genre'] for genre in genre_result)
db_result['time'] = datetime.datetime.now()
return jsonify(db_result)
# endpoint to get movies by filter
@app.route("/movie/filter/", methods=["GET"])
def movies_by_filter():
logging.debug("filter")
action = request.args['action']
if action == "year":
year = request.args['value']
db_result = {'data': select_query("SELECT id, title, released_year FROM movie_details WHERE released_year={0}"
.format(year))}
if not db_result:
return jsonify({'message': 'No data found in local!'}), 404
elif action == "year_range":
start_year = request.args['start_year']
end_year = request.args['end_year']
db_result = {'data': select_query("SELECT id, title, released_year FROM movie_details WHERE released_year "
"BETWEEN {0} AND {1}".format(start_year, end_year))}
if not db_result:
return jsonify({'message': 'No data found in local!'}), 404
elif action == "higher_rating":
value = request.args['value']
db_result = {'data': select_query("SELECT id, title, rating FROM movie_details WHERE rating > {0}"
.format(value))}
if not db_result:
return jsonify({'message': 'No data found in local!'}), 404
elif action == "lower_rating":
value = request.args['value']
db_result = {'data': select_query("SELECT id, title, rating FROM movie_details WHERE rating < {0}"
.format(value))}
if not db_result:
return jsonify({'message': 'No data found in local!'}), 404
elif action == "genre":
value = request.args['value']
db_result = {'data': select_query("SELECT id FROM movie_genres WHERE genre = '{0}'".format(value))}
if not db_result:
return jsonify({'message': 'No data found in local!'}), 404
else:
db_result = {'message': 'No filter selected!'}
db_result['time'] = datetime.datetime.now()
return jsonify(db_result)
if __name__ == '__main__':
logging.debug("App starts")
app.run(host='0.0.0.0')