3
# Copyright (c) 2006,2007 Dennis Kaarsemaker
4
# Copyright (c) 2008, 2009 Terence Simpson
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of version 2 of the GNU General Public License as
8
# published by the Free Software Foundation.
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
18
# This needs to be set to the location of the commoncgi.py file
19
sys.path.append('/var/www/bot')
20
from commoncgi import *
24
# Directory containing the factoid database
25
datadir = '/home/bot/'
26
# Database filename (without the .db extention)
27
default_database = 'ubuntu'
29
### Nothing below this line should be edited unless you know what you're doing ###
31
databases = [x for x in os.listdir(datadir)]
34
database = default_database
35
order_by = 'popularity DESC'
42
def __init__(self, name, value, author, added, popularity):
43
self.name, self.value, self._author, self._added, self.popularity = (name, value, author, added, popularity)
47
if '!' in self._author:
48
return self._author[:self._author.find('!')]
53
if '.' in self._added:
54
return self._added[:self._added.find('.')]
65
def __init__(self, author, added):
66
self._author, self._added = (author, added)
70
if '!' in self._author:
71
return self._author[:self._author.find('!')]
76
if '.' in self._added:
77
return self._added[:self._added.find('.')]
82
database = form['db'].value
83
if database not in databases:
84
database = default_database
85
con = sqlite.connect(os.path.join(datadir, database + '.db'))
88
try: page = int(form['page'].value)
92
if form['order'].value in ('added DESC', 'added ASC', 'name DESC', 'name ASC', 'popularity DESC','popularity ASC'):
93
order_by = form['order'].value
95
search = form['search'].value
99
keys = [urllib2.unquote(x.strip()) for x in search.split() if len(x.strip()) >=2][:5]
102
query1 = "SELECT name, value, author, added, popularity FROM facts WHERE name NOT LIKE '%-also' AND ("
103
query2 = "SELECT COUNT(name) FROM facts WHERE "
106
k = repr('%' + k + '%')
110
query1 += "name LIKE %s OR VAlUE LIKE %s" % (k, k)
111
query2 += "name LIKE %s OR VAlUE LIKE %s" % (k, k)
114
query1 += ') ORDER BY %s LIMIT %d, %d' % (order_by, NUM_PER_PAGE*page, NUM_PER_PAGE)
116
factoids = [Factoid(x) for x in cur.fetchall()]
118
total = cur.fetchall()[0][0]
120
cur.execute("SELECT name, value, author, added, popularity FROM facts WHERE value NOT LIKE '<alias>%%' AND name NOT LIKE '%%-also' ORDER BY %s LIMIT %d, %d" % (order_by, page*NUM_PER_PAGE, NUM_PER_PAGE))
121
factoids = [Factoid(*x) for x in cur.fetchall()]
122
cur.execute("""SELECT COUNT(*) FROM facts WHERE value NOT LIKE '<alias>%%'""")
123
total = cur.fetchall()[0][0]
126
npages = int(math.ceil(total / float(NUM_PER_PAGE)))
128
for i in range(npages):
129
print '<a href="factoids.cgi?db=%s&search=%s&order=%s&page=%s">%d</a> ·' % (database, search, order_by, i, i+1)
131
print '<br />Order by<br />·';
132
print ' <a href="factoids.cgi?db=%s&search=%s&order=%s&page=0">%s</a> ·' % (database, search, 'name ASC', 'Name +')
133
print ' <a href="factoids.cgi?db=%s&search=%s&order=%s&page=0">%s</a> ·' % (database, search, 'name DESC', 'Name -')
134
print ' <a href="factoids.cgi?db=%s&search=%s&order=%s&page=0">%s</a> ·' % (database, search, 'popularity ASC', 'Popularity +')
135
print ' <a href="factoids.cgi?db=%s&search=%s&order=%s&page=0">%s</a> ·' % (database, search, 'popularity DESC', 'Popularity -')
136
print ' <a href="factoids.cgi?db=%s&search=%s&order=%s&page=0">%s</a> ·' % (database, search, 'added ASC', 'Date added +')
137
print ' <a href="factoids.cgi?db=%s&search=%s&order=%s&page=0">%s</a> ·' % (database, search, 'added DESC', 'Date added -')
140
<table cellspacing="0">
143
<th style="width: 10%;">Factoid</th>
144
<th style="width: 70%;">Value</th>
145
<th style="width: 20%;">Author</th>
150
url_re = re.compile('(?P<url>(https?://\S+|www\S+))')
152
x = str(x).replace('&','&').replace('<','<').replace('>','>').replace('\n','<br />')
153
return url_re.sub(link, x)
155
url = match.group('url')
158
txt = txt[:20] + '…' + txt[-10:]
159
return '<a href="%s">%s</a>' % (url, txt)
162
for fact in factoids:
164
cur.execute("SELECT value FROM facts WHERE name = %s", fact.name + '-also')
165
more = cur.fetchall()
167
name += ' $hr$' + ' $hr$'.join([x[0] for x in more])
168
cur.execute("SELECT name FROM facts WHERE value LIKE %s", '<alias> ' + fact.name)
169
name += ' \n' + ' \n'.join([x[0] for x in cur.fetchall()])
170
data = [ q(x) for x in fact ]
171
cur.execute("SELECT author, added FROM log WHERE name = %s ORDER BY id DESC LIMIT 1", fact.name)
172
edit = [Log(*x) for x in cur.fetchall()]
173
# edit = [Log(author, added) for (author, added) in cur.fetchall()]
176
data[3] += "<br />Last edited by %s<br />Last modified: %s" % (q(log.author), q(log.added))
178
data[3] += "<br />Never edited"
180
sys.stdout.write(' <tr')
181
if i % 2: sys.stdout.write(' class="bg2"')
188
Requested %s times</td>
189
</tr>''' % tuple(data)
195
send_page('factoids.tmpl')