-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbase.py
168 lines (155 loc) · 5.78 KB
/
dbase.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
import sqlite3
import os
import time
import logging
log = logging.getLogger('mpsd')
class MpsdDB(object):
def __init__(self, path):
self.path = path
def connect(self):
"""
Connect to the specified database
"""
dne = False
c = None
if not (os.access(self.path, os.F_OK)):
dne = True
try:
self.db = sqlite3.connect(self.path)
c = self.db.cursor()
except sqlite3.Error as err:
log.error("%s" % (err.args[0]))
return None
c.execute('''PRAGMA foreign_key = ON''')
if dne:
c.execute('''CREATE TABLE artist (
id INTEGER, \
name TEXT, \
PRIMARY KEY (id) \
)''')
c.execute('''CREATE TABLE album ( \
id INTEGER, \
title TEXT, \
date INTEGER, \
artist INTEGER, \
FOREIGN KEY (artist) REFERENCES artist (id), \
PRIMARY KEY (id) \
)''')
c.execute('''CREATE TABLE track ( \
id INTEGER, \
num INTEGER, \
title TEXT, \
artist INTEGER, \
length INTEGER, \
genre TEXT, \
album INTEGER, \
FOREIGN KEY (album) REFERENCES album (id), \
FOREIGN KEY (artist) REFERENCES artist (id) \
PRIMARY KEY (id) \
)''')
c.execute('''CREATE TABLE listened ( \
track INTEGER, \
date TEXT, \
listentime INTEGER, \
FOREIGN KEY (track) REFERENCES track(id), \
PRIMARY KEY (track, date) \
)''')
#Dates are stored "YYYY-MM-DD HH:MM:SS"
self.db.commit()
def getInfo(self, info):
"""
Get all the info from the dictionary and return a full,
properly formatted dict that will be used
"""
#Add a log of badly tagged files?
rval = {}
# artist/albumartist
rval['albumartist'] = info.get('albumartist', 'Unkown Artist')
rval['artist'] = info.get('artist', rval['albumartist'])
# date
rval['date'] = info.get('date', '')
rval['date'] = int(rval['date'].rsplit('-')[0])
# track
rval['track'] = info.get('track', '0')
rval['track'] = int(rval['track'].rsplit('/')[0])
# genre
rval['genre'] = info.get('genre', 'Unknown')
if isinstance(rval['genre'], (list, tuple)):
rval['genre'] = rval['genre'][0]
# other
rval['title'] = info.get('title', 'Unknown Track')
rval['album'] = info.get('album', 'Unknown Album')
rval['time'] = int(info['time'])
return rval
def update(self, track):
"""
Update the database with the given info
"""
id = {}
info = self.getInfo(track)
c = self.db.cursor()
# Artist and AlbumArtist
for a in ("artist", "albumartist"):
c.execute('''SELECT id FROM artist \
WHERE name=?''', [info[a]])
row = c.fetchone()
if row == None:
#add the artist
c.execute('''INSERT INTO artist VALUES \
(?,?)''', (None, info[a]))
self.db.commit()
id[a] = c.lastrowid
log.debug("Adding new %s: %s, id: %s" % (a, info[a], id[a]))
else:
id[a] = int(row[0])
if(info['artist'] == info['albumartist']):
#Must be a better way...
id['albumartist'] = id[a]
break
# Album
c.execute('''SELECT id FROM album WHERE title=?''', [info['album']])
row = c.fetchone()
if row == None:
# add the album
c.execute('''INSERT INTO album VALUES \
(?, ?, ?, ?)''',
(None, info['album'], info['date'],
id['albumartist']))
self.db.commit()
id['album'] = c.lastrowid
log.debug("Adding new album: %s, id: %s"
% (info['album'], id['album']))
else:
id['album'] = int(row[0])
# Track
c.execute('''SELECT id,title \
FROM track \
WHERE title=? AND album=?''',
(info['title'], id['album']))
row = c.fetchone()
if row == None:
# add the track
c.execute('''INSERT INTO track VALUES (?,?,?,?,?,?,?)''',
(None, info['track'], info['title'], id['artist'],
info['time'], info['genre'], id['album']))
self.db.commit()
id['track'] = c.lastrowid
log.debug("Adding new track: %s. %s, id: %s"
% (info['track'], info['title'], id['track']))
else:
id['track'] = int(row[0])
# Listened Table insert
t = time.strftime('%Y-%m-%d %H:%M:%S')
c.execute('''INSERT INTO listened VALUES \
(?, ?, 0)''',
(id['track'], t))
self.db.commit()
log.info("Added track: %(artist)s - %(album)s - %(track)s. %(title)s"
% info)
return t
def updateListentime(self, total, date):
c = self.db.cursor()
c.execute('''UPDATE listened SET listentime=? WHERE date=?''',
(total, date))
self.db.commit()
log.debug("Updated listentime to %d" % (total))