From c1671a3e0ce966f414d2528427e963b22f47e71b Mon Sep 17 00:00:00 2001 From: John Smith Date: Sat, 12 Jan 2019 21:26:48 +0700 Subject: increase api delay step by 0.10; fix groupchats --- extensions/groupchats.py | 1 + library/vkapi.py | 30 ++++++++++++++++++------------ 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/extensions/groupchats.py b/extensions/groupchats.py index d359641..0695bc6 100644 --- a/extensions/groupchats.py +++ b/extensions/groupchats.py @@ -482,6 +482,7 @@ def exterminateChats(user=None, chats=[]): if not frm: logger.critical("no from in stanza! %s", stanza) return + chat = frm.getStripped() if xmpp.isResultNode(stanza): logger.debug("groupchats: target exterminated! Yay! target:%s (jid: %s)", chat, jid) else: diff --git a/library/vkapi.py b/library/vkapi.py index 6c54116..2f5d007 100644 --- a/library/vkapi.py +++ b/library/vkapi.py @@ -291,7 +291,6 @@ class APIBinding(RequestProcessor): """ Provides simple VK API binding Translates VK errors to python exceptions - Allows to make a password authorization """ def __init__(self, token, debug=None, logline=""): self.token = token @@ -299,11 +298,23 @@ class APIBinding(RequestProcessor): self.last = [] self.captcha = {} self.lastMethod = () - self.timeout = 1.00 + self.delay = 1.00 # to use it in logs without showing the token self.logline = logline RequestProcessor.__init__(self) + + def __delay(self): + """ + Delaying method execution to prevent "too fast" errors from happening + Typically VK allows us execution of 3 methods per second. + """ + self.last.append(time.time()) + if len(self.last) > 2: + if (self.last.pop() - self.last.pop(0)) <= self.delay: + time.sleep(self.delay / METHOD_THROUGHPUT) + self.last = [time.time()] + def method(self, method, values=None, notoken=False): """ Issues a VK method @@ -314,6 +325,7 @@ class APIBinding(RequestProcessor): Returns: The method execution result """ + self.__delay() url = "https://api.vk.com/method/%s" % method values = values or {} if not notoken: @@ -326,14 +338,9 @@ class APIBinding(RequestProcessor): self.captcha = {} self.lastMethod = (method, values) - # prevent “too fast” errors - self.last.append(time.time()) - if len(self.last) > 2: - if (self.last.pop() - self.last.pop(0)) <= self.timeout: - time.sleep(self.timeout / 3.0) start = time.time() - if method in self.debug or self.debug == "all": + if self.debug == "all" or method in self.debug: Print("SENT: method %s with values %s in thread: %s" % (method, colorizeJSON(values), threading.currentThread().name)) @@ -391,12 +398,11 @@ class APIBinding(RequestProcessor): # todo: where we going we NEED constants elif eCode in (1, 6, 9, 100): if eCode in (6, 9): # 6 - too fast / 9 - flood control - self.timeout += 0.05 + self.delay += 0.15 # logger doesn't seem to support %0.2f logger.warning("vkapi: got code %s, increasing timeout to %0.2f (for: %s)" % - (eCode, self.timeout, self.logline)) - # waiting a bit and trying to execute te method again - time.sleep(self.timeout) + (eCode, self.delay, self.logline)) + # rying to execute te method again (it will sleep a while in __delay()) return self.method(method, values) return {"error": eCode} raise VkApiError(eMsg) -- cgit v1.2.3