-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
136 lines (110 loc) · 4.44 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
import atexit
import os
from datetime import datetime
from flask import Flask, request, jsonify
from flask_cors import CORS, cross_origin
from cloudant_api.cloudant_api import CloudantApi
from filmyfy_libs.filmyfy_imdb import FilmyfyIMDB
from utils import authentication
app = Flask(__name__, static_url_path='')
CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
# On IBM Cloud Cloud Foundry, get the port number from the environment variable PORT
# When running this app on the local machine, default the port to 8000
port = int(os.getenv('PORT', 8000))
cloudantApi = None
imdbApi = None
# API documentation: https://filmify.docs.apiary.io
# register new user
@app.route('/api/user/register/', methods=['POST'])
def register_user():
nickname = request.json['username']
password_hash = authentication.hash_password(request.json['password'])
return cloudantApi.create_user(nickname, password_hash)
# login user
@app.route('/api/user/login/', methods=['POST', 'OPTIONS'])
@cross_origin()
def login_user():
nickname = request.json['username']
entered_password = request.json['password']
user = cloudantApi.get_user(nickname)
if not user:
return ""
stored_password = user['password']
if authentication.verify_password(stored_password, entered_password):
response = jsonify(nickname)
return response
else:
return ""
# delete user account
@app.route('/api/user/<username>/', methods=['DELETE'])
def remove_user(username):
return jsonify(cloudantApi.delete_user(username))
# find a movie by text string
@app.route('/api/movie/find/<text>/', methods=['GET'])
def find_movies(text):
movies = imdbApi.find_movie(text)
return jsonify(movies)
# find a movie by id
@app.route('/api/movie/<movie_id>/', methods=['GET'])
def get_movie(movie_id):
return jsonify(imdbApi.get_movie_metadata(movie_id))
# find similar movies to the one identified by movie_id
@app.route('/api/movie/similar/<movie_id>/', methods=['GET'])
def list_similar_movies(movie_id):
return jsonify(imdbApi.find_similar_movies(movie_id))
# recommend movies according to user profile history
@app.route('/api/user/<username>/recommend/', methods=['GET'])
def recommend_by_profile(username):
fav_ids = cloudantApi.get_user_favourite_movies(username)
seen_ids = cloudantApi.get_user_seen_movies(username)
return jsonify(imdbApi.find_similar_movie_by_favourite(fav_ids, seen_ids))
# get user's favourite movies
@app.route('/api/user/<username>/favourite/', methods=['GET'])
def get_favourite_movies(username):
ids = cloudantApi.get_user_favourite_movies(username)
movies = []
for id in ids:
movie_data = imdbApi.get_movie_metadata(id)
if movie_data:
movies.append(movie_data)
return jsonify(movies)
# get user's favourite movies ids
@app.route('/api/user/<username>/favourite_ids/', methods=['GET'])
def get_favourite_ids(username):
return jsonify(cloudantApi.get_user_favourite_movies(username))
# add movie/actor/director/category to favourites
@app.route('/api/user/<username>/favourite/<movie_id>/', methods=['POST', 'OPTIONS'])
@cross_origin()
def add_to_favourites(username, movie_id):
cloudantApi.add_seen_movie(username, movie_id)
return jsonify(cloudantApi.add_favourite_movie(username, movie_id))
# remove movie/actor/director/category from favourites
@app.route('/api/user/<username>/favourite/<movie_id>/', methods=['DELETE'])
def remove_from_favourites(username, movie_id):
return jsonify(cloudantApi.remove_favourite_movie(username, movie_id))
# get user's seen movies
@app.route('/api/user/<username>/seen/', methods=['GET'])
def get_seen_movies(username):
ids = cloudantApi.get_user_seen_movies(username)
movies = []
for id in ids:
movie_data = imdbApi.get_movie_metadata(id)
if movie_data:
movies.append(movie_data)
return jsonify(movies)
# get user's seen movies ids
@app.route('/api/user/<username>/seen_ids/', methods=['GET'])
def get_seen_ids(username):
return jsonify(cloudantApi.get_user_seen_movies(username))
# add movie/actor/director/category to seen
@app.route('/api/user/<username>/seen/<movie_id>/', methods=['POST'])
def add_to_seen(username, movie_id):
return jsonify(cloudantApi.add_seen_movie(username, movie_id))
@atexit.register
def on_exit():
cloudantApi.disconnect()
if __name__ == '__main__':
cloudantApi = CloudantApi()
imdbApi = FilmyfyIMDB()
app.run(host='0.0.0.0', port=port, debug=True)