-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMovieTrack.py
163 lines (144 loc) · 5.7 KB
/
MovieTrack.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
163
import sys
import sqlite3
import datetime
import argparse
parser = argparse.ArgumentParser()#setup for opt parse
parser.parse_args()
conn = sqlite3.connect('moviedb.db')
c = conn.cursor() #This is necessary
class Movie(object):
def __init__(self, db, name, create):
self.db = db
self.name = name.lower() #Not sure if I should always format to lower case.
self.day = day
self.rating = rating
if not self.in_db() and create:
self.create()
else:
return None
@property
def all(self):
return self.db.c.execute("SELECT * FROM myMovies")
def delete(self):
self.db.c.execute("DELETE FROM myMovies WHERE movie == ?", (self.name,))
conn.commit()
def in_db(self):
self.db.c.execute("SELECT * FROM myMovies WHERE movie == ?", (self.name,))
try:
self.db.c.fetchone()
return True
except sqlite3.DatabaseError:
return False
def create(self):
self.db.c.execute("INSERT INTO myMovies (movie, day, rating) VALUES (?, ?, ?) ", (self.name, self.day, self.rating))
conn.commit()
class Actor(object):
def __init__(self):
self.name = name
if not self.in_db():
self.add_to_db()
self.db.close()
print self.name
def delete(self):
self.db.c.execute("DELETE FROM actors WHERE actor == ?", (self.name,))
self.db.conn.commit()
def in_db(self):
self.db.c.execute("SELECT * FROM actors WHERE actor == ?", (self.name,))
try:
self.db.c.fetchone()
return True
except sqlite3.DatabaseError:
return False
def add_to_db(self):
self.db.c.execute("INSERT INTO actors VALUES (?)", (self.name,))
def Rating(object):
def __init__(self, rating, movie, user):
self.rating = rating
self.movie = movie
self.user = user
if not self.in_db():
self.add_to_db()
def delete(self):
self.db.c.execute("DELETE FROM ratings WHERE username == ?", (self.user.name,))
self.db.conn.commit()
def in_db(self):
self.db.c.execute("SELECT * FROM ratings WHERE username == ?", (self.user.name,))
try:
self.db.fetchone()
return True
except sqlite3.DatabaseError:
return False
def add_to_db(self):
self.db.c.execute("INSERT INTO ratings VALUES (?)", (self.user.name,)) # I'm not sure if this syntax is right; I just tried it. No guarantees.
def get_all_by_user(self):
self.db.c.execute("SELECT movie, date, rating FROM ratings WHERE username == ?", (self.user.name,))
return self.db.c.fetchall()
class User(object):
def __init__(self, db, username, password):
self.db = db
self.username = username
self.password = password
self.movies_viewed = self.get_viewed_movies()
def get_viewed_movies(self):
self.db.c.execute("SELECT movie, date, rating FROM viewed_movies WHERE username='%s'" % self.username)
row = c.fetchone()
row = (1, 2, 3) # replace with db fetch
if None in row:
return None
return dict(zip(('movie', 'date', 'rating'), row))
class Database(object):
def __init__(self, file):
self.db = sqlite3.connect(file)
self.c = self.db.cursor()
self.c.execute('''CREATE table IF NOT EXISTS myMovies
(movie text, day text, rating text)''')
self.c.execute('''CREATE TABLE IF NOT EXISTS users
(username text, password text)''')
self.c.execute('''CREATE TABLE IF NOT EXISTS viewed_movies
(movie text, date text, username text, rating text)''')
def __del__(self):
self.db.commit() # When the program closes, shit gonna commit.
if __name__ == "__main__":
db = Database("moviedb.db")
u = raw_input("Username: ")
p = raw_input("Password: ")
user = User(db, u, p)
while True:
print("""
1. Add a movie.
2. Delete a movie.
3. View all movies.
""")
option = raw_input("Please select an option:")
if option == "1":
movie_name = raw_input('Enter a movie title:')
day = raw_input('Enter the date you viewed the movie:')
rating = raw_input('Please enter a rating for you movie 1-10:')
movie = Movie(db, movie_name, create = True)
movie.create()
print("Your movie has been added!",)
elif option == "2":
to_delete = raw_input("Please type a movie that you want deleted:",)
day = "" #There is probably a better way to do this...change it later
rating = "" #There is probably a better way to do this...change it later
movie = Movie(db, to_delete, create = True)
if movie is None:
print("That movie is not in database!")
else:
movie.delete()
print("Movie {} has been deleted.".format(to_delete))
print("Your movie has been deleted.",)
elif option == "3":
print user.get_viewed_movies()
#elif option == "4":
# print user.movies_viewed
#elif option == "5":
# movie_name = raw_input("Movie name?")
# try:
# movie = Movie(db, movie_name)
# r = raw_input("Rating? (1-10)")
# rating = Rating(db, r, movie, user)
# except:
# print "I didn't handle this properly, I'm still on my bender and I bought a house; give me a break."
#sys.exit(0)
conn.close() #This needs to be somewhere, but system does not close with an empty string.