Welcome to mirror list, hosted at ThFree Co, Russian Federation.

vkapi.py « library - github.com/mrDoctorWho/vk4xmpp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c32284e536b73c8b00205b7fc45636f9c3356583 (plain)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
# coding: utf-8
# © simpleApps, 2013 — 2016.

"""
Manages VK API requests
Provides password login and direct VK API calls
Designed for huge number of clients (per ip)
Which is why it has that lot of code
"""

__author__ = "mrDoctorWho <mrdoctorwho@gmail.com>"

import cookielib
import httplib
import logging
import re
import socket
import ssl
import time
import threading
import urllib
import urllib2
import webtools
from printer import *

SOCKET_TIMEOUT = 20
REQUEST_RETRIES = 3

# VK APP ID
APP_ID = 3789129
# VK APP scope
SCOPE = 69638
# VK API VERSION
API_VERSION = "5.21"

socket.setdefaulttimeout(SOCKET_TIMEOUT)

logger = logging.getLogger("vk4xmpp")

token_exp = re.compile("(([\da-f]+){11,})", re.IGNORECASE)

ERRORS = (httplib.BadStatusLine,
	urllib2.URLError,
	socket.gaierror,
	socket.timeout,
	socket.error,
	ssl.SSLError)

METHOD_THROUGHPUT = 3.0

# Trying to use faster library usjon instead of simplejson
try:
	import ujson as json
	logger.debug("vkapi: using ujson instead of simplejson")
except ImportError:
	import json
	logger.warning("vkapi: ujson wasn't loaded, using simplejson instead")


def repeat(maxRetries, resultType, *errors):
	"""
	Tries to execute function ignoring specified errors specified number of
	times and returns specified result type on try limit.
	"""
	if not isinstance(resultType, type):
		resultType = lambda result = resultType: result
	if not errors:
		errors = Exception

	def decorator(func):

		def wrapper(*args, **kwargs):
			retries = 0
			while retries < maxRetries:
				try:
					data = func(*args, **kwargs)
				except errors as exc:
					retries += 1
					logger.warning("vkapi: trying to execute \"%s\" in #%d time",
						func.func_name, retries)
					time.sleep(0.2)
				else:
					break
			else:
				errno = getattr(exc, "errno", 0)
				if errno == 101:
					raise NetworkNotFound()
				elif errno == 104:
					raise NetworkError()
				data = resultType()
				logger.warning("vkapi: Error %s occurred on executing %s(*%s, **%s)",
					exc,
					func.func_name,
					str(args),
					str(kwargs))
			return data

		wrapper.__name__ = func.__name__
		return wrapper

	return decorator


class AsyncHTTPRequest(httplib.HTTPSConnection):
	"""
	A method to make asynchronous http requests
	Provides a way to get a socket object to use in select()
	"""
	def __init__(self, url, data=None, headers=(), timeout=SOCKET_TIMEOUT):
		host = urllib.splithost(urllib.splittype(url)[1])[0]
		httplib.HTTPSConnection.__init__(self, host, timeout=timeout)
		self.url = url
		self.data = data
		self.headers = headers or {}
		self.created = time.time()

	@repeat(REQUEST_RETRIES, None, *ERRORS)
	def open(self):
		self.connect()
		self.request(("POST" if self.data else "GET"), self.url, self.data,
			self.headers)
		return self

	def read(self):
		with self as resp:
			return resp.read()

	def __enter__(self):
		return self.getresponse()

	def __exit__(self, *args):
		self.close()

	@staticmethod
	def getOpener(url, query=None):
		"""
		Opens a connection to url and returns AsyncHTTPRequest() object
		Args: query a dict() of query parameters
		"""
		if query:
			url += "?%s" % urllib.urlencode(query)
		return AsyncHTTPRequest(url).open()


class RequestProcessor(object):
	"""
	Processes base requests:
		POST (application/x-www-form-urlencoded and multipart/form-data)
		GET
	"""
	headers = {"User-agent": "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:21.0)"
					" Gecko/20130309 Firefox/21.0",
				"Accept-Language": "ru-RU, utf-8"}
	boundary = "github.com/mrDoctorWho/vk4xmpp"

	def __init__(self, cook=False):
		if cook:
			cookieJar = cookielib.CookieJar()
			cookieProcessor = urllib2.HTTPCookieProcessor(cookieJar)
			self.open = urllib2.build_opener(cookieProcessor).open
			self.getCookie = lambda name: [c.value for c in cookieJar if c.name == name]
		else:
			self.open = urllib2.build_opener().open

	def multipart(self, key, name, ctype, data):
		"""
		Makes multipart/form-data encoding
		Parameters:
			key: form key (is there a form?)
			name: filename
			ctype: content type
			data: the data you want to send
		"""
		boundary = "--%s" % self.boundary
		disposition = "Content-Disposition: form-data; name=\"%s\"; filename=\"%s\""\
		% (key, name)
		ctype = "Content-Type: %s" % ctype
		header = "%(boundary)s\n%(disposition)s\n%(ctype)s\n\n" % vars()
		footer = "\n%s--\n" % boundary
		return header + data + footer

	def request(self, url, data=None, headers=None, urlencode=True):
		"""
		Makes a http(s) request
		Parameters:
			url: a request url
			data: a request data
			headers: a request headers (if not set, self.headers will be used)
			urlencode: urlencode flag
		"""
		headers = headers or self.headers
		if data and urlencode:
			headers["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8"
			data = urllib.urlencode(data)
		else:
			headers["Content-Type"] = "multipart/form-data; boundary=%s" % self.boundary
		request = urllib2.Request(url, data, headers)
		return request

	@repeat(REQUEST_RETRIES, tuple, *ERRORS)
	def post(self, url, data=None, urlencode=True):
		"""
		POST request
		"""
		resp = self.open(self.request(url, data, urlencode=urlencode))
		body = resp.read()
		return (body, resp)

	def get(self, url, query=None):
		"""
		GET request
		Args:
			query: a dict() of query parameters
		"""
		if query:
			url += "?%s" % urllib.urlencode(query)
		resp = self.open(self.request(url))
		body = resp.read()
		return (body, resp)


class PasswordLogin(RequestProcessor):
	"""
	Provides a way to log-in by a password
	"""
	def __init__(self, number, password):
		self.number = number
		self.password = password
		RequestProcessor.__init__(self, cook=True)

	def login(self):
		"""
		Logging in using password
		"""
		url = "https://login.vk.com/"
		values = {"act": "login", "email": self.number, "pass": self.password}

		body, response = self.post(url, values)

		if "sid=" in response.url:
			logger.error("vkapi: PasswordLogin ran into a captcha! (number: %s)",
				self.number)
			raise AuthError("Captcha!")

		if not self.getCookie("remixsid"):
			raise AuthError("Invalid password")

		if "security_check" in response.url:
			logger.warning("vkapi: PasswordLogin ran into a security check (number: %s)",
				self.number)
			hash = re.search("security_check.*?hash: '(.*?)'\};", body).group(0)
			if not self.number[0] == "+":
				self.number = "+" + self.number

			code = self.number[2:-2]  # valid for Russia only. Unfortunately.
			values = {"act": "security_check", "al": "1", "al_page": "3",
				"code": code, "hash": hash, "to": ""}
			post = self.post("https://vk.com/login.php", values)
			body, response = post
			if response and not body.split("<!>")[4] == "4":
				raise AuthError("Incorrect number")
		return self

	def confirm(self):
		"""
		Confirms the application and receives the token
		"""
		url = "https://oauth.vk.com/authorize/"
		values = {"display": "mobile", "scope": SCOPE,
			"client_id": APP_ID, "response_type": "token",
			"redirect_uri": "https://oauth.vk.com/blank.html"}

		token = None
		body, response = self.get(url, values)
		if response:
			if "access_token" in response.url:
				match = token_exp.search(response.url)
				if match:
					token = match.group(0)
				else:
					logger.error("token regexp doesn't match the url: %s", response.url)
					raise AuthError("Something went wrong. We're so sorry.")
			else:
				postTarget = webtools.getTagArg("form method=\"post\"", "action",
					body, "form")
				if postTarget:
					body, response = self.post(postTarget)
					token = token_exp.search(response.url).group(0)
				else:
					raise AuthError("Couldn't confirm the application!")
		return token


class APIBinding(RequestProcessor):
	"""
	Provides simple VK API binding
	Translates VK errors to python exceptions
	"""
	def __init__(self, token, debug=None, logline=""):
		self.token = token
		self.debug = debug
		self.last = []
		self.captcha = {}
		self.lastMethod = ()
		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
		Args:
			method: vk method
			values: method parameters
			notoken: whether to cut the token out of the request
		Returns:
			The method execution result
		"""
		self.__delay()
		url = "https://api.vk.com/method/%s" % method
		values = values or {}
		if not notoken:
			values["access_token"] = self.token
		values["v"] = API_VERSION

		if "key" in self.captcha:
			values["captcha_sid"] = self.captcha["sid"]
			values["captcha_key"] = self.captcha["key"]
			self.captcha = {}

		self.lastMethod = (method, values)

		start = time.time()
		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))

		response = self.post(url, values)
		if response:
			body, response = response
			if body:
				try:
					body = json.loads(body)
				except ValueError:
					return {}

			if self.debug:
				end = time.time()
				dbg = (method, colorizeJSON(body), threading.currentThread().name, (end - start), self.logline)
				if method in self.debug or self.debug == "all":
					Print("GOT: for method %s: %s in thread: %s (%0.2fs) for %s" % dbg)

				if self.debug == "slow":
					if (end - start) > 3:
						Print("GOT: (slow) response for method %s: %s in thread: %s (%0.2fs) for %s" % dbg)

			if "response" in body:
				return body["response"] or {}

			# according to vk.com/dev/errors
			elif "error" in body:
				error = body["error"]
				eCode = error["error_code"]
				eMsg = error.get("error_msg", "")
				logger.error("vkapi: error occured on executing method"
					" (%s(%s), code: %s, msg: %s), (for: %s)" % (method, values, eCode, eMsg, self.logline))

				if eCode == 7:  # not allowed
					raise NotAllowed(eMsg)

				elif eCode == 10:  # internal server error
					raise InternalServerError(eMsg)

				elif eCode == 13:  # runtime error
					raise RuntimeError(eMsg)

				elif eCode == 14:  # captcha
					if "captcha_sid" in error:
						self.captcha = {"sid": error["captcha_sid"], "img": error["captcha_img"]}
						raise CaptchaNeeded()

				elif eCode == 15:
					raise AccessDenied(eMsg)

				elif eCode == 17:
					raise ValidationRequired(eMsg)

				# 1 - unknown error / 100 - wrong method or parameters loss
				# 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.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.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)

	def retry(self):
		"""
		Tries to execute last method again
		Usually called after captcha is entered
		"""
		result = None
		if self.lastMethod:
			try:
				result = self.method(*self.lastMethod)
			except CaptchaNeeded:
				raise
			except Exception:
				pass
		return result


class NetworkNotFound(Exception):
	"""
	This happens in very weird situations
	Happened just once at 10.01.2014 (vk.com was down)
	"""
	pass


class NetworkError(Exception):
	"""
	Common network error
	"""
	pass


class LongPollError(Exception):
	"""
	Should be raised when longpoll exception occurred
	"""
	pass


class VkApiError(Exception):
	"""
	Base VK API Error
	"""
	pass


class CaptchaNeeded(Exception):
	"""
	Will be raised when captcha appears
	"""
	pass


class AuthError(VkApiError):
	"""
	Happens when user is trying to login using password
	"""
	pass


class InternalServerError(VkApiError):
	"""
	Well, that error should be probably ignored
	"""
	pass


class TokenError(VkApiError):
	"""
	Will be raised if Token Error occurred
	"""
	pass


class NotAllowed(VkApiError):
	"""
	Will be raised when happens error with code 7
	Happens usually when someone's added our user in the black-list
	"""
	pass


class AccessDenied(VkApiError):
	"""
	This one should be ignored as well.
	Happens for an unknown reason with any method
	"""
	pass


class ValidationRequired(VkApiError):
	"""
	New in API v4
	Happens if VK thinks we're
	logging in from an unusual location
	"""
	pass