-
Notifications
You must be signed in to change notification settings - Fork 19
/
db.py
144 lines (112 loc) · 4.54 KB
/
db.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
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, Unicode, Float, DateTime
from sqlalchemy import ForeignKey
from sqlalchemy.orm import sessionmaker, relationship
from config import config
import art
import datetime
import urllib
DATABASE_URL = config.get('Database', 'url')
engine = create_engine(DATABASE_URL, max_overflow=-1)
Session = sessionmaker(bind=engine)
Base = declarative_base()
class Song(Base):
__tablename__ = 'songs'
__table_args__ = {'mysql_engine': 'InnoDB'}
id = Column(Integer, primary_key=True)
title = Column(Unicode(200))
artist = Column(Unicode(200))
album = Column(Unicode(200))
length = Column(Float)
path = Column(Unicode(1000))
tracknumber = Column(Integer)
# MD5 checksum to verify file integrity
checksum = Column(String(32))
packet = relationship('Packet', uselist=False, cascade='all,delete-orphan',
passive_deletes=True, backref='songs')
history = relationship('PlayHistory', cascade='all,delete-orphan',
passive_deletes=True, backref='songs')
def mrl(self):
return 'file://' + urllib.quote(self.path.encode('utf-8'))
def dictify(self):
return {
'id': self.id,
'title': self.title,
'artist': self.artist,
'album': self.album,
'length': self.length,
'path': self.path,
'tracknumber': self.tracknumber,
'play_count': self.play_count(),
'art_uri': art.get_art(self.artist, self.album),
}
def play_count(self):
session = Session()
count = session.query(PlayHistory).filter_by(song_id=self.id).count()
session.commit()
return count
def last_played(self):
session = Session()
history_item = (session.query(PlayHistory).filter_by(song_id=self.id)
.order_by(PlayHistory.id.desc()).first())
session.commit()
if history_item:
return history_item.played_at
class PlayHistory(Base):
__tablename__ = 'play_history'
__table_args__ = {'mysql_engine': 'InnoDB'}
id = Column(Integer, primary_key=True)
song_id = Column(Integer, ForeignKey('songs.id', ondelete='CASCADE'))
user = Column(String(8))
played_at = Column(DateTime, default=datetime.datetime.utcnow)
player_name = Column(String(16))
class Packet(Base):
__tablename__ = 'packets'
__table_args__ = {'mysql_engine': 'InnoDB'}
id = Column(Integer, primary_key=True)
song_id = Column(Integer, ForeignKey('songs.id', ondelete='CASCADE'),
unique=True)
stream_url = Column(String(100)) # renamed from video_url
stream_title = Column(Unicode(100)) # renamed from video_title
stream_length = Column(Float) # renamed from video_length
stream_id = Column(String(32)) # compatibility with soundcloud & youtube
art_uri = Column(String(100)) # compatibility with soundcloud & youtube
artist = Column(Unicode(100)) # compatibility with soundcloud
user = Column(String(8))
arrival_time = Column(Float)
finish_time = Column(Float)
additional_votes = relationship('Vote', cascade='all,delete-orphan',
passive_deletes=True, backref='packets')
player_name = Column(String(16))
def num_votes(self):
return 1 + len(self.additional_votes)
def weight(self):
# The 1 denotes the user weight
return 1 * 2 ** (self.num_votes() - 1)
def has_voted(self, user):
return (self.user == user or
any(vote.user == user for vote in self.additional_votes))
class Vote(Base):
__tablename__ = 'votes'
__table_args__ = {'mysql_engine': 'InnoDB'}
packet_id = Column(Integer, ForeignKey('packets.id', ondelete='CASCADE'),
primary_key=True)
user = Column(String(8), primary_key=True)
class AuditLogMessage(Base):
__tablename__ = 'audit_log'
__table_args__ = {'mysql_engine': 'InnoDB'}
id = Column(Integer, primary_key=True)
user = Column(String(8))
message = Column(String(200))
timestamp = Column(DateTime, default=datetime.datetime.utcnow)
player_name = Column(String(16))
class BannedUser(Base):
__tablename__ = 'banned_users'
__table_args__ = {'mysql_engine': 'InnoDB'}
username = Column(String(8), primary_key=True)
reason = Column(String(200))
def init_db():
Base.metadata.create_all(engine)
if __name__ == '__main__':
init_db()