Introduction
Objective of this project was to create a backend application with a light framework, which receives a requested movie genre and serves only the movies of the corresponding genre.
Approach
The following elements were used in order to implement this project:
- Flask Framework
- CSV file: IMDB moview data was stored in the CSV file
- CSV parser: a csv parser function was developerd to read the csv file, and return the results to the flask application
How does it work
There are two options available for a user to search movies within this application:
- passing an argument searching for any genre, e.g. Action, Comedy
- searching the following predefined genres: Action, Adventure, Comedy, Drama, Romance
How to install and run
The following steps are required to run the application:
- Create a project directory: for example
mkdir my_imdb - Add project files: app.py and csv_parser.py
- Install Flask:
pip install Flask - To search any genre: GET on / This route will parse the GET parameter genre, to be able to filter by movie genre. Example on how to access this route:
curl "http://localhost:8080?genre=action" - To search a specific genre (e.g. action): GET on /action This route will only serve action movies. Example on how to access this route:
curl "http://localhost:8080/action" - To run the application:
python app.py
Program Files
csv_parser.py
import csv
def get_genre(genre):
file = "imdb-movie-data.csv"
result = list()
with open (file) as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
if genre in row["Genre"]:
result.append(row)
return result
app.py
# Import the Flask Class
from flask import Flask
from flask import request
import json
from csv_parser import get_genre
# Create an instance of Flask Class called name
app = Flask(__name__)
# Use the route() decorator to tell Flask what URL should trigger the index() function
@app.route('/')
# The function returns the data in JSON format
def index():
genre = request.args.get("genre")
data = genre.title()
result = get_genre(data)
return json.dumps(result)
@app.route('/action')
def action():
result = get_genre("Action")
return json.dumps(result)
@app.route('/adventure')
def adventure():
result = get_genre("Adventure")
return json.dumps(result)
@app.route('/comedy')
def comedy():
result = get_genre("Comedy")
return json.dumps(result)
@app.route('/drama')
def drama():
result = get_genre("Drama")
return json.dumps(result)
@app.route('/romance')
def romance():
result = get_genre("Romance")
return json.dumps(result)
app.run('0.0.0.0', port=8080) # default Flask port is 5000, 8080 is used to be accessed externally
