-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utilities.py
143 lines (115 loc) · 5.35 KB
/
Utilities.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
import calendar
import time
import sqlalchemy
import tagpy
import os
from models import Media, Studio, Playlist, session
class Utilities:
def __init__(self):
pass
@staticmethod
def generate_studios():
for letter in ('a', 'b'):
if session.query(Studio).filter_by(slug=u"studio_%s" % letter).count() == 0:
studio_x = Studio(name=u"Studio %s" % letter,
slug=u"studio_%s" % letter,
jukebox_liqname=u"studio_%s_jukebox_playlist" % letter,
bed_liqname=u"studio_%s_bed_playlist" % letter,
fx_liqname=u"studio_%s_fx_playlist" % letter,
rec_show_liqname=u"studio_%s_recorder_show" % letter,
rec_show_enabled=False,
rec_show_active=False,
rec_gold_liqname=u"studio_%s_recorder_gold" % letter,
rec_gold_enabled=False,
rec_gold_active=False,
selected=False)
session.add(studio_x)
session.commit()
@staticmethod
def attach_empty_playlists():
for letter in ('a', 'b'):
studio = session.query(Studio).filter_by(slug=u"studio_%s" % letter).one()
if studio.jukebox is None:
studio.jukebox = Playlist()
session.add(studio)
session.commit()
@staticmethod
def rescan(force=False):
dossier = "/home/synopslive/media/"
totalsec = notag = tagged = total = addednum = updatednum = deletednum = intacts = 0
alltimes = {}
notfound = []
for path, updated_at in session.query(Media.path, Media.updated_at):
alltimes[path] = calendar.timegm(updated_at.utctimetuple())
notfound.append(path)
for root, dirs, files in os.walk(dossier):
for name in files:
if name.lower().endswith("mp3") or name.lower().endswith("ogg"):
elpath = os.path.join(os.path.abspath(root), name)
elfilename = os.path.basename(elpath)
alreadyexists = elpath in alltimes.keys()
if alreadyexists:
notfound.remove(elpath)
if force or not alreadyexists or elpath in alltimes and \
os.stat(elpath).st_mtime > alltimes[elpath]:
f = tagpy.FileRef(os.path.join(root, name))
t = f.tag()
if not t.isEmpty() and t.title and t.artist:
tagged += 1
else:
notag += 1
nbsec = f.audioProperties().length
totalsec += nbsec
eltitle = t.title or None
elartist = t.artist or None
elalbum = t.album or None
try:
elpath = elpath.decode("utf-8")
elfilename = elfilename.decode("utf-8")
except UnicodeDecodeError:
try:
elpath = elpath.decode("latin1")
elfilename = elfilename.decode("latin1")
except UnicodeDecodeError:
pass
ellength = nbsec
if not alreadyexists:
session.add(Media(path=elpath,
filename=elfilename,
title=eltitle,
artist=elartist,
album=elalbum,
length=int(ellength),
added_at=sqlalchemy.func.now(),
updated_at=sqlalchemy.func.now()))
addednum += 1
elif alreadyexists:
media = session.query(Media).filter_by(path=elpath).first()
media.filename = elfilename
media.title = eltitle
media.album = elalbum
media.artist = elartist
media.length = int(ellength)
media.updated_at = int(time.time())
updatednum += 1
del elpath, eltitle, elartist, elalbum, ellength
del t, f
total += 1
else:
intacts += 1
if len(notfound):
for apath in notfound:
media = session.query(Media).filter_by(path=apath).first()
session.delete(media)
deletednum += 1
session.commit()
return {
"added": addednum,
"updated": updatednum,
"deleted": deletednum,
"total": total,
"untouched": intacts,
"tagged": tagged,
"untagged": notag,
"totalsec": totalsec
}