-
Notifications
You must be signed in to change notification settings - Fork 4
/
info.py
executable file
·191 lines (148 loc) · 5.48 KB
/
info.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
189
190
191
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2013 James Adams
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
import web
import calendar
from library import *
URLS = (
'/(.*)', 'Info',
)
PAGES = [
'last_played',
'most_skipped_artists',
'playthrough_progress',
'unplay_last_played',
'stats',
]
render = web.template.render('templates', globals={'type': type})
def navbar(page):
result = []
for p in PAGES:
active = ''
if p == page:
active = ' class="active"'
result.append('<li%s><a href="%s">%s</a></li>' % (active, p, p.replace('_', ' ').title()))
return "\n".join(result)
def pageify(page, content):
nav = navbar(page)
title = page.replace('_', ' ').title()
return render.index(nav, title, content)
class Info(object):
def GET(self, name):
if name == "last_played":
return last_played()
elif name == "unplay_last_played":
return unplay_last_played()
elif name == "most_skipped_artists":
return most_skipped_artists()
elif name == "playthrough_progress":
return playthrough_progress()
elif name == "stats":
return stats()
else:
return home()
def home():
library = connect()
return pageify(
'Super Simple Player',
[
'Schema Version %s' % SCHEMA_VERSION,
'%d tracks in library' % library.query(sspTrack).count(),
]
)
def last_played():
library = connect()
tracks = library.query(sspTrack).order_by(sspTrack.lastplayed.desc()).limit(50).all()
fields = ["Last Played", "Artist", "Track"]
table = []
for track in tracks:
filepath = track.filepath.split("/")
table.append([track.lastplayed, filepath[-3], filepath[-1]])
return render.table(navbar("last_played"), "Last Played", fields, table)
def unplay_last_played():
library = connect()
track = library.query(sspTrack).order_by(sspTrack.lastplayed.desc()).first()
return render.unplay_last_played(navbar('unplay_last_played'), track.filepath, track.playcount, track.skipcount, track.lastplayed)
#for i in range(0, 5):
# stdout.write("%d..." % (5-i))
# stdout.flush()
# sleep(1)
#track.playcount -= 1
#track.lastplayed = None
#library.commit()
#print "DONE"
def most_skipped_artists():
library = connect()
tracks = library.query(sspTrack.filepath).filter(sspTrack.skipcount >= 1).all()
fields = ["Skips", "Artist"]
table = []
artists = {}
for track in tracks:
artist = track.filepath.split("/")[-3]
if artist not in artists:
artists[artist] = 0
artists[artist] += 1
artists = [[s, a] for a, s in artists.iteritems()]
artists.sort(key=lambda record: record[1])
artists.sort(key=lambda record: record[0])
artists.reverse()
for record in artists[:20]:
table.append(record)
return render.table(navbar("most_skipped_artists"), "Most Skipped Artists", fields, table)
def playthrough_progress():
library = connect()
min_play_count = library.query(func.min(sspTrack.playcount)).first()[0]
unplayed = float(library.query(func.count(sspTrack.playcount)).filter(sspTrack.playcount == min_play_count).first()[0])
played = float(library.query(func.count(sspTrack.playcount)).filter(sspTrack.playcount > min_play_count).first()[0])
perc = '%.1f%%' % (played / (played + unplayed) * 100)
return render.playthrough_progress(navbar('playthrough_progress'), perc)
def stats():
library = connect()
rawstats = library.query(sspStat).all()
limit = float(max(
max([s.playcount for s in rawstats]),
max([s.skipcount for s in rawstats])
))
stats = []
for s in rawstats:
stats.append(
(
'%.2d - %.2d' % (s.hour, s.hour + 1),
'%.1f%%' % (50 - (s.skipcount / limit) * 50),
'%.1f%%' % ((s.skipcount / limit) * 50),
'%.1f%%' % ((s.playcount / limit) * 50),
s.skipcount,
s.playcount,
)
)
weekgrid = [[('rgb(255, 255, 255);', 0, 0) for _ in range(0, 24)] for _ in range(0, 7)]
weekstats = library.query(sspWeekStat).all()
weekmax = float(library.query(func.max(func.max(sspWeekStat.skipcount, sspWeekStat.playcount))).first()[0])
for s in weekstats:
rSkips = float(s.skipcount) / weekmax
rPlays = float(s.playcount) / weekmax
r = 255 - (255 * rPlays)
b = 255 - (255 * rSkips)
g = (r + b) / 2
weekgrid[s.day][s.hour] = ("rgb(%d, %d, %d);" % (r, g, b), s.playcount, s.skipcount)
return render.stats(navbar('stats'), stats, weekgrid, calendar.day_abbr)
if __name__ == "__main__":
app = web.application(URLS, globals())
app.run()