Skip to content

Commit

Permalink
Overhaul of coding standards
Browse files Browse the repository at this point in the history
  • Loading branch information
adipai committed Oct 12, 2023
1 parent e8d5e3b commit 947bbfa
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 58 deletions.
13 changes: 9 additions & 4 deletions Code/recommenderapp/app.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
"""
Module for routing all calls from the frontend
"""

import json
import sys
from search import Search
from flask import Flask, jsonify, render_template, request
from flask_cors import CORS
from utils import send_email_to_user, beautify_feedback_data

sys.path.append("../../")
from Code.prediction_scripts.item_based import recommend_for_new_user
from search import Search

app = Flask(__name__)
app.secret_key = "secret key"
Expand Down Expand Up @@ -45,8 +50,8 @@ def search():
Handles movie search requests.
"""
term = request.form["q"]
search = Search()
filtered_dict = search.resultsTop10(term)
finder = Search()
filtered_dict = finder.results_top_ten(term)
resp = jsonify(filtered_dict)
resp.status_code = 200
return resp
Expand All @@ -58,7 +63,7 @@ def feedback():
Handles user feedback submission and mails the results.
"""
data = json.loads(request.data)
user_email = "ananyamantravadi@gmail.com"
user_email = "adipai16@gmail.com"
send_email_to_user(user_email, beautify_feedback_data(data))
return data

Expand Down
31 changes: 31 additions & 0 deletions Code/recommenderapp/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""
This module contains all the constants
"""

EMAIL_HTML_CONTENT = """
<html>
<head></head>
<body>
<h1 style="color: #333333;">Movie Recommendations from PopcornPicks</h1>
<p style="color: #555555;">Dear Movie Enthusiast,</p>
<p style="color: #555555;">We hope you're having a fantastic day!</p>
<div style="padding: 10px; border: 1px solid #cccccc; border-radius: 5px; background-color: #f9f9f9;">
<h2>Your Movie Recommendations:</h2>
<h3>Movies Liked:</h3>
<ul style="color: #555555;">
{}
</ul>
<h3>Movies Disliked:</h3>
<ul style="color: #555555;">
{}
</ul>
<h3>Movies Yet to Watch:</h3>
<ul style="color: #555555;">
{}
</ul>
</div>
<p style="color: #555555;">Enjoy your movie time with PopcornPicks!</p>
<p style="color: #555555;">Best regards,<br>PopcornPicks Team 🍿</p>
</body>
</html>
"""
49 changes: 33 additions & 16 deletions Code/recommenderapp/search.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
import pandas as pd
"""
Search feature for the web application
"""

# from app import app
from flask import jsonify, request, render_template
import sys
import os
import pandas as pd
#from flask import jsonify, request, render_template


app_dir = os.path.dirname(os.path.abspath(__file__))
code_dir = os.path.dirname(app_dir)
project_dir = os.path.dirname(code_dir)


class Search:
"""
Search feature for landing page
"""

df = pd.read_csv(project_dir + "/data/movies.csv")

def __init__(self):
pass

def startsWith(self, word):
def starts_with(self, word):
"""
Function to check movie prefix
"""
n = len(word)
res = []
word = word.lower()
Expand All @@ -27,26 +35,35 @@ def startsWith(self, word):
res.append(x)
return res

def anywhere(self, word, visitedWords):
def anywhere(self, word, visited_words):
"""
Function to check visited words
"""
res = []
word = word.lower()
for x in self.df["title"]:
if x not in visitedWords:
if x not in visited_words:
curr = x.lower()
if word in curr:
res.append(x)
return res

def results(self, word):
startsWith = self.startsWith(word)
visitedWords = set()
for x in startsWith:
visitedWords.add(x)
anywhere = self.anywhere(word, visitedWords)
startsWith.extend(anywhere)
return startsWith

def resultsTop10(self, word):
"""
Function to serve the result render
"""
starts_with = self.starts_with(word)
visited_words = set()
for x in starts_with:
visited_words.add(x)
anywhere = self.anywhere(word, visited_words)
starts_with.extend(anywhere)
return starts_with

def results_top_ten(self, word):
"""
Function to get top 10 results
"""
return self.results(word)[:10]


Expand Down
55 changes: 17 additions & 38 deletions Code/recommenderapp/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
""" Module contains utility functions used for various purposes in the backend """

import logging
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import logging

import constants as c


def beautify_feedback_data(data):
"""
Expand All @@ -22,12 +27,11 @@ def beautify_feedback_data(data):
dislike.append(movie)

# Create a category-dictionary of liked, disliked and yet to watch movies
categorized_data_dict = {"Liked":like, "Disliked":dislike, "Yet to Watch":yet_to_watch}
categorized_data_dict = {"Liked": like,
"Disliked": dislike, "Yet to Watch": yet_to_watch}

return categorized_data_dict



def send_email_to_user(recipient_email, categorized_data):
"""
Utility function to send movie recommendations to user over email
Expand All @@ -36,11 +40,11 @@ def send_email_to_user(recipient_email, categorized_data):
# Email configuration
smtp_server = 'smtp.gmail.com'
# Port for TLS
smtp_port = 587
smtp_port = 587
sender_email = '[email protected]'

# Use an app password since 2-factor authentication is enabled
sender_password = 'uxnd shis sazo mstj'
sender_password = 'uxnd shis sazo mstj'
subject = 'Your movie recommendation from PopcornPicks'

# Create the email message
Expand All @@ -50,52 +54,27 @@ def send_email_to_user(recipient_email, categorized_data):
message['Subject'] = subject

# Create the email message with HTML content
html_content = """
<html>
<head></head>
<body>
<h1 style="color: #333333;">Movie Recommendations from PopcornPicks</h1>
<p style="color: #555555;">Dear Movie Enthusiast,</p>
<p style="color: #555555;">We hope you're having a fantastic day!</p>
<div style="padding: 10px; border: 1px solid #cccccc; border-radius: 5px; background-color: #f9f9f9;">
<h2>Your Movie Recommendations:</h2>
<h3>Movies Liked:</h3>
<ul style="color: #555555;">
{}
</ul>
<h3>Movies Disliked:</h3>
<ul style="color: #555555;">
{}
</ul>
<h3>Movies Yet to Watch:</h3>
<ul style="color: #555555;">
{}
</ul>
</div>
<p style="color: #555555;">Enjoy your movie time with PopcornPicks!</p>
<p style="color: #555555;">Best regards,<br>PopcornPicks Team 🍿</p>
</body>
</html>
""".format('\n'.join(f'<li>{movie}</li>' for movie in categorized_data['Liked']),
'\n'.join(f'<li>{movie}</li>' for movie in categorized_data['Disliked']),
'\n'.join(f'<li>{movie}</li>' for movie in categorized_data['Yet to Watch']))
html_content = c.EMAIL_HTML_CONTENT.format('\n'.join(f'<li>{movie}</li>' for movie in categorized_data['Liked']),
'\n'.join(
f'<li>{movie}</li>' for movie in categorized_data['Disliked']),
'\n'.join(f'<li>{movie}</li>' for movie in categorized_data['Yet to Watch']))

# Attach the HTML email body
message.attach(MIMEText(html_content, 'html'))

# Connect to the SMTP server
try:
server = smtplib.SMTP(smtp_server, smtp_port)
# Start TLS encryption
server.starttls()
server.starttls()
server.login(sender_email, sender_password)

# Send the email
server.sendmail(sender_email, recipient_email, message.as_string())
logging.info("Email sent successfully!")

except Exception as e:
logging.warning(f'Email could not be sent. Error: {str(e)}')
logging.warning("Email could not be sent. Error: %s", str(e))

finally:
server.quit()

0 comments on commit 947bbfa

Please sign in to comment.