def egg_calculator(text): """<time> - Parses dragonvalebreedingguide.com for a list of possible dragons based on the incubation time. Enter the time as 5 hours, 30 minutes. For upgraded incubation times put 'upgrade' at the front of the time length""" time = "" time2 = "" if text.lower().startswith("upgrade"): timer = text.replace("upgrade", "") time2 = time_parse(timer.strip()) if not time2: return "invalid time format" else: timer = text time = time_parse(timer.strip()) if not time: return "invalid time format" params = { 'time': time, 'time2': time2, 'avail': 1 } r = requests.get(egg_calc_url, params=params, timeout=5) soup = BeautifulSoup(r.text) dragons = [] for line in soup.findAll('td', {'class': 'views-field views-field-title'}): dragons.append(line.text.replace("\n", "").strip()) return ", ".join(dragons)
def test_time_parse(): from cloudbot.util.timeparse import time_parse assert time_parse('1:24') == 84 assert time_parse(':22') == 22 assert time_parse('1 minute, 24 secs') == 84 assert time_parse('1m24s') == 84 assert time_parse('1.2 minutes') == 72 assert time_parse('1.2 seconds') == 1.2 assert time_parse('- 1 minute') == -60 assert time_parse('+ 1 minute') == 60 assert time_parse('1:30') == 90 assert time_parse('1:30', granularity='minutes') == 5400
def check_host_command(db, conn, chan, text): """<host|mask|addr> [last_seen] - Looks up [host|mask|addr] in the users database, optionally filtering to entries newer than [last_seen] specified in the format [-|+]5w4d3h2m1s, defaulting to forever""" allowed, admin = check_channel(conn, chan) if not allowed: return split = text.split(None, 1) host = split.pop(0).strip() host_lower = host.lower() now = datetime.datetime.now() if split: interval_str = split[0].strip() if interval_str == '*': last_time = None else: time_span = datetime.timedelta( seconds=timeparse.time_parse(interval_str)) last_time = now + time_span else: last_time = None if admin: hosts = host_lower addrs = host_lower else: hosts = None addrs = None return query_and_format(db, _masks=host_lower, _hosts=hosts, _addrs=addrs, last_seen=last_time, is_admin=admin)
async def remind(text, nick, chan, db, conn, event, async_call): """<1 minute, 30 seconds>: <do task> - reminds you to <do task> in <1 minute, 30 seconds>""" count = len([ x for x in reminder_cache if x[0] == conn.name and x[3] == nick.lower() ]) if text == "clear": if count == 0: return "You have no reminders to delete." await delete_all(async_call, db, conn.name, nick) await load_cache(async_call, db) return "Deleted all ({}) reminders for {}!".format(count, nick) # split the input on the first ":" parts = text.split(":", 1) if len(parts) == 1: # user didn't add a message, send them help event.notice_doc() return if count > 10: return "Sorry, you already have too many reminders queued (10), you will need to wait or " \ "clear your reminders to add any more." time_string = parts[0].strip() message = colors.strip_all(parts[1].strip()) # get the current time in both DateTime and Unix Epoch current_epoch = time.time() current_time = datetime.fromtimestamp(current_epoch) # parse the time input, return error if invalid seconds = time_parse(time_string) if not seconds: return "Invalid input." if seconds > 2764800 or seconds < 60: return "Sorry, remind input must be more than a minute, and less than one month." # work out the time to remind the user, and check if that time is in the past remind_time = datetime.fromtimestamp(current_epoch + seconds) if remind_time < current_time: # pragma: no cover # This should technically be unreachable because of the previous checks return "I can't remind you in the past!" # finally, add the reminder and send a confirmation message await add_reminder(async_call, db, conn.name, nick, chan, message, remind_time, current_time) await load_cache(async_call, db) remind_text = format_time(seconds, count=2) output = "Alright, I'll remind you \"{}\" in $(b){}$(clear)!".format( message, remind_text) return colors.parse(output)
def remind(text, nick, chan, db, conn, notice, async): """<1m30s>: <do task> -- reminds you to <do task> in <1 minute, 30 seconds>.If no colon is given, only the first word will be used to determine the time. """ count = len([x for x in reminder_cache if x[0] == conn.name and x[4].lower() == nick.lower()]) if text == "clear": if count == 0: return "You have no reminders to delete." yield from delete_all(async, db, conn.name, nick) yield from load_cache(async, db) return "Deleted all ({}) reminders for {}!".format(count, nick) if ":" in text: # split the input on the first ":" parts = text.split(":", 1) else: # take only the first word for the time value otherwise parts = text.split(" ", 1) if len(parts) == 1: # user didn't add a message, send them help notice(remind.__doc__) return if count > 10: notice("Sorry, you already have too many reminders queued (10), you will need to wait or clear your reminders to add any more") return time_string = parts[0].strip() message = colors.strip_all(parts[1].strip()) # get the current time in both DateTime and Unix Epoch current_epoch = time.time() current_time = datetime.fromtimestamp(current_epoch) # parse the time input, return error if invalid seconds = time_parse(time_string) if not seconds: notice("Invalid time. Try something like '2m30s', '2 days 3 hours:', or '1:30s'") return if seconds > 2764800 or seconds < 60: return "Sorry, remind input must be more than a minute, and less than one month" # work out the time to remind the user, and check if that time is in the past remind_time = datetime.fromtimestamp(current_epoch + seconds) if remind_time < current_time: return "I can't remind you in the past!" # finally, add the reminder and send a confirmation message yield from add_reminder(async, db, conn.name, nick, chan, message, remind_time, current_time) yield from load_cache(async, db) remind_text = format_time(seconds, count=2) output = "Alright, I'll remind you \"{}\" in $(b){}$(clear)!".format(message, remind_text) notice(colors.parse(output))
def remind(text, nick, chan, db, conn, notice, async): """<1 minute, 30 seconds>: <do task> -- reminds you to <do task> in <1 minute, 30 seconds>""" count = len([x for x in reminder_cache if x[0] == conn.name and x[3] == nick.lower()]) if text == "clear": if count == 0: return "You have no reminders to delete." yield from delete_all(async, db, conn.name, nick) yield from load_cache(async, db) return "Deleted all ({}) reminders for {}!".format(count, nick) # split the input on the first ":" parts = text.split(":", 1) if len(parts) == 1: # user didn't add a message, send them help notice(remind.__doc__) return if count > 10: return ( "Sorry, you already have too many reminders queued (10), you will need to wait or " "clear your reminders to add any more." ) time_string = parts[0].strip() message = colors.strip_all(parts[1].strip()) # get the current time in both DateTime and Unix Epoch current_epoch = time.time() current_time = datetime.fromtimestamp(current_epoch) # parse the time input, return error if invalid seconds = time_parse(time_string) if not seconds: return "Invalid input." if seconds > 2764800 or seconds < 60: return "Sorry, remind input must be more then a minute, and less then one month." # work out the time to remind the user, and check if that time is in the past remind_time = datetime.fromtimestamp(current_epoch + seconds) if remind_time < current_time: return "I can't remind you in the past!" # finally, add the reminder and send a confirmation message yield from add_reminder(async, db, conn.name, nick, chan, message, remind_time, current_time) yield from load_cache(async, db) remind_text = format_time(seconds, count=2) output = 'Alright, I\'ll remind you "{}" in $(b){}$(clear)!'.format(message, remind_text) return colors.parse(output)
def remind(text, nick, chan, db, conn, notice, async_call): """<1 minute, 30 seconds>: <do task> -- reminds you to <do task> in <1 minute, 30 seconds>""" count = len([x for x in reminder_cache if x[0] == conn.name and x[1] == nick.lower()]) if text == "clear": if count == 0: return "You have no reminders to delete." yield from delete_all(async_call, db, conn.name, nick) yield from load_cache(async_call, db) return "Deleted all ({}) reminders for {}!".format(count, nick) # split the input on the first ":" parts = text.split(":", 1) if len(parts) == 1: # user didn't add a message, send them help notice(remind.__doc__) return if count > 10: return "Sorry, you already have too many reminders queued (10), you will need to wait or " \ "clear your reminders to add any more." time_string = parts[0].strip() message = parts[1].strip() # get the current time in both DateTime and Unix Epoch current_epoch = time.time() current_time = datetime.fromtimestamp(current_epoch) # parse the time input, return error if invalid seconds = time_parse(time_string) if not seconds: return "Invalid timespan format." if seconds > 2764800 or seconds < 60: return "Sorry, remind input must be more than a minute, and less than one month." # work out the time to remind the user, and check if that time is in the past remind_time = datetime.fromtimestamp(current_epoch + seconds) if remind_time < current_time: return "I can't remind you in the past!" # finally, add the reminder and send a confirmation message yield from add_reminder(async_call, db, conn.name, nick, chan, message, remind_time, current_time) yield from load_cache(async_call, db) remind_text = format_time(seconds, count=2) return "Alright, I'll remind you \"{}\" in {}!".format(message, remind_text)
def check_command(conn, chan, text, db): """<nick> [last_seen] - Looks up [nick] in the users database, optionally filtering to entries newer than [last_seen] specified in the format [-|+]5w4d3h2m1s, defaulting to forever""" allowed, admin = check_channel(conn, chan) if not allowed: return split = text.split(None, 1) nick = split.pop(0).strip() now = datetime.datetime.now() if split: interval_str = split[0].strip() if interval_str == '*': last_time = None else: time_span = datetime.timedelta( seconds=timeparse.time_parse(interval_str)) last_time = now + time_span else: last_time = None return query_and_format(db, nick, last_seen=last_time, is_admin=admin)
def remind(text, nick, chan, db, conn, notice, async): """<1m30s>: <do task> -- reminds you to <do task> in <1 minute, 30 seconds>.If no colon is given, only the first word will be used to determine the time. """ count = len([ x for x in reminder_cache if x[0] == conn.name and x[4].lower() == nick.lower() ]) if text == "clear": if count == 0: return "You have no reminders to delete." yield from delete_all(async, db, conn.name, nick) yield from load_cache(async, db) return "Deleted all ({}) reminders for {}!".format(count, nick) if ":" in text: # split the input on the first ":" parts = text.split(":", 1) else: # take only the first word for the time value otherwise parts = text.split(" ", 1) if len(parts) == 1: # user didn't add a message, send them help notice(remind.__doc__) return if count > 10: notice( "Sorry, you already have too many reminders queued (10), you will need to wait or clear your reminders to add any more" ) return time_string = parts[0].strip() message = colors.strip_all(parts[1].strip()) # get the current time in both DateTime and Unix Epoch current_epoch = time.time() current_time = datetime.fromtimestamp(current_epoch) # parse the time input, return error if invalid seconds = time_parse(time_string) if not seconds: notice( "Invalid time. Try something like '2m30s', '2 days 3 hours:', or '1:30s'" ) return if seconds > 2764800 or seconds < 60: return "Sorry, remind input must be more than a minute, and less than one month" # work out the time to remind the user, and check if that time is in the past remind_time = datetime.fromtimestamp(current_epoch + seconds) if remind_time < current_time: return "I can't remind you in the past!" # finally, add the reminder and send a confirmation message yield from add_reminder(async, db, conn.name, nick, chan, message, remind_time, current_time) yield from load_cache(async, db) remind_text = format_time(seconds, count=2) output = "Alright, I'll remind you \"{}\" in $(b){}$(clear)!".format( message, remind_text) notice(colors.parse(output))