From a86ee5e33d269104ed57730062e3f3ab4dbdd963 Mon Sep 17 00:00:00 2001 From: mrhanky Date: Tue, 16 May 2017 13:44:39 +0200 Subject: [PATCH] =?UTF-8?q?n=C3=B6p?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nxy/migrate.py | 23 ++++++++++++++++------ nxy/plugins/coins.py | 6 ++---- nxy/plugins/ctcp.py | 8 ++------ nxy/plugins/futures.py | 26 ++++++++++++------------- nxy/utils.py | 43 ++++++++++++++++++++++-------------------- 5 files changed, 56 insertions(+), 50 deletions(-) diff --git a/nxy/migrate.py b/nxy/migrate.py index da72c67..0150ecb 100644 --- a/nxy/migrate.py +++ b/nxy/migrate.py @@ -1,12 +1,23 @@ # -*- coding: utf-8 -*- import sqlite3 +import os + +DATA = 'data' +MIGRATE = os.path.join(DATA, 'migrate') + +db = sqlite3.connect(os.path.join(DATA, 'nxy.db')) -def mcmaniac(): - with open('data/mcmaniac.txt', 'r') as fp: - items = [(l.replace('\n', ''),) for l in fp] - con = sqlite3.connect('data/nxy.db') - con.executemany('insert into mcmaniacs (item) values (?)', items) - con.commit() +def read_file(file): + with open(os.path.join(MIGRATE, file), 'r') as fp: + return [(l.replace('\n', ''),) for l in fp] +def batch_insert(table, column, data): + db.executemany('insert into {table} ({column}) values (?)'. + format(table=table, column=column), data) + db.commit() + + +batch_insert('mcmaniacs', 'item', read_file('mcmaniac.txt')) +batch_insert('yiffs', 'item', read_file('yiff.txt')) diff --git a/nxy/plugins/coins.py b/nxy/plugins/coins.py index 398bbc3..c34e0a6 100644 --- a/nxy/plugins/coins.py +++ b/nxy/plugins/coins.py @@ -28,7 +28,5 @@ class Coins(Plugin): 'High: {bold}{color}{green}${high:,.2f}{reset} - ' 'Low: {bold}{color}{maroon}${low:,.2f}{reset} - ' 'Volume: {bold}฿{volume:,.2f}', - last=float(data['last']), - high=float(data['high']), - low=float(data['low']), - volume=float(data['volume'])) + last=float(data['last']), high=float(data['high']), + low=float(data['low']), volume=float(data['volume'])) diff --git a/nxy/plugins/ctcp.py b/nxy/plugins/ctcp.py index f381b18..3f8c08a 100644 --- a/nxy/plugins/ctcp.py +++ b/nxy/plugins/ctcp.py @@ -55,12 +55,8 @@ class CTCP(Plugin): unit = 'ms' else: unit = 's' - reply = '{delta:.3f} {unit}'.format( - unit=unit, # 'ms' if delta < 0 else 's', - delta=delta) - return fmt('{bold}[PING]{reset} {nick}: {text}', - nick=nick, - text=reply) + reply = '{delta:.3f} {unit}'.format(unit=unit, delta=delta) + return fmt('{bold}[PING]{reset} {nick}: {text}', nick=nick, text=reply) @command async def finger(self, mask: IrcString, channel: IrcString, diff --git a/nxy/plugins/futures.py b/nxy/plugins/futures.py index 0221f0d..1295137 100644 --- a/nxy/plugins/futures.py +++ b/nxy/plugins/futures.py @@ -21,9 +21,6 @@ class Futures(DatabasePlugin): def __init__(self, bot: IrcBot): super().__init__(bot) - self.restore() - - def restore(self): self.cur.execute('select * from timers') for res in self.cur.fetchall(): delta = res['until'] - datetime.utcnow() @@ -33,7 +30,9 @@ class Futures(DatabasePlugin): @command def timer(self, mask: IrcString, channel: IrcString, args: DocOptDict): - """Timer command. + """Timer command, delay can be: s(econd), m(min), h(our), w(week), + mon(th), y(ear) + %%timer ... """ delay = args[''] @@ -42,14 +41,15 @@ class Futures(DatabasePlugin): date = datetime.utcnow() + delta message = ' '.join(args['']) self.cur.execute('''insert into timers (mask, channel, message, - delay, until) values (?, ?, ?, ?, ?)''', - [mask.nick, channel, message, delay, date]) + delay, until) values (?, ?, ?, ?, ?)''', [mask.nick, channel, + message, delay, date]) self.con.commit() - asyncio.ensure_future(self._timer( - mask, channel, delta, message, delay, self.cur.lastrowid)) + asyncio.ensure_future(self._timer(mask, channel, delta, message, + delay, self.cur.lastrowid)) + self.bot.notice(mask.nick, 'Timer in {delay} set: {message}' + .format(delay=delay, message=message)) else: - self.bot.notice(mask.nick, 'Invalid delay "{delay}" for "timer"' - .format(delay=args[''])) + self.bot.privmsg(channel, 'Invalid timer delay') async def _timer(self, mask: IrcString, channel: IrcString, delta: timedelta, message: str, delay: str, row_id: int): @@ -60,10 +60,8 @@ class Futures(DatabasePlugin): seconds = delta.total_seconds() if seconds > 0: await asyncio.sleep(seconds) - text = fmt('{bold}[Reminder]{reset} {nick}: {message} ({delay})', - message=message, - delay=delay, - nick=mask.nick) + text = fmt('{bold}[Timer]{reset} {nick}: {message} ({delay})', + message=message, delay=delay, nick=mask.nick) self.bot.privmsg(channel, text) self.cur.execute('delete from timers where id = ?', [row_id]) self.con.commit() diff --git a/nxy/utils.py b/nxy/utils.py index ebfad4f..98c345b 100644 --- a/nxy/utils.py +++ b/nxy/utils.py @@ -6,22 +6,22 @@ from pprint import pprint # @formatter:off COLORS = dict( - white='00', # white - black='01', # black - blue='02', # blue (navy) - green='03', # green - red='04', # red - maroon='05', # brown (maroon) - purple='06', # purple - orange='07', # orange (olive) - yellow='08', # yellow - ltgreen='09', # light green (lime) - teal='10', # teal (a green/blue cyan) - ltcyan='11', # light cyan (cyan / aqua) - ltblue='12', # light blue (royal) - pink='13', # pink (light purple / fuchsia) - grey='14', # grey - ltgrey='15', # light grey (silver) + white=0, # white + black=1, # black + blue=2, # blue (navy) + green=3, # green + red=4, # red + maroon=5, # brown (maroon) + purple=6, # purple + orange=7, # orange (olive) + yellow=8, # yellow + ltgreen=9, # light green (lime) + teal=10, # teal (a green/blue cyan) + ltcyan=11, # light cyan (cyan / aqua) + ltblue=12, # light blue (royal) + pink=13, # pink (light purple / fuchsia) + grey=14, # grey + ltgrey=15, # light grey (silver) ) FORMATTING = dict( @@ -45,22 +45,25 @@ def fmt(__text: str, **kwargs) -> str: def time_delta(text: str) -> timedelta: - match = re.match(r'(\d+)([smhdwy])', text) + match = re.match(r'(\d+)(s|m|h|mon|w|y)', text) if match: num, unit = match.groups() num = int(num) if unit == 's': unit = 'seconds' elif unit == 'm': - unit = 'months' + unit = 'minutes' elif unit == 'h': unit = 'hours' elif unit == 'd': unit = 'days' elif unit == 'w': unit = 'weeks' - elif unit == 's': - unit = 'years' + elif unit == 'mon': + unit = 'weeks' + num *= 4 + elif unit == 'y': + unit = 'weeks' num *= 52 return timedelta(**{unit: num})