forked from speutel/oyster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blacklist.py
executable file
·187 lines (149 loc) · 5.78 KB
/
blacklist.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
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# oyster - a python-based jukebox and web-frontend
#
# Copyright (C) 2004 Benjamin Hanzelmann <ben@nabcos.de>,
# Stephan Windmüller <windy@white-hawk.de>,
# Stefan Naujokat <git@ethric.de>
#
# 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""
Allows the user to specify regex-lines for files
which should not be played at random
"""
__revision__ = 1
import cgi
import config
import cgitb
import os.path
import urllib
import re
cgitb.enable()
def print_blacklist ():
"Opens current blacklist and prints each line"
lineaffects = {}
blacklistmatchers = {}
if os.path.exists(myconfig['savedir'] + "blacklists/" + playlist):
blacklist = open(myconfig['savedir'] + "blacklists/" + playlist)
for line in blacklist.readlines():
blacklistmatchers[line[:-1]] = re.compile(line.rstrip())
line = line.replace(mediadir, '', 1)[:-1]
lineaffects[line] = 0
blacklist.close()
listfile = open(myconfig['savedir'] + "lists/" + playlist)
totalaffected = 0
# Count affected files for each rule
for line in listfile.readlines():
isblacklisted = 0
line = line.replace(mediadir, '', 1)[:-1]
for key in blacklistmatchers.keys():
if blacklistmatchers[key].search(line):
isblacklisted = 1
lineaffects[key] += 1
if isblacklisted:
totalaffected += 1
listfile.close()
blacklistlines = blacklistmatchers.keys()
blacklistlines.sort()
print "<table width='100%'>"
for line in blacklistlines:
escapedline = urllib.quote(line)
print "<tr><td width='60%'>" + line + "</td>"
print "<td width='25%' align='left'><a href='blacklist.py?" + \
"action=test&affects=" + escapedline + "'>Affects</a> (" + \
str(lineaffects[line]) + ")</td>"
print "<td width='15%' align='center'><a href='blacklist.py?" + \
"action=delete&affects=" + escapedline + \
"'>Delete</a></td></tr>"
print "</table>"
print "<p><strong>Total files affected:</strong> " + \
str(totalaffected) + "</p>"
def print_affects (affects):
"Shows all files, which are affected by a blacklist-rule"
affectresults = []
# Add all matching lines to results
if os.path.exists(myconfig['savedir'] + "lists/" + playlist):
listfile = open (myconfig['savedir'] + "lists/" + playlist, 'r')
for line in listfile.readlines():
line = line.replace(mediadir, '', 1)[:-1]
if re.compile(affects).search(line) != None:
affectresults.append(line)
listfile.close()
# Sort results alphabetically
if affectresults != []:
affectresults.sort()
common.results = affectresults
common.listdir('/', 0,'file2')
else:
print "<p>No songs match these rule.</p>"
def add_to_blacklist (affects):
"Appends a rule to the blacklist"
blacklist = open(savedir + "blacklists/" + playlist, 'a')
blacklist.write(affects + "\n")
blacklist.close()
def delete_from_blacklist (affects):
"removes a rule from the blacklist"
os.system ("cp \"" + savedir + "blacklists/" + playlist + "\" " + \
savedir + "blacklist.tmp")
blacklist = open(savedir + "blacklist.tmp")
newblacklist = open(savedir + "blacklists/" + playlist, 'w')
for line in blacklist.readlines():
if line[:-1] != affects:
newblacklist.write(line)
blacklist.close()
newblacklist.close()
os.unlink (savedir + "blacklist.tmp")
myconfig = config.get_config()
basedir = myconfig['basedir']
savedir = myconfig['savedir']
mediadir = myconfig['mediadir'][:-1]
form = cgi.FieldStorage()
playlist = config.get_playlist()
import common
common.navigation_header("Blacklists")
results = []
if form.has_key('affects') and form.has_key('action') \
and form['action'].value == 'test':
escaffects = cgi.escape(form['affects'].value)
else:
escaffects = ''
# Create form
print "<form method='post' action='blacklist.py' " + \
"enctype='application/x-www-form-urlencoded'>"
print "<fieldset class='searchform'>"
print "<legend class='searchform'>Add entries to blacklist</legend>"
print "<input id='searchfield' type='text' size='40' name='affects' value='" + escaffects + "'/>"
print "<input id='searchsubmit' type='submit' name='.submit' value='Go'/>"
print "<table id='searchoptions'><tr>"
print "<td><input type='radio' name='action' value='test' checked='checked'/> " + \
"Test only</td></tr>"
print "<tr><td><input type='radio' name='action' value='add'/> " + \
"Add to blacklist</td></tr>"
print "</table>"
print "</fieldset>"
print "</form>"
print "<p><a href='blacklist.py'>Show current blacklist</a></p>"
if form.has_key('action') and form.has_key('affects'):
if form['action'].value == 'test':
print_affects(form['affects'].value)
elif form['action'].value == 'add':
add_to_blacklist(form['affects'].value)
print_blacklist()
elif form['action'].value == 'delete':
delete_from_blacklist(form['affects'].value)
print_blacklist()
else:
print_blacklist()
print "</body></html>"