Created
November 26, 2016 21:02
-
-
Save artsobolev/15a60248b9fe7c94a5d99d4e5472ca82 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import requests | |
import json | |
last_call = 0 | |
def vk_query(method, **kwargs): | |
global last_call | |
ct = time.time() | |
if ct - last_call < 0.34: | |
time.sleep(0.34 - ct + last_call) | |
last_call = ct | |
url = 'https://api.vk.com/method/{}?v=5.60'.format(method) | |
for key, value in kwargs.iteritems(): | |
url += '&{}={}'.format(key, value) | |
body = requests.get(url).content | |
try: | |
res = json.loads(body)['response'] | |
except KeyError: | |
print "Failed to execute query", url | |
print "Result" | |
print body | |
return | |
except RuntimeError as e: | |
print "Failed to execute query", url | |
print "Result" | |
print body | |
raise e | |
return res |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment