Skip to content

Commit

Permalink
custom sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
ScriptSmith committed Nov 19, 2018
1 parent d00c84c commit c333a5f
Showing 1 changed file with 53 additions and 2 deletions.
55 changes: 53 additions & 2 deletions socialreaper/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


class API:
def __init__(self):
def __init__(self, session=None):
self.log_function = print
self.retry_rate = 5
self.num_retries = 5
Expand All @@ -19,6 +19,7 @@ def __init__(self):
self.common_errors = (requests.exceptions.ConnectionError,
requests.exceptions.Timeout,
requests.exceptions.HTTPError)
self.session = session

def __str__(self):
return pformat(vars(self))
Expand Down Expand Up @@ -61,7 +62,8 @@ def get(self, *args, **kwargs):
"""

try:
req = requests.get(*args, **kwargs)
req_func = self.session.get if self.session else requests.get
req = req_func(*args, **kwargs)
req.raise_for_status()
self.failed_last = False
return req
Expand Down Expand Up @@ -674,3 +676,52 @@ def read_edge(self, edge, fields, **params):
parameters = self.merge_params(parameters, params)

return self.api_call(edge, parameters)


class Twitch(API):
def __init__(self, client_id):
super().__init__()

self.client_id = client_id
self.url = "https://api.twitch.tv/helix"
self.request_rate = 5

self.last_request = time()

def api_call(self, edge, parameters, return_results=True):
headers = {
'Client-ID': self.client_id
}
req = self.get(f"{self.url}/{edge}", params=parameters,
headers=headers)

time_diff = time() - self.last_request
if time_diff < self.request_rate:
sleep(self.request_rate - time_diff)

self.last_request = time()

if return_results:
return req.json()

def videos(self, id=None, user_id=None, game_id=None, after=None,
before=None, first=100, period='all', sort='time',
type='all', **kwargs):

parameters = {
'id': id,
'user_id': user_id,
'game_id': game_id,
'after': after,
'before': before,
'first': first,
'period': period,
'sort': sort,
'type': type
}

parameters = self.merge_params(parameters, kwargs)
return self.api_call('videos', parameters=parameters)

def user_id(self, username):
return self.api_call('users', parameters={'login': username})

0 comments on commit c333a5f

Please sign in to comment.