Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wiseplat
GitHub Repository: wiseplat/python-code
Path: blob/master/ invest-robot-contest_TinkoffBotTwitch-main/venv/lib/python3.8/site-packages/twitchio/cooldowns.py
7801 views
1
# -*- coding: utf-8 -*-
2
3
"""
4
The MIT License (MIT)
5
6
Copyright (c) 2017-2021 TwitchIO
7
8
Permission is hereby granted, free of charge, to any person obtaining a
9
copy of this software and associated documentation files (the "Software"),
10
to deal in the Software without restriction, including without limitation
11
the rights to use, copy, modify, merge, publish, distribute, sublicense,
12
and/or sell copies of the Software, and to permit persons to whom the
13
Software is furnished to do so, subject to the following conditions:
14
15
The above copyright notice and this permission notice shall be included in
16
all copies or substantial portions of the Software.
17
18
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24
DEALINGS IN THE SOFTWARE.
25
"""
26
27
import asyncio
28
import time
29
30
31
class RateBucket:
32
33
HTTPLIMIT = 800
34
IRCLIMIT = 20
35
MODLIMIT = 100
36
37
HTTP = 60
38
IRC = 30
39
40
def __init__(self, *, method: str):
41
self.method = method
42
43
if method == "irc":
44
self.reset_time = self.IRC
45
self.limit = self.IRCLIMIT
46
elif method == "mod":
47
self.reset_time = self.IRC
48
self.limit = self.MODLIMIT
49
else:
50
self.reset_time = self.HTTP
51
self.limit = self.HTTPLIMIT
52
53
self.tokens = 0
54
self._reset = time.time() + self.reset_time
55
self._event = asyncio.Event()
56
self._event.set()
57
58
@property
59
def limited(self):
60
return self.tokens >= self.limit
61
62
def reset(self):
63
self.tokens = 0
64
self._reset = time.time() + self.reset_time
65
66
def limit_until(self, t):
67
"""
68
artificially causes a limit until t
69
"""
70
self.tokens = self.limit
71
self._reset = t
72
73
def update(self, *, reset=None, remaining=None):
74
now = time.time()
75
76
if self._reset <= now:
77
self.reset()
78
79
if reset:
80
self._reset = int(reset)
81
82
if remaining:
83
self.tokens = self.limit - int(remaining)
84
else:
85
self.tokens += 1
86
87
async def wait_reset(self):
88
await self._wait()
89
90
def __await__(self):
91
return self._wait()
92
93
async def _wait(self):
94
if self.tokens < self.limit:
95
if self._event.is_set():
96
self._event.clear()
97
98
return
99
100
if not self._event.is_set():
101
await self._event.wait()
102
return
103
else:
104
now = time.time()
105
await asyncio.sleep(self._reset - now)
106
self.reset()
107
self._event.set()
108
109