From 947bbfabfb29bd258a63998fb1a34507b4695785 Mon Sep 17 00:00:00 2001 From: adipai Date: Thu, 12 Oct 2023 13:11:54 -0400 Subject: [PATCH] Overhaul of coding standards --- Code/recommenderapp/app.py | 13 +++++--- Code/recommenderapp/constants.py | 31 ++++++++++++++++++ Code/recommenderapp/search.py | 49 ++++++++++++++++++---------- Code/recommenderapp/utils.py | 55 ++++++++++---------------------- 4 files changed, 90 insertions(+), 58 deletions(-) create mode 100644 Code/recommenderapp/constants.py diff --git a/Code/recommenderapp/app.py b/Code/recommenderapp/app.py index ecf96e9af..dc94f4631 100644 --- a/Code/recommenderapp/app.py +++ b/Code/recommenderapp/app.py @@ -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" @@ -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 @@ -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 diff --git a/Code/recommenderapp/constants.py b/Code/recommenderapp/constants.py new file mode 100644 index 000000000..31a92b464 --- /dev/null +++ b/Code/recommenderapp/constants.py @@ -0,0 +1,31 @@ +""" +This module contains all the constants +""" + +EMAIL_HTML_CONTENT = """ + + + +

Movie Recommendations from PopcornPicks

+

Dear Movie Enthusiast,

+

We hope you're having a fantastic day!

+
+

Your Movie Recommendations:

+

Movies Liked:

+ +

Movies Disliked:

+ +

Movies Yet to Watch:

+ +
+

Enjoy your movie time with PopcornPicks!

+

Best regards,
PopcornPicks Team 🍿

+ + + """ diff --git a/Code/recommenderapp/search.py b/Code/recommenderapp/search.py index 776336306..dce873a2f 100644 --- a/Code/recommenderapp/search.py +++ b/Code/recommenderapp/search.py @@ -1,9 +1,11 @@ -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) @@ -11,13 +13,19 @@ 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() @@ -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] diff --git a/Code/recommenderapp/utils.py b/Code/recommenderapp/utils.py index 37aae3228..7858f4244 100644 --- a/Code/recommenderapp/utils.py +++ b/Code/recommenderapp/utils.py @@ -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): """ @@ -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 @@ -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 = 'popcornpicks504@gmail.com' # 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 @@ -50,44 +54,19 @@ def send_email_to_user(recipient_email, categorized_data): message['Subject'] = subject # Create the email message with HTML content - html_content = """ - - - -

Movie Recommendations from PopcornPicks

-

Dear Movie Enthusiast,

-

We hope you're having a fantastic day!

-
-

Your Movie Recommendations:

-

Movies Liked:

- -

Movies Disliked:

- -

Movies Yet to Watch:

- -
-

Enjoy your movie time with PopcornPicks!

-

Best regards,
PopcornPicks Team 🍿

- - - """.format('\n'.join(f'
  • {movie}
  • ' for movie in categorized_data['Liked']), - '\n'.join(f'
  • {movie}
  • ' for movie in categorized_data['Disliked']), - '\n'.join(f'
  • {movie}
  • ' for movie in categorized_data['Yet to Watch'])) + html_content = c.EMAIL_HTML_CONTENT.format('\n'.join(f'
  • {movie}
  • ' for movie in categorized_data['Liked']), + '\n'.join( + f'
  • {movie}
  • ' for movie in categorized_data['Disliked']), + '\n'.join(f'
  • {movie}
  • ' 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 @@ -95,7 +74,7 @@ def send_email_to_user(recipient_email, categorized_data): 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()