-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathdatabase.py
98 lines (80 loc) · 2.62 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
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
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship, scoped_session
from sqlalchemy.pool import QueuePool
from sqlalchemy import Column, Integer, CHAR, DateTime, String, ForeignKey, Boolean, Text, create_engine
import config, datetime
Base = declarative_base()
###
# Models
###
class Track(Base):
__tablename__ = 'tracks'
id = Column(Integer, primary_key=True)
uid = Column(CHAR(length=32))
time = Column(DateTime)
hash = Column(CHAR(length=32))
size = Column(Integer)
style = Column(String)
# Tag Attributes
length = Column(Integer)
samplerate = Column(Integer)
channels = Column(Integer)
extension = Column(String)
bitrate = Column(Integer)
title = Column(String)
artist = Column(String)
album = Column(String)
art = Column(String)
thumbnail = Column(String)
events = relationship("Event")
def __init__(self, uid, hash = None, size = None, style = None, length = None, samplerate = None, channels = None, extension = None, bitrate = None, title = None, artist = None, album = None, art = None, thumbnail = None):
self.uid = uid
self.time = datetime.datetime.now()
self.hash = hash
self.size = size
self.style = style
self.length = length
self.samplerate = samplerate
self.channels = channels
self.extension = extension
self.bitrate = bitrate
self.title = title
self.artist = artist
self.album = album
self.art = art
self.thumbnail = thumbnail
class Event(Base):
__tablename__ = 'events'
id = Column(Integer, primary_key=True)
uid = Column(CHAR(length=32) , ForeignKey('tracks.uid'))
action = Column(String)
start = Column(DateTime)
end = Column(DateTime)
success = Column(Boolean)
ip = Column(String)
detail = Column(Text)
track = relationship("Track")
def __init__(self, uid, action, success = None, ip = None, detail = None):
self.uid = uid
self.start = datetime.datetime.now()
if success is not None:
self.end = datetime.datetime.now()
self.action = action
self.success = success
self.ip = ip
self.detail = detail
def time(self):
try:
return self.end - self.start
except:
return datetime.timedelta(0)
###
# DB Connection Handling
###
engine = create_engine(
config.database_connect_string,
echo=config.echo_database_queries,
poolclass=QueuePool,
pool_recycle=10
)
Session = scoped_session(sessionmaker(engine))