From bcb385d3e82650b889dbff13a1054be8a3b8855e Mon Sep 17 00:00:00 2001 From: Pratyush Desai Date: Sat, 30 Nov 2024 03:14:39 +0530 Subject: [PATCH] Drug lookup added to listdose listdose now supports filtering with drug Signed-off-by: Pratyush Desai --- plugin.py | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/plugin.py b/plugin.py index ce88f09..34ce165 100644 --- a/plugin.py +++ b/plugin.py @@ -260,23 +260,33 @@ class Tripsit(callbacks.Plugin): else: irc.error(f'No doses saved for {nick}') - @wrap(['positiveInt']) - def listdose(self, irc, msg, args, history): - """ - - retrieves your last logged doses + @wrap([getopts({'drug': 'something'}), 'positiveInt']) + def listdose(self, irc, msg, args, opts, history): + """[--drug ] + Retrieves your last logged doses, optionally filtered by drug. """ if history > 20: - irc.error("you can't retrieve more than 20 doses") + irc.error("You can't retrieve more than 20 doses.") return + + opts = dict(opts) + drug_filter = opts.get('drug') nick = msg.nick + if nick in self.db: + doses = self.db[nick]['doses'] + if drug_filter: + doses = [dose for dose in doses if dose['drug'].lower() == drug_filter.lower()] + + if len(doses) == 0: + irc.error(f"No doses found for drug '{drug_filter}'.") + return + try: - rangecheck = self.db[nick]['doses'][-int(history)] - irc.reply(f"Your last {history} doses logged are:", private=True) - for number in range(history,0,-1): - lastdose = self.db[nick]['doses'][-int(number)] + irc.reply(f"Your last {history} dose(s) are:", private=True) + for number in range(history, 0, -1): + lastdose = doses[-number] dose = lastdose['dose'] drug = lastdose['drug'] method = lastdose['method'] @@ -286,17 +296,22 @@ class Tripsit(callbacks.Plugin): time = datetime.datetime.now(tz=tz) since_dose = time - dose_time since_dose_seconds = since_dose.total_seconds() + if number == 1: number = "The" else: number = num2words(number, to='ordinal') - re = utils.str.format("::> %i last dose: Amount: %s of \x02%s\x0F via %s | datetime: %s %s | timedelta %T ", number, dose, drug, method, dose_time.strftime("%c"), timezone, since_dose_seconds) + + re = utils.str.format( + "::> %s last dose: Amount: %s of \x02%s\x0F via %s | datetime: %s %s | timedelta %T", + number, dose, drug, method, dose_time.strftime("%c"), timezone, since_dose_seconds + ) irc.reply(re, private=True) except IndexError: - irc.error("You haven't logged that many doses") + irc.error("You haven't logged that many doses.") return else: - irc.error(f'No doses saved for {nick}') + irc.error(f"No doses saved for {nick}.") @wrap(["something"]) def grepdose(self, irc, msg, args, drug):