forked from exit-zero-academy/NetflixMovieCatalog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
71 lines (48 loc) · 1.77 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
from flask import Flask, request, jsonify
import json
import random
app = Flask(__name__)
with open('data/data_tv.json', 'r') as f:
data_tv = json.load(f)
with open('data/data_movies.json', 'r') as f:
data_movies = json.load(f)
@app.route("/", methods=['GET'])
def home():
return "Hiiouuuu Sujal Don, this works! This app is an API, there is no UI ;-)"
@app.route('/discover')
def get_discover():
"""
Find movies using over filters and sort options.
"""
type_ = request.args.get('type')
data = data_tv if type_ == 'tv' else data_movies
genre_id = request.args.get('genre')
if not genre_id:
results = random.sample(list(data.values()), 20)
else:
results = []
for item in data.values():
if int(genre_id) in item['genre_ids']:
results.append(item)
if len(results) >= 20:
break
return jsonify(results)
@app.route('/updatePopularity', methods=['POST'])
def update_popularity():
movie_id = request.json.get('movieId')
new_popularity = request.json.get('popularity')
if movie_id not in data_movies:
return jsonify({'error': 'Movie Id value not provided or not found'}), 400
if new_popularity is None:
return jsonify({'error': 'Popularity value not provided'}), 400
try:
new_popularity = float(new_popularity)
except ValueError:
return jsonify({'error': 'Popularity value must be a float'}), 400
data_movies[movie_id]['popularity'] = new_popularity
return jsonify({'message': 'Popularity updated successfully', 'apki new popularity ye hai ': new_popularity}), 200
@app.route('/status')
def status():
return 'OK'
if __name__ == '__main__':
app.run(port=8080, host='0.0.0.0')