Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Email notifier genre #18

Merged
merged 6 commits into from
Oct 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 63 additions & 4 deletions src/recommenderapp/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,45 @@
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

import pandas as pd
import constants as c

def create_colored_tags(genres):
"""
Utitilty function to create colored tags for different
movie genres
"""
# Define colors for specific genres
genre_colors = {
'Musical': '#FF1493', # DeepPink
'Sci-Fi': '#00CED1', # DarkTurquoise
'Mystery': '#8A2BE2', # BlueViolet
'Thriller': '#FF4500', # OrangeRed
'Horror': '#FF0000', # Red
'Documentary': '#228B22', # ForestGreen
'Fantasy': '#FF8C00', # DarkOrange
'Adventure': '#FFD700', # Gold
'Children': '#32CD32', # LimeGreen
'Film-Noir': '#000000', # Black
'Comedy': '#FFD700', # Gold
'Crime': '#8B0000', # DarkRed
'Drama': '#8B008B', # DarkMagenta
'Western': '#FF6347', # Tomato
'IMAX': '#7FFFD4', # Aquamarine
'Action': '#FF4500', # OrangeRed
'War': '#B22222', # FireBrick
'(no genres listed)': '#A9A9A9', # DarkGray
'Romance': '#FF69B4', # HotPink
'Animation': '#20B2AA' # LightSeaGreen
}
tags = []
for genre in genres:
color = genre_colors.get(genre, '#CCCCCC') # Default color if not found
tag = f'<span style="background-color: {color}; color: #FFFFFF; \
padding: 5px; border-radius: 5px;">{genre}</span>'
tags.append(tag)
return ' '.join(tags)

def beautify_feedback_data(data):
"""
Utility function to beautify the feedback json containing predicted movies for sending in email
Expand All @@ -33,6 +70,19 @@ def beautify_feedback_data(data):

return categorized_data_dict

def create_movie_genres(movie_genre_df):
"""
Utility function for creating a dictionary for movie-genres mapping
"""
# Create a dictionary to map movies to their genres
movie_to_genres = {}

# Iterating on all movies to create the map
for row in movie_genre_df.iterrows():
movie = row[1]['title']
genres = row[1]['genres'].split('|')
movie_to_genres[movie] = genres
return movie_to_genres
def send_email_to_user(recipient_email, categorized_data):
"""
Utility function to send movie recommendations to user over email
Expand All @@ -53,12 +103,21 @@ def send_email_to_user(recipient_email, categorized_data):
message['From'] = sender_email
message['To'] = recipient_email
message['Subject'] = subject

# Load the CSV file into a DataFrame
movie_genre_df = pd.read_csv('../../data/movies.csv')
# Creating movie-genres map
movie_to_genres = create_movie_genres(movie_genre_df)
# Create the email message with HTML content
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']))
'\n'.join(f'<li>{movie} \
{create_colored_tags(movie_to_genres.get(movie, ["Unknown Genre"]))}</li><br>' \
for movie in categorized_data['Liked']),
'\n'.join(f'<li>{movie} \
{create_colored_tags(movie_to_genres.get(movie, ["Unknown Genre"]))}</li><br>' \
for movie in categorized_data['Disliked']),
'\n'.join(f'<li>{movie} \
{create_colored_tags(movie_to_genres.get(movie, ["Unknown Genre"]))}</li><br>' \
for movie in categorized_data['Yet to Watch']))

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