forked from Rast1234/vkd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Api.py
72 lines (63 loc) · 2.69 KB
/
Api.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
__author__ = 'rast'
import json
import urllib2
from urllib import urlencode
import re
from time import sleep
import logging
def auth(args):
"""Interact with user to get access_token"""
url = "https://oauth.vk.com/oauth/authorize?" + \
"redirect_uri=https://oauth.vk.com/blank.html&response_type=token&" + \
"client_id=%s&scope=%s&display=wap" % (args.app_id, ",".join(args.access_rights))
print("Please open this url:\n\n\t{}\n".format(url))
raw_url = raw_input("Grant access to your acc and copy resulting URL here: ")
res = re.search('access_token=([0-9A-Fa-f]+)', raw_url, re.I)
if res is not None:
return res.groups()[0]
else:
return None
def captcha(data):
"""Ask user to solve captcha"""
logging.debug("Captcha needed..")
print("VK thinks you're a bot - and you are ;)")
print("They want you to solve CAPTCHA. Please open this URL, and type here a captcha solution:")
print("\n\t{}\n".format(data[u'error'][u'captcha_img']))
solution = raw_input("Solution = ").strip()
return data[u'error'][u'captcha_sid'], solution
def call_api(method, params, args):
while True:
if isinstance(params, list):
params_list = [kv for kv in params]
elif isinstance(params, dict):
params_list = params.items()
else:
params_list = [params]
params_list.append(("access_token", args.token))
url = "https://api.vk.com/method/%s?%s" % (method, urlencode(params_list))
json_stuff = urllib2.urlopen(url).read()
result = json.loads(json_stuff)
if u'error' in result.keys():
if result[u'error'][u'error_code'] == 6: # too many requests
logging.debug("Too many requests per second, sleeping..")
sleep(1)
continue
elif result[u'error'][u'error_code'] == 14: # captcha needed :\
sid, key = captcha(result)
params.extend([(u"captcha_sid", sid), (u"captcha_key", key)])
continue
else:
msg = "API call resulted in error ({}): {}".format(result[u'error'][u'error_code'],
result[u'error'][u'error_msg'])
logging.error(msg)
raise RuntimeError(msg)
else:
logging.debug("API call succeeded: {}".format(url))
break
if not u'response' in result.keys():
msg = "API call result has no response"
logging.error(msg)
raise RuntimeError(msg)
else:
#logging.debug("API call answer: {}".format(str(result[u'response'])))
return result[u'response'], json_stuff