-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdatabase.py
More file actions
392 lines (291 loc) · 12.5 KB
/
database.py
File metadata and controls
392 lines (291 loc) · 12.5 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import sqlite3
from datetime import datetime
import config
import metadata
SERIES_COLUMNS="s.id, s.zap2it_id, s.imdb_id, s.title, s.description, s.actors, s.genres, s.content_rating, s.watch"
EPISODE_COLUMNS="e.id, e.title, e.description, e.season_number, e.episode_number, e.original_air_date, e.rating, e.director, e.host, e.choreographer, e.guest_stars, e.writers, e.executive_producers, e.producers"
MOVIE_COLUMNS="m.id, m.imdb_id, m.title, m.description, m.time, m.rating, m.directors, m.writers, m.producers, m.actors, m.movie_year, m.mpaa_rating, m.genres"
TIME_FORMAT='%Y-%m-%d %H:%M:%S'
ORIGINAL_AIR_DATE_FORMAT='%Y-%m-%d'
class Database():
def __init__(self, config, debug):
self.debug = debug
database_file = config.getDatabaseFile()
self.connection = sqlite3.connect(database_file)
#self.connection.text_factory = sqlite3.OptimizedUnicode
def get_series(self, id):
c = self.connection.cursor()
try:
sql = 'select %s from series s where id=?' % (SERIES_COLUMNS, )
c.execute(sql, (id, ))
results = c.fetchall()
if len(results) == 1:
r = results[0]
to_return = self.create_series_from_row(r)
return to_return
else:
return None
finally:
c.close()
def get_all_series(self):
c = self.connection.cursor()
try:
sql = 'select %s from series s' % (SERIES_COLUMNS, )
c.execute(sql)
to_return = []
results = c.fetchall()
for r in results:
to_return.append(self.create_series_from_row(r))
return to_return
finally:
c.close()
def add_series(self, series):
existing_series = self.get_series(series.id)
if existing_series is None:
if self.debug:
print "Adding series '%s' to the local cache." % (series.title, )
c = self.connection.cursor()
try:
if series.watch:
watch = 1
else:
watch = 0
c.execute(
'insert into series (id, zap2it_id, imdb_id, title, description, actors, genres, content_rating, watch) values (?, ?, ?, ?, ?, ?, ?, ?, ?)',
(series.id, series.zap2it_id, series.imdb_id, series.title, series.description, '|'.join(series.actors), '|'.join(series.genres), series.content_rating, watch))
self.connection.commit()
finally:
c.close()
def watch_series(self, series, watch):
existing_series = self.get_series(series.id)
if not existing_series is None:
c = self.connection.cursor()
try:
if watch:
db_watch = 1
else:
db_watch = 0
c.execute(
'update series set watch = ? where id = ?',
(db_watch, series.id))
self.connection.commit()
finally:
c.close()
if watch:
print "Will now watch series '%s'." % (series.title, )
else:
print "Will no longer watch series '%s'." % (series.title, )
def get_watched_series(self):
c = self.connection.cursor()
try:
sql = 'select %s from series s where watch=1 order by title' % (SERIES_COLUMNS, )
c.execute(sql)
to_return = []
results = c.fetchall()
for r in results:
to_return.append(self.create_series_from_row(r))
return to_return
finally:
c.close()
def clear_series(self, series_id):
c = self.connection.cursor()
try:
sql = 'delete from series where id=?'
c.execute(sql, (series_id, ))
self.connection.commit()
finally:
c.close()
def add_episode(self, episode, series):
if self.debug:
print "Adding season %i episode %i of series '%s' to the local cache." % (episode.season_number, episode.episode_number, series.title)
c = self.connection.cursor()
try:
directors = '|'.join(episode.directors)
guest_stars = '|'.join(episode.guest_stars)
writers = '|'.join(episode.writers)
executive_producers = '|'.join(episode.executive_producers)
producers = '|'.join(episode.producers)
if episode.original_air_date is None:
original_air_date = ''
else:
original_air_date = episode.original_air_date.strftime(ORIGINAL_AIR_DATE_FORMAT)
sql = 'insert into episodes '
sql = sql + '(series_id, title, description, season_number, episode_number, '
sql = sql + ' original_air_date, rating, director, host, choreographer, '
sql = sql + ' guest_stars, writers, executive_producers, producers)'
sql = sql + 'values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'
c.execute(sql,
(series.id, episode.title, episode.description, episode.season_number,
episode.episode_number, original_air_date,
episode.rating, directors, episode.host, episode.choreographer,
guest_stars, writers, executive_producers, producers))
self.connection.commit()
finally:
c.close()
def clear_all_episodes(self, series_id):
c = self.connection.cursor()
try:
sql = 'delete from episodes where series_id=?'
c.execute(sql, (series_id, ))
self.connection.commit()
finally:
c.close()
def get_all_episodes(self, series_id):
c = self.connection.cursor()
try:
sql = 'select %s, %s from episodes e join series s on e.series_id = s.id where s.id=?' % (SERIES_COLUMNS, EPISODE_COLUMNS, )
c.execute(sql, (series_id, ))
results = c.fetchall()
to_return = []
for r in results:
to_return.append(self.create_episode_from_row(r))
return to_return
finally:
c.close()
def get_episodes(self, series_id, season_number):
c = self.connection.cursor()
try:
sql = 'select %s, %s from episodes e join series s on e.series_id = s.id where s.id=? and e.season_number=?' % (SERIES_COLUMNS, EPISODE_COLUMNS, )
c.execute(sql, (series_id, season_number))
results = c.fetchall()
to_return = []
for r in results:
to_return.append(self.create_episode_from_row(r))
return to_return
finally:
c.close()
def get_episode(self, series_id, season_number, episode_number):
c = self.connection.cursor()
try:
sql = 'select %s, %s from episodes e join series s on e.series_id = s.id where s.id=? and e.season_number=? and e.episode_number=?' % (SERIES_COLUMNS, EPISODE_COLUMNS, )
c.execute(sql, (series_id, season_number, episode_number))
results = c.fetchall()
if len(results) == 1:
to_return = self.create_episode_from_row(results[0])
return to_return
else:
return None
finally:
c.close()
def get_episode_by_date(self, series_id, year, month, day):
the_date = datetime(year, month, day)
the_date_str = the_date.strftime(ORIGINAL_AIR_DATE_FORMAT)
c = self.connection.cursor()
try:
sql = 'select %s, %s from episodes e join series s on e.series_id = s.id where s.id=? and e.original_air_date=?' % (SERIES_COLUMNS, EPISODE_COLUMNS, )
c.execute(sql, (series_id, the_date_str))
results = c.fetchall()
if len(results) == 1:
to_return = self.create_episode_from_row(results[0])
return to_return
else:
return None
finally:
c.close()
def get_movie(self, id):
c = self.connection.cursor()
try:
sql = 'select %s from movies m where imdb_id=?' % (MOVIE_COLUMNS, )
c.execute(sql, (id, ))
results = c.fetchall()
if len(results) == 1:
r = results[0]
to_return = self.create_movie_from_row(r)
return to_return
else:
return None
finally:
c.close()
def get_all_movies(self):
c = self.connection.cursor()
try:
sql = 'select %s from movies m' % (MOVIE_COLUMNS, )
c.execute(sql)
to_return = []
results = c.fetchall()
for r in results:
to_return.append(self.create_movie_from_row(r))
return to_return
finally:
c.close()
def add_movie(self, movie):
existing_movie = self.get_movie(movie.id)
if existing_movie is None:
if self.debug:
print "Adding movie '%s' to the local cache." % (movie.title, )
c = self.connection.cursor()
try:
c.execute(
'insert into movies (imdb_id, title, description, time, rating, directors, writers, producers, actors, movie_year, mpaa_rating, genres) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
(movie.id, movie.title, movie.description, movie.time, movie.rating, '|'.join(movie.directors), '|'.join(movie.writers), '|'.join(movie.producers), '|'.join(movie.actors), movie.movie_year, movie.mpaa_rating, '|'.join(movie.genres)))
self.connection.commit()
finally:
c.close()
def clear_movie(self, movie_id):
c = self.connection.cursor()
try:
sql = 'delete from movies where imdb_id=?'
c.execute(sql, (movie_id, ))
self.connection.commit()
finally:
c.close()
def create_series_from_row(self, row):
to_return = metadata.Series()
to_return.id = row[0]
to_return.zap2it_id = row[1]
to_return.imdb_id = row[2]
to_return.title = row[3]
to_return.description = row[4]
to_return.actors = row[5].split('|')
to_return.genres = row[6].split('|')
to_return.content_rating = row[7]
if row[8] == 1:
to_return.watch = True
else:
to_return.watch = False
return to_return
def create_episode_from_row(self, row):
to_return = metadata.Episode()
to_return.series = self.create_series_from_row(row)
to_return.db_id = row[9]
to_return.title = row[10]
to_return.description = row[11]
to_return.season_number = row[12]
to_return.episode_number = row[13]
if not row[14].strip() == '':
to_return.original_air_date = self.get_datetime_from_string(row[14], ORIGINAL_AIR_DATE_FORMAT)
else:
to_return.original_air_date = None
to_return.rating = row[15]
to_return.directors = row[16].split('|')
to_return.host = row[17]
to_return.choreographer = row[18]
to_return.guest_stars = row[19].split('|')
to_return.writers = row[20].split('|')
to_return.executive_producers = row[21].split('|')
to_return.producers = row[22].split('|')
return to_return
def create_movie_from_row(self, row):
to_return = metadata.Movie()
to_return.db_id = row[0]
to_return.id = row[1]
to_return.title = row[2]
to_return.description = row[3]
if not row[4].strip() == '':
to_return.time = self.get_datetime_from_string(row[4], TIME_FORMAT)
else:
to_return.time = None
to_return.rating = row[5]
to_return.directors = row[6].split('|')
to_return.writers = row[7].split('|')
to_return.producers = row[8].split('|')
to_return.actors = row[9].split('|')
to_return.movie_year = row[10]
to_return.mpaa_rating = row[11]
to_return.genres = row[12].split('|')
return to_return
def get_datetime_from_string(self, input_str, format):
if input_str is None or input_str == '':
return None
else:
return datetime.strptime(input_str, format)