def available(self): now = utils.datetime_utcnow() if os.path.exists(self._filename): with open(self._filename, "r") as lock_file: timestamp_str = lock_file.read().strip().split(" ", 1)[0] timestamp = utils.iso8601_parse(timestamp_str) if (now - timestamp).total_seconds() < EXPIRATION: return False return True
def _parse_date(self, dt: str): if dt.lower() == "today": return utils.datetime_utcnow() else: match = RE_HUMAN_FORMAT.match(dt) if not match: raise utils.EventError("Invalid date format, please use %s" % HUMAN_FORMAT_HELP) return datetime.datetime( year=int(match.group(1)), month=int(match.group(2)), day=int(match.group(3))).replace(tzinfo=datetime.timezone.utc)
def badges(self, event): """ :help: Show all badges for you or a given user """ user = event["user"] if event["args"]: user = event["server"].get_user(event["args_split"][0]) now = self._round_up_day(utils.datetime_utcnow()) badges = [] for badge, date in self._get_badges(user).items(): days_since = self._days_since(now, utils.iso8601_parse(date)) badges.append("%s on day %s" % (badge, days_since)) event["stdout"].write("Badges for %s: %s" % (user.nickname, ", ".join(badges)))
def badge(self, event): """ :help: Show a badge :usage: <badge> """ badge = event["args"] badge_lower = badge.lower() badges = self._get_badges(event["user"]) now = self._round_up_day(utils.datetime_utcnow()) found_badge = self._find_badge(badges, badge) if found_badge: dt = utils.iso8601_parse(badges[found_badge]) days_since = self._days_since(now, dt) event["stdout"].write("(%s) %s on day %s (%s)" % (event["user"].nickname, found_badge, days_since, self._date_str(dt))) else: event["stderr"].write("You have no '%s' badge" % badge)
def next(self): return max(0, (self._next_lock - utils.datetime_utcnow()).total_seconds())
def lock(self): with open(self._filename, "w") as lock_file: last_lock = utils.datetime_utcnow() lock_file.write("%s" % utils.iso8601_format(last_lock)) self._next_lock = last_lock + datetime.timedelta( seconds=EXPIRATION / 2)