-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappimdb3.py
More file actions
106 lines (82 loc) · 4.34 KB
/
appimdb3.py
File metadata and controls
106 lines (82 loc) · 4.34 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
from flask import Flask, render_template
from datetime import datetime
import pandas as pd
import os
import random
app = Flask(__name__)
today = datetime.today()
matches_part1 = pd.read_csv("all_matches_part1.csv")
matches_part2 = pd.read_csv("all_matches_part2.csv")
matches = pd.concat([matches_part1, matches_part2], ignore_index=True).reset_index(drop=True)
#filter today
matches = matches[(matches["Month"] == today.month) & (matches["Day"] == today.day)]
# matches=pd.read_csv("matches29072023_IMDB.csv") # --- use if you want a specific csv
# Concatenating books parts
books_part1 = pd.read_csv("best_books_part1.csv")
books_part2 = pd.read_csv("best_books_part2.csv")
books = pd.concat([books_part1, books_part2], ignore_index=True).reset_index(drop=True)
movies = pd.read_csv("imdb_movie_fetch.csv")
# Replace ' with '
matches = matches.replace("'", "'", regex=True)
books = books.replace("'", "'", regex=True)
movies = movies.replace("'", "'", regex=True)
matches["Date"] = matches["Date"].str[:10]
@app.route('/')
def home():
matches_today = matches
international_days = matches_today[matches_today['Country'].notna()]
birthdays = matches_today[matches_today['description'] == "birthday"]
anniversaries = matches_today[(matches_today['Country'].isna()) & (matches_today['description'] != "birthday")]
days_list = [international_days, birthdays, anniversaries]
recommendations = []
for days in days_list:
if not days.empty:
day = days.sample(1)
for _, match in day.iterrows():
movies_match = [movies[movies['title'] == match[f'match_movie{i+1}']] for i in range(3)]
books_match = [books[books['title'] == match[f'match_book{i+1}']] for i in range(3)]
movies_list = [movie_match.iloc[0] if not movie_match.empty else None for movie_match in movies_match]
books_list = [book_match.iloc[0] if not book_match.empty else None for book_match in books_match]
if any(movie is not None for movie in movies_list) or any(book is not None for book in books_list):
if pd.notna(match['Country']):
day_name = str(match["Name"]) + " (" + str(match["Country"])+ ")"
day_description = match['description']
elif match['description'] == "birthday":
day_name = "Birthday"
day_description = match["Name"] + " (" + match["Date"] + ")"
else:
day_name = match['Name'] + " (" + match["Date"] + ")"
day_description = match['description']
recommendation_movies = []
for i, movie in enumerate(movies_list):
if movie is not None:
recommendation_movies.append({
'title': movie['title'],
'description': match[f'match_movie{i+1}_descr'],
'image': movie["image"] if pd.notna(movie["image"]) else None,
'rating': movie['rating'],
'nr_rates': movie['nr_rates'],
'url': movie["url"] if pd.notna(movie["url"]) else None
})
recommendation = {
'day_name': day_name,
'day_description': day_description,
'day_image': match['image'],
'movies': recommendation_movies,
'books': [{
'title': book['title'],
'description': match[f'match_book{i+1}_descr'],
'image': book['img_url'],
'rating': book['rating'],
'nr_rates': book['rating_count'],
'url': book['url'],
} for i, book in enumerate(books_list) if book is not None]
}
recommendations.append(recommendation)
return render_template('home2.html', recommendations=recommendations)
if __name__ == "__main__":
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)