Advertisement
stuppid_bot

vkclient.py

Nov 10th, 2013
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.08 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. import urllib
  3. import json
  4. from formdata import *
  5. import urllib2
  6.  
  7. class VKClientException(Exception): pass
  8.  
  9. class VKClient:
  10.     def __init__(self):
  11.         self.client_id = '2692017'
  12.         self.client_secret = 'rqIkXYBauPjqE2EVxB2j'
  13.         self.permissions = 'notify,friends,photos,audio,video,docs,notes,wall,groups,messages,notifications,activity,status,pages,stats'
  14.         self.api_version = '5.3'
  15.         self.auth_info = None
  16.  
  17.     def auth(self, login, password, captcha_sid='', captcha_key=''):
  18.         payload = {
  19.             'username': login,
  20.             'password': password,
  21.             'captcha_sid': captcha_sid,
  22.             'captcha_key': captcha_key,
  23.             'client_id': self.client_id,
  24.             'client_secret': self.client_secret,
  25.             'scope': self.permissions,
  26.             'grant_type': 'password',
  27.             'v': self.api_version
  28.         }
  29.         r = urllib.urlopen('https://oauth.vk.com/token?' + urllib.urlencode(payload));
  30.         if r.code != 200:
  31.             raise VKClientException('HTTP status code was not 200')
  32.         self.auth_info = auth_info = json.loads(r.read())
  33.         if 'error' in self.auth_info:
  34.             raise VKClientException('%s: %s' % (auth_info['error'], auth_info['error_description'] if 'error_description' in auth_info else ''))
  35.  
  36.     def api_call(self, method, data):
  37.         if type(data) == dict:
  38.             data_type = 'application/x-www-form-urlencoded'
  39.             data['access_token'] = self.auth_info['access_token']
  40.             data = urllib.urlencode(data)
  41.         elif isinstance(data, FormData):
  42.             data_type = 'multipart/form-data; boundary=' + data.boundary
  43.             data.add_text('access_token', self.auth_info['access_token'].encode('utf-8'))
  44.             data = str(data)
  45.         else:
  46.             raise VKClientException('Bad data')
  47.         req = urllib2.Request('https://api.vk.com/method/' + method, data, {'Content-Type': data_type})
  48.         res = urllib2.urlopen(req)
  49.         # print res.info().headers
  50.         return json.loads(res.read().decode('utf-8'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement