-
Notifications
You must be signed in to change notification settings - Fork 1
/
mockdata.py
99 lines (85 loc) · 3.07 KB
/
mockdata.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
import os
import binascii
import sqlite3
class User(object):
@staticmethod
def load_by_name(name):
with sqlite3.connect('database.db') as conn:
c = conn.cursor()
c.execute('SELECT * FROM users WHERE name=?', (name,))
row = c.fetchone()
if row:
return User(row)
return None
@staticmethod
def load_by_id(id):
with sqlite3.connect('database.db') as conn:
c = conn.cursor()
c.execute('SELECT * FROM users WHERE id=?', (id,))
row = c.fetchone()
if row:
return User(row)
return None
def __init__(self, row):
id, name, timestamp = row
self.id = id
self.name = name
self.timestamp = timestamp
def scrobble(self, timestamp, artist, track, album, albumArtist):
with sqlite3.connect('database.db') as conn:
c = conn.cursor()
c.execute('INSERT INTO scrobbles (user, timestamp, artist, track, album, albumArtist) values (?,?,?,?,?,?);', (self.id, timestamp, artist, track, album, albumArtist,))
class Session(object):
@staticmethod
def load(session):
with sqlite3.connect('database.db') as conn:
c = conn.cursor()
c.execute('SELECT * FROM sessions WHERE id=?', (session,))
row = c.fetchone()
if row:
return Session(row)
return None
@staticmethod
def create(user):
session = binascii.b2a_hex(os.urandom(20))
with sqlite3.connect('database.db') as conn:
c = conn.cursor()
c.execute('INSERT INTO sessions (id, user) VALUES (?,?);', (session, user.id,))
return Session.load(session)
def __init__(self, row):
id, user, timestamp = row
self.id = id
self.user = User.load_by_id(user)
self.timestamp = timestamp
class Token(object):
@staticmethod
def load(token):
with sqlite3.connect('database.db') as conn:
c = conn.cursor()
c.execute('SELECT * FROM tokens WHERE token=?', (token,))
row = c.fetchone()
if row:
return Token(row)
return None
@staticmethod
def generate():
token = binascii.b2a_hex(os.urandom(20))
with sqlite3.connect('database.db') as conn:
c = conn.cursor()
c.execute('INSERT INTO tokens (token) VALUES (?);', (token,))
return Token.load(token)
def __init__(self, row):
token, user, timestamp = row
self.token = token
self.timestamp = timestamp
self.user = None
if user:
self.user = User.load_by_id(user)
def validate(self, user):
with sqlite3.connect('database.db') as conn:
c = conn.cursor()
c.execute('UPDATE tokens SET user = ? WHERE token=?', (user, self.token,))
def consume(self):
with sqlite3.connect('database.db') as conn:
c = conn.cursor()
c.execute('DELETE FROM tokens WHERE token=?', (self.token,))