Skip to content

Instantly share code, notes, and snippets.

@artsobolev
Created November 26, 2016 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artsobolev/15a60248b9fe7c94a5d99d4e5472ca82 to your computer and use it in GitHub Desktop.
Save artsobolev/15a60248b9fe7c94a5d99d4e5472ca82 to your computer and use it in GitHub Desktop.
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