-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApirate.py
56 lines (47 loc) · 1.56 KB
/
Apirate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from time import sleep
import time
##################
seconds=5 # after Y seconds the api rate counter will become 0
apireqs=2 # X api requests will be allowed ever Y seconds
##################
class APIcount:
'''Class that checks that no more than `apireqs` number of API calls can be made for a period
of `seconds` seconds .If more calls than that are made it waits minimum 1 second until API
calls are freed'''
maxsecs=seconds
maxcalls=apireqs
def __init__(self,calls=0):
self.calls=calls
self.starttime=time.time()
self.activetime=time.time()-self.starttime
def __send(self,apicall=1):
if self.calls==0:
self.starttime=time.time()
self.calls=self.calls + apicall
self.activetime=time.time()-self.starttime
def reset(self):
self.calls=0
def check(self):
self.__send()
if self.calls < self.maxcalls:
pass
elif self.calls >=self.maxcalls and self.activetime < self.maxsecs:
time.sleep(int(round(self.maxsecs-self.activetime))+1)
self.reset()
pass
elif self.calls>=self.maxcalls and self.activetime>=self.maxsecs:
self.reset()
#time.sleep(1)
pass
class Timer:
def __init__(self,extrasec=0):
self.extra=extrasec
self.start=time.time()
self.run=1
def get_time(self):
self.run+=1
return time.time()-self.start+self.extra
def reset_time(self):
self.run=2
self.start=time.time()
thresh=APIcount()