Skip to content

Commit

Permalink
Add http support. Most of these are untested
Browse files Browse the repository at this point in the history
  • Loading branch information
IAmTomahawkx committed Dec 19, 2020
1 parent 1cac258 commit 88e54e8
Show file tree
Hide file tree
Showing 4 changed files with 603 additions and 6 deletions.
4 changes: 3 additions & 1 deletion twitchio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
import asyncio
from typing import Union, Callable

from twitchio.websocket import WSConnection
from .websocket import WSConnection
from .http import TwitchHTTP


class Client:
Expand All @@ -46,6 +47,7 @@ def __init__(self,
self.loop = loop or asyncio.get_event_loop()
self._connection = WSConnection(bot=self, token=irc_token, nick=nick.lower(), loop=self.loop,
initial_channels=initial_channels)
self._http = TwitchHTTP(self, nick, api_token=api_token, client_id=client_id, client_secret=client_secret)


def run(self):
Expand Down
35 changes: 30 additions & 5 deletions twitchio/cooldowns.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

class RateBucket:

HTTPLIMIT = 30
HTTPLIMIT = 800
IRCLIMIT = 20
MODLIMIT = 100

Expand All @@ -52,15 +52,24 @@ def __init__(self, *, method: str):

self.tokens = 0
self._reset = time.time() + self.reset_time
self._event = asyncio.Event()
self._event.set()

@property
def limited(self):
return self.tokens == self.limit
return self.tokens >= self.limit

def reset(self):
self.tokens = 0
self._reset = time.time() + self.reset_time

def limit_until(self, t):
"""
artificially causes a limit until t
"""
self.tokens = self.limit
self._reset = t

def update(self, *, reset=None, remaining=None):
now = time.time()

Expand All @@ -76,7 +85,23 @@ def update(self, *, reset=None, remaining=None):
self.tokens += 1

async def wait_reset(self):
now = time.time()
await self._wait()

def __await__(self):
return self._wait()

async def _wait(self):
if self.tokens < self.limit:
if self._event.is_set():
self._event.clear()

await asyncio.sleep(self._reset - now)
self.reset()
return

if not self._event.is_set():
await self._event.wait()
return
else:
now = time.time()
await asyncio.sleep(self._reset - now)
self.reset()
self._event.set()
12 changes: 12 additions & 0 deletions twitchio/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,15 @@ class IRCCooldownError(TwitchIOException):

class EchoMessageWarning(TwitchIOException):
pass

class NoClientID(TwitchIOException):
pass

class NoToken(TwitchIOException):
pass

class HTTPException(TwitchIOException):
pass

class Unauthorized(HTTPException):
pass

0 comments on commit 88e54e8

Please sign in to comment.