-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseModel.py
More file actions
231 lines (185 loc) · 8.51 KB
/
DatabaseModel.py
File metadata and controls
231 lines (185 loc) · 8.51 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
from sqlalchemy import create_engine, ForeignKey, Column, String, Integer
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
import DataGrabber
import KeysandPaths
# Outline Movie table has an ID, title, year of release, IMDB ID, poster directory, overview text file directory, may or
# may not have an age rating and viewer rating, but has a runtime, and actor IDs and director IDs and genre IDS
# actor names can be returned using ID as well as Directors and genre types. A movie must have actors and directors and
# at least 1 genre, actors may or may not have a movie ID which represent movie they were in, A director must have a
# movie ID to be a director and finally Genres may or may not have a movie ID.
Base = declarative_base()
class Movie(Base):
__tablename__ = "Movies"
Movie_ID = Column("Movie_ID", Integer, primary_key=True, unique=True)
Title = Column("Title", String)
Year = Column("Release_Year", Integer)
IMDB_ID = Column("IMDB_ID", Integer)
Poster_Dir = Column("Poster_Dir", String)
Overview_Dir = Column("Overview_Dir", String)
Subtitle_Dir = Column("Subtitle_Dir", String)
Age_rating = Column("Age_rating", String)
Viewer_rating = Column("Viewer_rating_out_of_10", Integer)
Runtime = Column("Runtime", Integer)
def __int__(self, movie_id, title, year, imbd_id, poster_dir, Overview_Dir, Subtitle_Dir, Age_rating, Viewer_rating,
Runtime):
self.Movie_ID = movie_id
self.Title = title
self.Year = year
self.IMDB_ID = imbd_id
self.Poster_Dir = poster_dir
self.Overview_Dir = Overview_Dir
self.Subtitle_Dir = Subtitle_Dir
self.Age_rating = Age_rating
self.Viewer_rating = Viewer_rating
self.Runtime = Runtime
def __repr__(self):
return f"({self.Movie_ID}) {self.Title} {self.Year} {self.IMDB_ID} {self.Poster_Dir} {self.Overview_Dir}" \
f" {self.Subtitle_Dir} {self.Age_rating} {self.Viewer_rating} {self.Runtime}"
class Actor(Base):
__tablename__ = "Actors"
Actor_ID = Column("Actor_ID", Integer, primary_key=True)
Movie_ID = Column(Integer, ForeignKey("Movies.Movie_ID"))
Actor_name = Column("Actor_Name", String)
popularity = Column("popularity_score_out_of_100", Integer)
def __int__(self, actor_id, movie_id, actor_name, popularity):
self.Actor_ID = actor_id
self.Movie_ID = movie_id
self.Actor_name = actor_name
self.popularity = popularity
def __repr__(self):
return f"({self.Actor_ID}) {self.Movie_ID} {self.Actor_name} {self.popularity}"
class Director(Base):
__tablename__ = "Directors"
Director_ID = Column("Director_ID", Integer, primary_key=True)
Movie_ID = Column(Integer, ForeignKey("Movies.Movie_ID"))
Director_name = Column("Director_name", String)
def __int__(self, director_id, movie_id, director_name):
self.Director_ID = director_id
self.Movie_ID = movie_id
self.Director_name = director_name
def __repr__(self):
return f"({self.Director_ID}) {self.Movie_ID} {self.Director_name}"
class Genre(Base):
__tablename__ = "Genres"
Genre_ID = Column("Genre_ID", Integer, primary_key=True)
Movie_ID = Column(Integer, ForeignKey("Movies.Movie_ID"))
Genre_name = Column("Genre_name", String)
def __int__(self, genre_id, movie_id, genre_name):
self.Genre_ID = genre_id
self.Movie_ID = movie_id
self.Genre_name = genre_name
def __repr__(self, ):
return f"({self.Genre_ID}) {self.Movie_ID} {self.Genre_name}"
class User(Base):
__tablename__ = "Users"
User_ID = Column("User_ID", Integer, primary_key=True, unique=True)
User_Name = Column("User_Name", String)
email = Column("Email", String, unique=True)
password = Column("Password", String)
def __int__(self, user_id, user_name, email, password):
self.User_ID = user_id
self.User_Name = user_name
self.email = email
self.password = password
def __repr__(self):
return f"({self.User_ID}) {self.User_Name} {self.email} {self.password}"
engine = create_engine("sqlite:///Home_Movies.db", echo=True)
Base.metadata.create_all(bind=engine)
Session = sessionmaker(bind=engine)
session = Session()
print(f'Total Movies in Home Movies Folder {len(DataGrabber.my_movie_dictionary(KeysandPaths.rtnfolder_path()))} \n')
count = 1
imdb_ids = []
# For each Movie In my home movies folder get:
# len(DataGrabber.my_movie_dictionary(KeysandPaths.rtnfolder_path()))
for i in range(len(DataGrabber.my_movie_dictionary(KeysandPaths.rtnfolder_path()))):
# Title and Year
T = DataGrabber.my_movie_dictionary(KeysandPaths.rtnfolder_path())[i][0]
Y = DataGrabber.my_movie_dictionary(KeysandPaths.rtnfolder_path())[i][1]
print(f"{count}.", f"{T} {Y}")
ID = DataGrabber.get_imdb_movie_poster_and_id(T, Y)
# IMDB ID
print(f"IMDB ID: {ID[0]['id']}")
imdb_ids.append(ID[0]['id'])
# Posters
print(f"Movie Poster: {ID[0]['image']} \n")
img_url = ID[0]['image']
img_dir = T + "(" + Y + ")"
path_4_posters = KeysandPaths.rtnpath_4_posters(img_dir)
# download_image(f"{path_4_posters} ", img_url,f"{T}{Y} poster.jpg")
# Age rating
age_rating_certification = [rating for rating in DataGrabber.get_age_rating(imdb_ids[i]) if rating["iso_3166_1"]
== "US"]
rating = []
if not age_rating_certification:
rating.append('NR')
else:
age_rating = [rating for rating in age_rating_certification[0]['release_dates'] if rating['certification']]
for r in range(len(age_rating)):
rating.append(age_rating[r]['certification'])
if not age_rating:
rating.append('NR')
print(f"Age rating: {rating[0]}\n")
# Genres
movie_genres = []
genres = DataGrabber.getmovieinfo(imdb_ids[i])[0]['genres']
print("Genres:")
for g in range(len(genres)):
movie_genres.append(genres[g]['name'])
for g in range(len(genres)):
print(movie_genres[g])
# Overview
overview = DataGrabber.getmovieinfo(imdb_ids[i])[0]['overview']
print(f"\nOverview: {overview} \n")
overview_dir = T + "(" + Y + ")"
path_4_overview = KeysandPaths.rtnpath_4_overview(overview_dir)
# write_movie_overview(f"{path_4_overview}",overview,f"{T}{Y} overview.txt" )
path_4_subtitles = KeysandPaths.rtnpath_4_subtites(overview_dir)
# Runtime
runtime = DataGrabber.getmovieinfo(imdb_ids[i])[0]['runtime']
print(f"Runtime: {runtime} \n")
v_rating = int(DataGrabber.getmovieinfo(imdb_ids[i])[0]['vote_average'])
# Viewer rating
print(f"Viewer rating: {v_rating} \n")
# starring cast
print("Starring: ")
crew = DataGrabber.get_credits(imdb_ids[i])[0]['cast']
crew_scores = [c for c in crew if crew[len(crew) - 1]['popularity']]
pop_score = []
for c in range(len(crew_scores)):
pop_score.append(crew_scores[c]['popularity'])
pop_score.sort()
mod_score = pop_score[len(pop_score) - 1] / 2
cast = []
for c in range(len(crew)):
if int(crew[c]['popularity']) >= int(mod_score):
cast.append(crew[c])
for c in range(len(cast)):
print(cast[c]['name'])
# Directors
Directors = []
lst_directors = [credit for credit in DataGrabber.get_credits(imdb_ids[i])[0]['crew'] if credit["job"] == "Director"
]
for people in range(len(lst_directors)):
Directors.append(lst_directors[people]['name'])
for name in range(len(lst_directors)):
print(f"\nDirector: {Directors[name]}")
print("____________________________________________________________________________________________________\n")
movie = Movie(Title=T, Year=Y, IMDB_ID=imdb_ids[0], Poster_Dir=path_4_posters, Overview_Dir=path_4_overview,
Subtitle_Dir=path_4_subtitles, Age_rating=rating[0], Viewer_rating=v_rating, Runtime=runtime)
session.add(movie)
session.commit()
for c in range(len(cast)):
actor = Actor(Movie_ID=movie.Movie_ID, Actor_name=cast[c]['name'], popularity=int(crew[c]['popularity']))
session.add(actor)
for d in range(len(Directors)):
director = Director(Movie_ID=movie.Movie_ID, Director_name=Directors[d])
session.add(director)
for g in range(len(movie_genres)):
genre = Genre(Movie_ID=movie.Movie_ID, Genre_name=movie_genres[g])
session.add(genre)
count += 1
user = User(User_Name="My_name", email="MyEmail@gmail.com", password="password123")
session.add(user)
session.commit()