Path: blob/master/ invest-robot-contest_TinkoffBotTwitch-main/venv/lib/python3.8/site-packages/twitchio/cooldowns.py
7801 views
# -*- coding: utf-8 -*-12"""3The MIT License (MIT)45Copyright (c) 2017-2021 TwitchIO67Permission is hereby granted, free of charge, to any person obtaining a8copy of this software and associated documentation files (the "Software"),9to deal in the Software without restriction, including without limitation10the rights to use, copy, modify, merge, publish, distribute, sublicense,11and/or sell copies of the Software, and to permit persons to whom the12Software is furnished to do so, subject to the following conditions:1314The above copyright notice and this permission notice shall be included in15all copies or substantial portions of the Software.1617THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING22FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER23DEALINGS IN THE SOFTWARE.24"""2526import asyncio27import time282930class RateBucket:3132HTTPLIMIT = 80033IRCLIMIT = 2034MODLIMIT = 1003536HTTP = 6037IRC = 303839def __init__(self, *, method: str):40self.method = method4142if method == "irc":43self.reset_time = self.IRC44self.limit = self.IRCLIMIT45elif method == "mod":46self.reset_time = self.IRC47self.limit = self.MODLIMIT48else:49self.reset_time = self.HTTP50self.limit = self.HTTPLIMIT5152self.tokens = 053self._reset = time.time() + self.reset_time54self._event = asyncio.Event()55self._event.set()5657@property58def limited(self):59return self.tokens >= self.limit6061def reset(self):62self.tokens = 063self._reset = time.time() + self.reset_time6465def limit_until(self, t):66"""67artificially causes a limit until t68"""69self.tokens = self.limit70self._reset = t7172def update(self, *, reset=None, remaining=None):73now = time.time()7475if self._reset <= now:76self.reset()7778if reset:79self._reset = int(reset)8081if remaining:82self.tokens = self.limit - int(remaining)83else:84self.tokens += 18586async def wait_reset(self):87await self._wait()8889def __await__(self):90return self._wait()9192async def _wait(self):93if self.tokens < self.limit:94if self._event.is_set():95self._event.clear()9697return9899if not self._event.is_set():100await self._event.wait()101return102else:103now = time.time()104await asyncio.sleep(self._reset - now)105self.reset()106self._event.set()107108109