#!/usr/bin/env python3 ### # Copyright (c) 2006-2007 Dennis Kaarsemaker # Copyright (c) 2008-2009 Terence Simpson # Copyright (c) 2018- Krytarik Raido # # 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. # ### import sys, sqlite3 # This needs to be set to the location of the commoncgi.py file sys.path.append('/var/www/bot') from commoncgi import * ### Variables NUM_PER_PAGE = 50 # Directory containing the factoid database datadir = '/home/bot/' # Database filename (without the .db extention) database = 'ubuntu' ### Nothing below this line should be edited unless you know what you're doing ### databases = [x for x in os.listdir(datadir)] # Initialize order_url = 'popularity|DESC' order_by = order_url.replace('|',' ') page = 1 search = '' factoids = [] total = 0 class Factoid: 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 class Log: def __init__(self, author, added): self.author, self.added = author, added # Read POST if 'db' in form and form['db'].value in databases: database = form['db'].value con = sqlite3.connect(os.path.join(datadir, '%s.db' % database)) cur = con.cursor() 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('|',' ') if 'search' in form: search = form['search'].value # Select factoids if search: keys = utils.web.urlunquote(search).split()[:5] qterms, values = '', [] for k in keys: if qterms: qterms += ' AND ' qterms += '(name LIKE ? OR value LIKE ? OR value LIKE ?)' values.extend(['%%%s%%' % k.lower(), '%%%s%%' % k, '%%%s%%' % k.lower()]) 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" % (qterms, order_by, NUM_PER_PAGE * (page - 1), NUM_PER_PAGE), values) factoids = [Factoid(*x) for x in cur.fetchall()] cur.execute("SELECT COUNT(*) FROM facts WHERE name NOT LIKE '%%-also' AND %s" % qterms, values) total = cur.fetchall()[0][0] else: cur.execute("SELECT name, value, author, added, editor, edited, popularity FROM facts WHERE value NOT LIKE '%%%%' AND name NOT LIKE '%%-also' ORDER BY %s LIMIT %d, %d" % (order_by, NUM_PER_PAGE * (page - 1), NUM_PER_PAGE)) factoids = [Factoid(*x) for x in cur.fetchall()] cur.execute("SELECT COUNT(*) FROM facts WHERE value NOT LIKE '%%%%' AND name NOT LIKE '%%-also'") total = cur.fetchall()[0][0] # Pagination links plink = ' {}' % (database, utils.web.urlquote(search)) npages = int(math.ceil(float(total) / NUM_PER_PAGE)) print(' ·\n'.join(list(map(lambda x: plink.format(order_url, x, x) if x != page else str(x), range(1, npages+1))))) print('
Order by
'); 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 -')])) print('''\ ''') url_re = re.compile('(?P(https?://|www\.)\S+)') def q(x): x = x.replace('&','&').replace('<','<').replace('>','>').replace('"','"') return url_re.sub(link, x) def link(match): url = txt = match.group('url') # if len(txt) > 30: # txt = '%s…%s' % (txt[:20], txt[-10:]) return '%s' % (url, txt) i = 0 for fact in factoids: cur.execute("SELECT name FROM facts WHERE value LIKE ?", ('_%s' % fact.name,)) name = ' '.join([fact.name] + [x[0] for x in cur.fetchall()]) cur.execute("SELECT value FROM facts WHERE name = ?", ('%s-also' % fact.name,)) 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('.')]] if fact.editor: data.extend(["Last edited by %s" % fact.editor[:fact.editor.find('!')], "Date: %s" % fact.edited[:fact.edited.rfind('.')]]) data.append("Requested %s times" % fact.popularity) print('''\ ''' % (' class="bg2"' if i % 2 else '', q(name), q(value), '
\n '.join(data))) i += 1 print('''\
Factoid Value Author
%s %s %s
''') send_page('factoids.tmpl')