Last active
April 20, 2016 17:04
-
-
Save ermakovpetr/5ed7946e5b66bc1f26542c6f8f36d637 to your computer and use it in GitHub Desktop.
This file contains 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 collections | |
import requests | |
from tqdm import tqdm | |
default_url = 'https://api.vk.com/method/{}' + \ | |
'&v=5.50&' + \ | |
'access_token=6ebe46f21ec2fca1ab326f34d88341c2979d4eba3850614ee73aaf0987d3da58fe71f4d6f314c7bb2661b' | |
deq = collections.deque(maxlen=3) | |
def trottling_request(): | |
deq.appendleft(time.time()) | |
if len(deq) == 3: | |
time.sleep(max(1+deq[2]-deq[0], 0)) | |
result = {} | |
for owner_id in tqdm(range(1000)): | |
trottling_request() | |
raw_resp = requests.get( | |
default_url.format( | |
'audio.get?owner_id={owner_id}&count=6000'.format( | |
owner_id=owner_id | |
) | |
) | |
) | |
try: | |
resp = raw_resp.json() | |
result[owner_id] = resp['response']['items'] | |
except Exception as e: | |
if raw_resp.json().get("error", {}).get("error_code", 0) == 18: | |
continue | |
print raw_resp.text | |
print e | |
json.dump(result, open('vk_music.json', 'w')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment