From 4f9ff0feb1caca819ce7778eed3e98cb84e5332e Mon Sep 17 00:00:00 2001 From: mogad0n Date: Fri, 18 Sep 2020 10:47:25 +0530 Subject: [PATCH] initial implementation of historical dose logging and local time --- plugin.py | 100 ++++++++++++++++++++++++++++++++--------------- requirements.txt | 1 + 2 files changed, 70 insertions(+), 31 deletions(-) create mode 100644 requirements.txt diff --git a/plugin.py b/plugin.py index 44aca37..a840e0a 100644 --- a/plugin.py +++ b/plugin.py @@ -36,6 +36,10 @@ import requests import pickle import sys import datetime +import pytz + +ct = datetime.datetime.now(tz=tz) +import datetime import time try: from supybot.i18n import PluginInternationalization @@ -149,6 +153,18 @@ class Tripsit(callbacks.Plugin): combo = wrap(combo, [("something"), ("something")]) + def set(self, irc, msg, args, timezone): + """ + + Sets location for your current ident@host to + for eg. America/Chicago + """ + + ih = msg.prefix.split("!")[1] + self.db[ih] = timezone + irc.replySuccess() + set = wrap(set, ["text"]) + @wrap(["something", "something", optional("something"), optional("something")]) def idose(self, irc, msg, args, dose, name, method, ago): """ [] [] @@ -159,6 +175,7 @@ class Tripsit(callbacks.Plugin): r = requests.get(url_drug, params={"name": name}).json() found_method = False onset = None + self.db[msg.nick] = [] if not r['err']: drug = r['data'][0] drug_name = drug['pretty_name'] @@ -187,54 +204,75 @@ class Tripsit(callbacks.Plugin): method = method.title() drug_and_method = "%s via %s" % (drug_and_method, method) else: - method = 'Undefined/Probably boofed' + method = 'Undefined' - - - time = datetime.datetime.utcnow() - if not ago: - self.db[msg.nick] = {'type': 'idose' ,'time': str(time), 'dose': dose, 'drug': name, 'method': method } - re = f" You dosed {dose} of {drug_and_method} at {str(time)} UTC" - if not onset == None: - re += f". You should start feeling effects {onset} from now" + ih = msg.prefix.split("!")[1] + if self.db[ih] is not None: + timezone = self.db[ih] + tz = pytz.timezone(timezone) + time = datetime.datetime.now(tz=tz) else: + timezone = 'UTC' + time = datetime.datetime.utcnow() + if len(ago) == 4: hours = int(ago[0:2]) minutes = int(ago[2:4]) dose_td = datetime.timedelta(hours=hours, minutes=minutes) - time_dosed = time - dose_td - self.db[msg.nick] = {'type': 'hdose', 'time': str(time), 'time_dosed': str(time_dosed), 'dose': dose, 'drug': name, 'method': method } - re = f" You dosed {dose} of {drug_and_method} at {str(time_dosed)} UTC, {str(hours)} hours and {str(minutes)} minutes ago" - if not onset == None: - re += f". You should have/will start feeling effects {onset} from {str(time_dosed)} UTC" + dose_td_s = dose_td.total_seconds() + time = time - dose_td + + + log_dict = {'time': time, 'timezone': timezone, 'dose': dose, 'drug': name, 'method': method } + + nickdb = self.db[msg.nick] + if nickdb is not None and len(nickdb) < 10: + # if dose db exists and there are less that 10 + nickdb.append(log_dict) + elif len(nickdb) == 10: + # how can this be greater than it has to be equal to 10 + nickdb.pop(0) + elif nickdb is None: + # dose db doesnt exist. Initialise list + nickdb = [log_dict] + + if dose_td is None: + re = utils.str.format("You dosed %s of %s at %t, %s", dose, drug_and_method, time, timezone) + if onset is not None: + re += utils.str.format(". You should start feeling effects %s from now", onset) + else: + re = utils.str.format("You dosed %s of %s at %t, %s %T", dose, drug_and_method, time, timezone, dose_td_s) + if onset is not None: + re += utils.str.format(". You should have/will start feeling effects %s from/after dosing", onset) irc.reply(re) - - def lastdose(self, irc, msg, args): - """This command takes no arguments + @wrap([optional('positiveInt')]) + def lastdose(self, irc, msg, args, history): + """ are digits upto but not including 10 retrieves your last logged dose """ if msg.nick in self.db: - lastdose = self.db[msg.nick] - time = datetime.datetime.utcnow() - if lastdose['type'] == 'idose': - dose_time = dateutil.parser.isoparse(lastdose['time']) - elif lastdose['type'] == 'hdose': - dose_time = dateutil.parser.isoparse(lastdose['time_dosed']) + if history: + lastdose = self.db[msg.nick][history] + else: + history = 0 + lastdose = self.db[msg.nick] + dose = lastdose['dose'] + drug = lastdose['drug'] + timezone = lastdose['timezone'] + if timezone == 'UTC': + time = datetime.datetime.utcnow() + else: + tz = pytz.timezone(timezone) + time = datetime.datetime.now(tz=tz) + dose_time = dateutil.parser.isoparse(lastdose['time']) since_dose = time - dose_time since_dose_seconds = since_dose.total_seconds() - since_dose_formatted = utils.str.format('%T', since_dose_seconds) - re = f"You last dosed {lastdose['dose']} of {lastdose['drug'] }at {dose_time} UTC, {since_dose_formatted} ago" + re = utils.str.format("Your #%i last dose was %s of %s at %t %s, %T ago", history, dose, drug, dose_time, since_dose_seconds) irc.reply(re) else: irc.error(f'No last dose saved for {msg.nick}') - lastdose = wrap(lastdose) - - - - - Class = Tripsit diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..7c141b3 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +pytz==2020.1