-
Notifications
You must be signed in to change notification settings - Fork 0
/
movies.py
162 lines (125 loc) · 6.61 KB
/
movies.py
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import pandas as pd
class Movies():
"""Class that contains all the functions that the API needs."""
def __init__(self):
"""Constructor"""
url='https://drive.google.com/file/d/1QuvhMiZLka18ZXnx8o1P5C8Cf5oHCgjL/view?usp=sharing'
url='https://drive.google.com/uc?id=' + url.split('/')[-2]
self._df_movies = pd.read_csv(url)
self._df_movies['release_date'] = pd.to_datetime(self._df_movies['release_date'],
format='%Y-%m-%d')
def get_count_movies_month(self, month=''):
"""Get the amount of movies released in the requested month.
Args:
month (str, optional): Released month. Defaults to ''.
Returns:
dict: Message with the information requested.
"""
valid_months = {'enero': 1, 'febrero': 2, 'marzo': 3, 'abril': 4,
'mayo': 5, 'junio': 6, 'julio': 7, 'agosto': 8,
'septiembre': 9, 'setiembre': 9, 'octubre': 10,
'noviembre': 11, 'diciembre': 12}
if month.lower() in valid_months:
variable = valid_months.get(month.lower())
condition = self._df_movies['release_date'].dt.month == variable
return {'mes': month,
'cantidad': f'{self._df_movies[condition].shape[0]}'}
return {'message': f'Month not exists: {month}'}
def get_count_movies_day(self, day=''):
"""Get the amount of movies released in the requested day.
Args:
day (str, optional): Released day. Defaults to ''.
Returns:
dict: Message with the information requested.
"""
valid_days = {'lunes': 0, 'martes': 1, 'miercoles': 2, 'jueves': 3,
'viernes': 4, 'sabado': 5, 'domingo': 6}
if day.lower() in valid_days:
condition = self._df_movies['release_date'].dt.dayofweek == valid_days.get(day.lower())
return {'dia': day, 'cantidad': f'{self._df_movies[condition].shape[0]}'}
return {'message': f'Dia no existente: {day}'}
def get_score_title(self, title=''):
"""Get the released year and the score of the requested title.
Args:
title (str, optional): Movie to be searched. Defaults to ''.
Returns:
dict: Message with the information requested.
"""
df_aux = self._df_movies['title'].str.lower()
index = df_aux[df_aux == title.lower()].index
if len(index.values) > 0:
df_aux = self._df_movies.iloc[index][['title', 'release_year', 'popularity']]
return {'titulo': f'{df_aux["title"].values[0]}',
'anio': f'{df_aux["release_year"].values[0]}',
'popularidad': f'{df_aux["popularity"].values[0].round(1)}'}
return {'message': f'Movie `{title}` not found'}
def get_votes_title(self, title=''):
"""Get the released year, vote count and vote average of the requested title.
Args:
title (str, optional): Movie to be searched. Defaults to ''.
Returns:
dict: Message with the information requested.
"""
df_aux = self._df_movies['title'].str.lower()
index = df_aux[df_aux == title.lower()].index
if len(index.values) > 0:
df_aux = self._df_movies.iloc[index][['title', 'release_year', 'vote_count',
'vote_average']]
if df_aux["vote_count"].values[0] >= 2000:
return {'titulo': f'{df_aux["title"].values[0]}',
'anio': f'{df_aux["release_year"].values[0]}',
'voto_total': f'{int(df_aux["vote_count"].values[0])}',
'voto_promedio': f'{df_aux["vote_average"].values[0].round(1)}'}
return {'message': f'Movie `{title}` has not enough votes'}
return {'message': f'Movie `{title}` not found'}
def get_actor(self, actor=''):
"""Get the actor movies, themaximun return ans the average return.
Args:
actor (str, optional): Actor to be searched. Defaults to ''.
Returns:
dict: Message with the information requested.
"""
df_aux = self._df_movies['cast'].str.lower()
index_list = list(df_aux[df_aux.str.contains(actor.lower())].index.values)
movies_count = len(index_list)
if movies_count > 0:
ret_mean = 0
for index in index_list:
ret_mean += self._df_movies.iloc[index]['return']
if movies_count > 0:
ret_mean = (ret_mean/movies_count).round(1)
else:
ret_mean = 0
return_list = [self._df_movies['return'].iloc[ret] for ret in index_list]
max_value = max(return_list)
index_max_return = index_list[return_list.index(max_value)]
return {'actor': actor,
'cantidad_filmaciones': f'{movies_count}',
'retorno_total': f'{self._df_movies.iloc[index_max_return]["return"].round(1)}',
'retorno_promedio': f'{ret_mean}'}
return {'message': f'Actor `{actor}` not found'}
def get_director(self, director=''):
"""Get all the movies, released year, return, revenue and budget of the requested director.
Args:
director (str, optional): Director to be searched. Defaults to ''.
Returns:
dict: Message with the information requested.
"""
df_aux = self._df_movies['director'].str.lower()
index_list = list(df_aux[df_aux.str.contains(director.lower())].index.values)
if len(index_list)> 0:
m_list = [self._df_movies['title'].iloc[ret] for ret in index_list]
d_list = [self._df_movies['release_year'].iloc[ret] for ret in index_list]
rev_list = [self._df_movies['revenue'].iloc[ret] for ret in index_list]
c_list = [self._df_movies['budget'].iloc[ret] for ret in index_list]
ret_list = [self._df_movies['return'].iloc[ret] for ret in index_list]
max_value = max(ret_list)
index_var = index_list[ret_list.index(max_value)]
return {'director': director,
'retorno_total_director': f'{self._df_movies.iloc[index_var]["return"].round(1)}',
'peliculas': f'{m_list}',
'anio': f'{d_list}',
'retorno_pelicula': f'{ret_list}',
'budget_pelicula': f'{c_list}',
'revenue_pelicula': f'{rev_list}'}
return {'message': f'Director `{director}` not found'}