-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharbs-logger.py
executable file
·188 lines (143 loc) · 5.39 KB
/
arbs-logger.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
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
#!/usr/bin/env python2
import sys
import yaml
import mysql.connector
import datetime
import shout
from mpd import MPDClient
class PlaylistDB:
def __init__(self, connection, cursor):
self.con = connection
self.cur = cursor
def addPlaylist(self, timestamp, song):
sql = "INSERT INTO playlist (date, song) VALUES (%s, %s)"
self.cur.execute(sql, [timestamp, song])
self.con.commit()
def addArtist(self, artist):
sql = "INSERT INTO artists (artist) VALUES (%s)"
self.cur.execute(sql, [artist])
self.con.commit()
def addSong(self, artist, title, length, filename):
sql = ("INSERT INTO songs (artist, title, length, filename) "
"VALUES (%s, %s, %s, %s)")
self.cur.execute(sql, [artist, title, length, filename])
self.con.commit()
# this will check if the artist is in the database, if not, add it
def validateArtist(self, artist):
if not self.checkArtist(artist):
self.addArtist(artist)
return self.getArtistId(artist)
def getSong(self, filename):
sql = ("SELECT s.id, a.artist, s.title, s.length, s.filename "
"FROM songs s, artists a "
"WHERE s.artist = a.id AND filename = %s")
self.cur.execute(sql, [filename])
row = self.cur.fetchone()
return row
def getSongId(self, filename):
sql = "SELECT id FROM songs WHERE filename = %s"
self.cur.execute(sql, [filename])
row = self.cur.fetchone()
if not row:
return None
else:
return int(row[0])
def getArtistId(self, artist):
sql = "SELECT id FROM artists WHERE artist = %s"
self.cur.execute(sql, [artist])
row = self.cur.fetchone()
if not row:
return None
else:
return int(row[0])
def updateArtist(self, songid, artist):
artistid = self.validateArtist(artist)
sql = "UPDATE songs SET artist = %s WHERE id = %s"
self.cur.execute(sql, (artistid, songid))
self.con.commit()
def update(self, songid, field, value):
sql = "UPDATE songs SET " + field + " = %s WHERE id = %s"
self.cur.execute(sql, [value, songid])
self.con.commit()
def checkArtist(self, artist):
if not self.getArtistId(artist):
return False
else:
return True
def setupDB(hostname, username, password, database):
conn = mysql.connector.connect(user=username,
password=password,
database=database,
host=hostname)
return conn
def main(arg):
with open(arg[0], 'r') as configfile:
try:
cfg = yaml.load(configfile)
except yaml.scanner.ScannerError, e:
sys.exit("Error in configuration file: %s" % e)
m = MPDClient()
m.timeout = 60
m.idletimeout = None
# connect to the local running instance
m.connect("localhost", 6600)
conn = setupDB(cfg["database"]["hostname"],
cfg["database"]["username"],
cfg["database"]["password"],
cfg["database"]["database"])
cursor = conn.cursor(buffered=True)
s = shout.Shout()
s.port = cfg["stream"]["port"]
s.host = cfg["stream"]["hostname"]
s.mount = cfg["stream"]["mount"]
s.password = cfg["stream"]["password"]
try:
s.open()
# we can just ignore this - for some strange reason
except shout.ShoutException:
pass
db = PlaylistDB(conn, cursor)
# everything is set up, get ready to receive new events
while 1:
subsystem = m.idle()
if "player" in subsystem:
currentsong = m.currentsong()
status = m.status()
if status["state"] != "play":
continue
if cfg["folders"]["teasers"] in currentsong["file"]:
print(currentsong["file"])
continue
if "artist" not in currentsong.keys():
print(currentsong["file"])
continue
now = datetime.datetime.now()
filename = currentsong["file"]
if filename.startswith("file://"):
filename = filename.replace("file://", "")
song = db.getSong(filename)
# song is not already added
if not song:
artistid = db.validateArtist(currentsong["artist"])
db.addSong(artistid,
currentsong["title"],
currentsong["time"],
filename)
# we should now have a new song id
songid = db.getSongId(filename)
else:
songid = song[0]
db.addPlaylist(now.strftime("%Y-%m-%d %H:%M:%S"), songid)
# update the stream metadata
s.set_metadata(
{
"song": "{} - {}".format(currentsong["artist"],
currentsong["title"]),
}
)
print("{}: {} - {} ({})".format(datetime.datetime.now(),
currentsong["artist"],
currentsong["title"],
filename))
if __name__ == '__main__':
main(sys.argv[1:])