-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
executable file
·69 lines (52 loc) · 1.87 KB
/
database.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
#! /usr/bin/env python
#
# (c) 2012 matt@ginzton.net
import sqlite3
class Database:
def __init__(self):
self.conn = sqlite3.connect('psdb.sqlite', check_same_thread = False)
self.cursor = self.conn.cursor()
def commit(self):
self.conn.commit()
# Photos
def get_photo_count(self):
self.cursor.execute('SELECT count(hash) from photos')
count = self.cursor.fetchone()[0]
return count
def enum_photos(self):
self.cursor.execute('SELECT hash FROM photos')
rows = self.cursor.fetchall()
return [r[0] for r in rows]
def add_photo(self, path, thumb, digest):
self.cursor.execute('INSERT INTO photos VALUES(?,?,?);', (digest, path, thumb))
def get_photo_thumbnail(self, digest):
self.cursor.execute('SELECT thumbnail FROM photos WHERE hash = ?', (digest,))
thumb_data = self.cursor.fetchone()[0]
return thumb_data
def set_photo_rating(self, digest, username, rating):
self.cursor.execute('INSERT OR REPLACE INTO ratings VALUES(?,?,?)', (digest, username, rating))
def get_photo_ratings(self, digest):
self.cursor.execute('SELECT user, rating FROM ratings WHERE photo_hash = ?', (digest,))
ratings = {}
for row in self.cursor.fetchall():
ratings[row[0]] = row[1]
return ratings
# Directories
def get_directory_count(self):
return 1
# Ratings
if __name__ == '__main__':
import sys
if sys.argv[1] == 'init':
cmds = '''
-- users
CREATE TABLE users(name STRING PRIMARY KEY);
INSERT INTO USERS VALUES('matt');
-- photos
CREATE TABLE photos(hash STRING PRIMARY KEY, filename STRING, thumbnail BLOB);
-- ratings
CREATE TABLE ratings(photo_hash STRING, user STRING, rating STRING);
'''
db = Database()
db.cursor.executescript(cmds)
db.commit()