SHARE
TWEET

Untitled

stuppid_bot Jan 25th, 2016 (edited) 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from .ui_authentication import Ui_Authentication
  2. from PyQt5.QtGui import QImage, QPixmap
  3. from PyQt5.QtWidgets import QDialog
  4. import logging
  5.  
  6.  
  7. AUTH_URL = "https://oauth.vk.com/token"
  8. CLIENT_ID = 2274003
  9. CLIENT_SECRET = 'hHbZxrka2uZ6jB1inYsH'
  10.  
  11.  
  12. class Authentication(QDialog):
  13.     def __init__(self,
  14.                  client,
  15.                  client_id=CLIENT_ID,
  16.                  client_secret=CLIENT_SECRET,
  17.                  scope=None,
  18.                  parent=None):
  19.         self.client = client
  20.         self.client_id = client_id
  21.         self.client_secret = client_secret
  22.         self.scope = scope
  23.         self.logger = logging.getLogger('.'.join([
  24.             __name__, self.__class__.__name__]))
  25.         self.captcha_img = None
  26.         self.captcha_sid = None
  27.         super(QDialog, self).__init__(parent)
  28.         self.ui = Ui_Authentication()
  29.         self.ui.setupUi(self)
  30.         self.hide_error()
  31.         self.hide_captcha()
  32.         self.connect_slots()
  33.  
  34.     def connect_slots(self):
  35.         # Убираем пробелы у значений полей
  36.         self.ui.username_line.editingFinished.connect(
  37.             lambda: self.ui.username_line.setText(
  38.                 self.ui.username_line.text().strip()))
  39.         self.ui.password_line.editingFinished.connect(
  40.             lambda: self.ui.password_line.setText(
  41.                 self.ui.password_line.text().strip()))
  42.         self.ui.captcha_line.editingFinished.connect(
  43.             lambda: self.ui.captcha_line.setText(
  44.                 self.ui.captcha_line.text().strip()))
  45.         self.ui.refresh_captcha_button.clicked.connect(self.load_captcha)
  46.         self.ui.login_button.clicked.connect(self.login)
  47.  
  48.     def login(self):
  49.         username = self.ui.username_line.text()
  50.         password = self.ui.password_line.text()
  51.         if not username or not password:
  52.             self.show_error('Username and password are required.')
  53.             return
  54.         params = {
  55.             'username': username,
  56.             'password': password,
  57.             'v': self.client.api_version,
  58.             'client_id': self.client_id,
  59.             'client_secret': self.client_secret,
  60.             'grant_type': 'password'
  61.         }
  62.         if self.scope:
  63.             params['scope'] = self.scope
  64.         if self.ui.captcha_frame.isVisible():
  65.             params['captcha_sid'] = self.captcha_sid
  66.             params['captcha_key'] = self.ui.captcha_line.text()
  67.         response = self.client.get(AUTH_URL, params)
  68.         if 'captcha_sid' in response:
  69.             self.captcha_img = response.captcha_img
  70.             self.captcha_sid = response.captcha_sid
  71.             self.show_error('Captcha required.')
  72.             self.show_captcha()
  73.             return
  74.         if 'error' in response:
  75.             self.logger.warning(
  76.                 "{error}: {error_description}".format(**response))
  77.             self.show_error(response.error_description)
  78.             self.ui.password_line.clear()
  79.             self.ui.username_line.setFocus()
  80.             self.ui.username_line.selectAll()
  81.             self.hide_captcha()
  82.             return
  83.         self.client.access_token = response.access_token
  84.         self.client.user_id = response.user_id
  85.         self.client.secret = response.get('secret')
  86.         assert response.expires_in == 0
  87.         self.logger.info('Authentication successful')
  88.         self.accept()
  89.  
  90.     def hide_error(self):
  91.         self.ui.error_label.hide()
  92.  
  93.     def show_error(self, message):
  94.         self.ui.error_label.setText(self.tr(message))
  95.         self.ui.error_label.show()
  96.  
  97.     def hide_captcha(self):
  98.         self.ui.captcha_frame.hide()
  99.  
  100.     def show_captcha(self):
  101.         self.load_captcha()
  102.         self.ui.captcha_frame.show()
  103.  
  104.     def load_captcha(self):
  105.         data = self.client.session.get(self.captcha_img).content
  106.         pix = QPixmap.fromImage(QImage.fromData(data))
  107.         self.ui.captcha_image.setPixmap(pix)
  108.         self.ui.captcha_line.clear()
  109.         self.ui.captcha_line.setFocus()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top