Skip to content
This repository has been archived by the owner on Jun 2, 2023. It is now read-only.

Commit

Permalink
Improve db responses
Browse files Browse the repository at this point in the history
  • Loading branch information
rossjrw committed Jun 21, 2019
1 parent 4acfa1b commit ee2cd2e
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 19 deletions.
2 changes: 2 additions & 0 deletions commands/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class kill:
@classmethod
def command(cls, irc_c, msg, cmd):
msg.reply(kill_bye())
irc_c.RAW("QUIT See you on the other side")
irc_c.client.die()

class join:
Expand Down Expand Up @@ -52,6 +53,7 @@ def command(cls, irc_c, msg, cmd):
# reboot the bot completely
if msg.nick == 'Croquembouche':
msg.reply("Rebooting...")
irc_c.RAW("QUIT Rebooting, will be back soon!")
os.execl(sys.executable, sys.executable, *sys.argv)
else:
msg.reply("Only Croquembouche can do that.")
Expand Down
18 changes: 15 additions & 3 deletions commands/prop.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
def prop_print(text):
print("[{}] {}".format(nickColor("Propagation"), text))

def chunks(array, length):
for i in range(0, len(array), length):
yield array[i:i + length]

class propagate:
@classmethod
def command(cls, irc_c, msg, cmd):
Expand Down Expand Up @@ -62,7 +66,15 @@ def get_wiki_data_for(cls, irc_c, urls, **kwargs):
irc_c.db._driver.add_article(article, commit=False)
reply("Done!")
irc_c.db._driver.commit()
if urls[0] == 'attribution-metadata':
metadata.external(irc_c)

def chunks(array, length):
for i in range(0, len(array), length):
yield array[i:i + length]
class metadata:
@classmethod
def command(cls, irc_c, msg, cmd):
"""Gets the attribution metadata."""
metadata.external(irc_c)

@staticmethod
def external(irc_c):
pass
15 changes: 10 additions & 5 deletions commands/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ def command(cls, irc_c, msg, cmd):
"be specified")
for regex in cmd.getarg('regex'):
try:
regexes.append(re.compile(regex))
re.compile(regex)
# don't append the compiled - SQL doesn't like that
regexes.append(regex)
except re.RegexError:
raise CommandError("'{}' isn't a valid regular expression"
.format(search))
Expand Down Expand Up @@ -256,7 +258,7 @@ def command(cls, irc_c, msg, cmd):
"\"; ")
if len(regexes) > 0:
verbose += ("matching the regex /" +
"/ & /".join([r.pattern for r in regexes]) +
"/ & /".join(regexes) +
"/; ")
if parents is not None:
verbose += ("whose parent page is '" +
Expand Down Expand Up @@ -344,8 +346,11 @@ def command(cls, irc_c, msg, cmd):
return
pages = [irc_c.db._driver.get_article_info(p['id']) for p in pages]
if len(pages) > 1:
msg.reply("{} results: {}".format(
len(pages), ", ".join([p['title'] for p in pages])))
msg.reply("{} results: {}".format(len(pages), ", ".join(
["\x02[{}]\x0F {}".format(i+1,p['title']) for i,p in enumerate(pages)]
)))
if len(pages) > 3:
return
if len(pages) == 0:
# len args is 2, not 1, because --select is always present
if len(cmd.args) == 2 and len(cmd.args['root']) != 0:
Expand All @@ -365,7 +370,7 @@ def command(cls, irc_c, msg, cmd):
return
for page in pages:
msg.reply(
"\x02{}\x0F · {} · {} · {} · {}".format(
"{} · {} · {} · {} · {}".format(
("\x02{}\x0F: {}".format(page['scp-num'],page['title'])
if 'scp' in page['tags'] else
"\x02{}\x0F".format(page['title'])),
Expand Down
11 changes: 6 additions & 5 deletions docs/help/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,15 @@ <h2 id="help">.help <div class="jarvis">J</div><div class="helen">H</div></h2>
<p>Provides a link to both this page and <a href="https://github.com/rossjrw/tars/wiki/Quick-Commands">Quick Commands</a>.</p>

<h2 id="search">.search, .sea, .s <div class="jarvis">J</div><div class="helen">H</div></h2>
<p>Searches the wiki for pages, and provides URLs and basic info about the page that matches your search criteria.</p>
<p>Searches the wiki for pages, and provides URLs and basic info about the page that matches your search criteria. Searching is never case-sensitive.</p>
<p><code>.search [title] [--regex regex] [--tags tags] [--author author] [--rating rating] [--created created] [--parent parent] [--fullname] [--summary] [--select type number offset]</code></p>
<ul>
<li><b>[title]</b>
<br> Search for pages whose title contains all of these words. Words are space-separated. If neither <b>-f</b> nor <b>-x</b> are present, anything wrapped in quotemarks (<code>"</code>) will be treated as a single word. Not case sensitive. If you leave <b>title</b> empty, then it will match all pages, and you'll need to specify more criteria.
<br> Search for pages whose title contains all of these words. Words are space-separated. If neither <b>-f</b> nor <b>-x</b> are present, anything wrapped in quotemarks (<code>"</code>) will be treated as a single word. If you leave <b>title</b> empty, then it will match all pages, and you'll need to specify more criteria.
If you actually need to search for quotemarks, escape them with a backslash - e.g. <code>.s \"The Administrator\"</code>.
</li>
<li><b>--regex, -x [regex]</b><br>
Filter pages by a regular expression. You may use more than one regex in a single search, still delimited by a space. If you want to include a literal space in the regex, you should either wrap the whole regex in quotes or use <code>\s</code> instead. Not case sensitive.
Filter pages by a regular expression. You may use more than one regex in a single search, still delimited by a space. If you want to include a literal space in the regex, you should either wrap the whole regex in quotes or use <code>\s</code> instead.
</li>
<li><b>--tags, --tagged, --tag, -t [tags]</b><br>
Filter pages by tags. <code>+scp</code> means pages tagged scp. <code>-scp</code> means pages not tagged scp. If you don't put either <code>+</code> or <code>-</code>, TARS will assume you meant to add a <code>+</code>.
Expand Down Expand Up @@ -258,9 +258,10 @@ <h2 id="lastcreated">.lastcreated, .lc, .l, .recent <div class="jarvis">J</div><
<p>Example: <code>.lc -a Croquembouche</code> - <em>shows the last 3 articles posted by Croquembouche.</em></p>

<h2 id="regexsearch">.regexsearch, .regexsea, .rsea, .rs</h2>
<p>Exactly the same as <a href="#search"><b><code>search</code></b></a>, except your search terms are parsed as regular expressions.</p>
<p>Exactly the same as <a href="#search"><b><code>search</code></b></a>, except your search terms are parsed as regular expressions. Not case-sensitive.</p>
<p>Example: <code>.rs ^SCP- -t -scp</code> - <em>searches for articles starting with "SCP-" but that are not tagged 'scp'.</em></p>

<p>Example: <code>.rs ^((?!the).)*$</code> - <em>searches for articles that don't contain 'the'.</em></p>

<h2 id="showmore">.showmore, .sm <div class="jarvis">J</div><div class="helen">H</div></h2>
<p>When TARS presents you with a list of articles to pick from, use this command to pick which one you want to see.</p>
<p>Example: <code>.sm 2</code> - <em>pick the 2nd article from the list.</em></p>
Expand Down
27 changes: 21 additions & 6 deletions helpers/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
from pypika import MySQLQuery, Table, Field
from pprint import pprint

sqlite3.enable_callback_tracebacks(True)

def dbprint(text, error=False):
bit = "[\x1b[38;5;108mDatabase\x1b[0m] "
if error:
Expand Down Expand Up @@ -51,8 +53,11 @@ def norm(thing):

def _regexp(expr, item):
"""For evaluating db strings against a given regex."""
reg = re.compile(expr)
return reg.search(item) is not None
return re.search(expr, item, re.IGNORECASE) is not None

def _glob(expr, item):
"""For evaluating db strings against a given string."""
return expr.lower() in item.lower()

# mark this file as the driver instead of pyaib.dbd.sqlite
# also set by db.backend in the config
Expand All @@ -71,6 +76,7 @@ def __init__(self, config):
self._create_database()
self.conn.row_factory = sqlite3.Row
self.conn.create_function("REGEXP", 2, _regexp)
self.conn.create_function("GLOB", 2, _glob)

def commit(self):
"""Just commits the database.
Expand Down Expand Up @@ -666,7 +672,11 @@ def get_article_info(self, id):
result = c.fetchone()
for column in result.keys():
page[column] = result[column]
page['fullname'] = ":".join([page['category'], page['url']])
# generate the fullname from category:url
if page['category'] == '_default':
page['fullname'] = page['url']
else:
page['fullname'] = ":".join([page['category'], page['url']])
# now get authors and tags
c.execute('''
SELECT tag FROM articles_tags WHERE article_id=?
Expand Down Expand Up @@ -724,20 +734,25 @@ def get_articles(self, searches, selection):
q = q.where(articles.date_posted >= search['term']['min'])
elif search['type'] == 'author':
# need to query articles_authors
au_q = MySQLQuery()
# make a subquery!
pass
elif search['type'] == 'tags':
# need to query articles_tags
pass
elif search['type'] == None:
q = q.where(
(articles.title.like('%{}%'.format(search['term'])))
(articles.title.like(search['term']))
| (articles.scp_num == search['term'])
)
elif search['type'] == 'regex':
pass

q = q.where(articles.title.regex(search['term']))
# query complete
print(str(q))
# make the query sqlite-compatible
# like --> "glob" which is a custom function
q = str(q).replace(" LIKE "," GLOB ").replace(" REGEX "," REGEXP ")
print(str(q))
c = self.conn.cursor()
c.execute(str(q))
return c.fetchall()
1 change: 1 addition & 0 deletions helpers/greetings.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def acronym_gen():
"!These !Are !Really !Something.",
"!TARS !Acronym !Repeating !Successfully",
"!Turnt !At #!Romanticpenthouse!Suite",
"!T!A!R!SPWTCOTTTADC",
]
last_response = []
while True:
Expand Down

0 comments on commit ee2cd2e

Please sign in to comment.