2021-01-28 04:04:04 +00:00
#!/usr/bin/env python3
2007-02-10 21:47:18 +00:00
###
2018-03-09 18:56:04 +00:00
# Copyright (c) 2006-2007 Dennis Kaarsemaker
# Copyright (c) 2008-2009 Terence Simpson
# Copyright (c) 2018- Krytarik Raido
2007-02-10 21:47:18 +00:00
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# 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.
#
###
2006-06-26 17:57:20 +00:00
2018-05-21 22:45:04 +00:00
import sys, sqlite3
2008-05-05 11:34:57 +00:00
# This needs to be set to the location of the commoncgi.py file
2009-10-12 18:26:35 +00:00
sys.path.append('/var/www/bot')
2007-02-10 21:47:18 +00:00
from commoncgi import *
2006-06-26 17:57:20 +00:00
2007-02-10 21:47:18 +00:00
### Variables
2018-05-21 22:45:04 +00:00
NUM_PER_PAGE = 50
2009-10-12 18:26:35 +00:00
# Directory containing the factoid database
datadir = '/home/bot/'
# Database filename (without the .db extention)
2018-05-21 22:45:04 +00:00
database = 'ubuntu'
2006-06-26 17:57:20 +00:00
2009-10-12 18:26:35 +00:00
### Nothing below this line should be edited unless you know what you're doing ###
2008-05-05 11:34:57 +00:00
2007-02-10 21:47:18 +00:00
databases = [x for x in os.listdir(datadir)]
2006-06-26 17:57:20 +00:00
2007-02-10 21:47:18 +00:00
# Initialize
2018-05-21 22:45:04 +00:00
order_url = 'popularity|DESC'
order_by = order_url.replace('|',' ')
page = 1
2007-02-10 21:47:18 +00:00
search = ''
factoids = []
total = 0
2006-06-26 17:57:20 +00:00
2010-03-29 08:02:17 +00:00
class Factoid:
2018-03-22 20:34:04 +00:00
def __init__(self, name, value, author, added, editor, edited, popularity):
self.name, self.value, self.author, self.added, self.editor, self.edited, self.popularity = \
name, value, author, added, editor, edited, popularity
2010-03-29 08:02:17 +00:00
class Log:
def __init__(self, author, added):
2018-05-21 22:45:04 +00:00
self.author, self.added = author, added
2010-03-29 08:02:17 +00:00
2007-02-10 21:47:18 +00:00
# Read POST
2018-05-21 22:45:04 +00:00
if 'db' in form and form['db'].value in databases:
2006-07-13 20:21:54 +00:00
database = form['db'].value
2018-03-09 18:56:04 +00:00
con = sqlite3.connect(os.path.join(datadir, '%s.db' % database))
2006-06-26 17:57:20 +00:00
cur = con.cursor()
2018-05-21 22:45:04 +00:00
try:
page = int(form['page'].value)
except:
pass
if 'order' in form and form['order'].value in ('added|DESC', 'added|ASC', 'name|DESC', 'name|ASC', 'popularity|DESC', 'popularity|ASC'):
order_url = form['order'].value
order_by = order_url.replace('|',' ')
2006-09-28 12:35:24 +00:00
if 'search' in form:
2007-02-10 21:47:18 +00:00
search = form['search'].value
2018-05-21 22:45:04 +00:00
2007-02-10 21:47:18 +00:00
# Select factoids
if search:
2018-05-21 22:45:04 +00:00
keys = utils.web.urlunquote(search).split()[:5]
qterms, values = '', []
2006-09-28 12:35:24 +00:00
for k in keys:
2018-05-21 22:45:04 +00:00
if qterms:
qterms += ' AND '
qterms += '(name LIKE ? OR value LIKE ? OR value LIKE ?)'
values.extend(['%%%s%%' % k.lower(), '%%%s%%' % k, '%%%s%%' % k.lower()])
2018-03-22 20:34:04 +00:00
cur.execute("SELECT name, value, author, added, editor, edited, popularity FROM facts WHERE name NOT LIKE '%%-also' AND %s ORDER BY %s LIMIT %d, %d" %
2018-05-21 22:45:04 +00:00
(qterms, order_by, NUM_PER_PAGE * (page - 1), NUM_PER_PAGE), values)
2015-04-04 19:21:13 +00:00
factoids = [Factoid(*x) for x in cur.fetchall()]
2018-05-21 22:45:04 +00:00
cur.execute("SELECT COUNT(*) FROM facts WHERE name NOT LIKE '%%-also' AND %s" % qterms, values)
2007-02-10 21:47:18 +00:00
total = cur.fetchall()[0][0]
2006-09-28 12:35:24 +00:00
else:
2021-06-13 23:56:04 +00:00
cur.execute("SELECT name, value, author, added, editor, edited, popularity FROM facts WHERE value NOT LIKE '%%<alias>%%' AND name NOT LIKE '%%-also' ORDER BY %s LIMIT %d, %d" %
2018-05-21 22:45:04 +00:00
(order_by, NUM_PER_PAGE * (page - 1), NUM_PER_PAGE))
2010-03-29 08:02:17 +00:00
factoids = [Factoid(*x) for x in cur.fetchall()]
2021-06-13 23:56:04 +00:00
cur.execute("SELECT COUNT(*) FROM facts WHERE value NOT LIKE '%%<alias>%%' AND name NOT LIKE '%%-also'")
2007-02-10 21:47:18 +00:00
total = cur.fetchall()[0][0]
# Pagination links
2021-06-13 23:04:04 +00:00
plink = ' <a href="factoids.cgi?db=%s&search=%s&order={}&page={}">{}</a>' % (database, utils.web.urlquote(search))
2018-05-21 22:45:04 +00:00
npages = int(math.ceil(float(total) / NUM_PER_PAGE))
2021-06-13 23:04:04 +00:00
print(' ·\n'.join(list(map(lambda x: plink.format(order_url, x, x) if x != page else str(x), range(1, npages+1)))))
2018-05-21 22:45:04 +00:00
print(' <br />Order by<br />');
2021-06-13 23:04:04 +00:00
print(' ·\n'.join([plink.format('name|ASC', 1, 'Name +'), plink.format('name|DESC', 1, 'Name -'),
plink.format('popularity|ASC', 1, 'Popularity +'), plink.format('popularity|DESC', 1, 'Popularity -'),
plink.format('added|ASC', 1, 'Date added +'), plink.format('added|DESC', 1, 'Date added -')]))
2018-05-21 22:45:04 +00:00
print('''\
<table style="border-collapse: collapse;">
2010-03-29 08:02:17 +00:00
<thead>
<tr>
2018-05-21 22:45:04 +00:00
<th style="width: 15%;">Factoid</th>
<th style="width: 68%;">Value</th>
<th style="width: 17%;">Author</th>
2010-03-29 08:02:17 +00:00
</tr>
</thead>
2018-02-22 10:45:04 +00:00
<tbody>''')
2007-02-10 21:47:18 +00:00
2018-05-21 22:45:04 +00:00
url_re = re.compile('(?P<url>(https?://|www\.)\S+)')
2007-02-10 21:47:18 +00:00
def q(x):
2018-05-21 22:45:04 +00:00
x = x.replace('&','&').replace('<','<').replace('>','>').replace('"','"')
2007-02-10 21:47:18 +00:00
return url_re.sub(link, x)
def link(match):
2018-05-21 22:45:04 +00:00
url = txt = match.group('url')
# if len(txt) > 30:
# txt = '%s…%s' % (txt[:20], txt[-10:])
2007-02-10 21:47:18 +00:00
return '<a href="%s">%s</a>' % (url, txt)
2006-06-26 17:57:20 +00:00
i = 0
2010-03-29 08:02:17 +00:00
for fact in factoids:
2018-05-21 22:45:04 +00:00
cur.execute("SELECT name FROM facts WHERE value LIKE ?", ('<alias>_%s' % fact.name,))
name = ' '.join([fact.name] + [x[0] for x in cur.fetchall()])
2018-03-09 18:56:04 +00:00
cur.execute("SELECT value FROM facts WHERE name = ?", ('%s-also' % fact.name,))
2018-05-21 22:45:04 +00:00
value = ' '.join([fact.value] + [x[0] for x in cur.fetchall()])
data = ["Added by %s" % fact.author[:fact.author.find('!')], "Date: %s" % fact.added[:fact.added.rfind('.')]]
2018-03-22 20:34:04 +00:00
if fact.editor:
data.extend(["Last edited by %s" % fact.editor[:fact.editor.find('!')], "Date: %s" % fact.edited[:fact.edited.rfind('.')]])
2018-05-21 22:45:04 +00:00
data.append("Requested %s times" % fact.popularity)
print('''\
<tr%s>
2010-03-29 08:02:17 +00:00
<td>%s</td>
<td>%s</td>
2018-05-21 22:45:04 +00:00
<td>%s</td>
</tr>''' % (' class="bg2"' if i % 2 else '', q(name), q(value), '<br />\n '.join(data)))
i += 1
2006-06-26 17:57:20 +00:00
2018-05-21 22:45:04 +00:00
print('''\
2010-03-29 08:02:17 +00:00
</tbody>
2018-02-22 10:45:04 +00:00
</table>''')
2006-06-26 17:57:20 +00:00
2007-02-10 21:47:18 +00:00
send_page('factoids.tmpl')