Advertisement
stuppid_bot

Untitled

Apr 17th, 2015
561
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.13 KB | None | 0 0
  1. # Copyright (c) 2013-2015 Sergei Snegirev <tz4678@gmail.com>
  2. #
  3. # Permission is hereby granted, free of charge, to any person obtaining a copy
  4. # of this software and associated documentation files (the "Software"), to deal
  5. # in the Software without restriction, including without limitation the rights
  6. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. # copies of the Software, and to permit persons to whom the Software is
  8. # furnished to do so, subject to the following conditions:
  9. #
  10. # The above copyright notice and this permission notice shall be included in
  11. # all copies or substantial portions of the Software.
  12. #
  13. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. # SOFTWARE.
  20. from vkapi import *
  21. from vkapi.auth import CLIENT_ID, CLIENT_SECRET, SCOPE, DirectAuth, AuthError
  22. from PyQt5.QtWidgets import QDialog, QLineEdit, QMessageBox
  23. from PyQt5.QtGui import QPixmap
  24. from .ui_CaptchaDialog import Ui_CaptchaDialog
  25. from .ui_AuthDialog import Ui_AuthDialog
  26.  
  27. __author__ = "Sergei Snegirev <tz4678@gmail.com>"
  28. __license__ = "MIT"
  29.  
  30.  
  31. class CaptchaDialog(QDialog):
  32.     def __init__(self, client, image_url, parent=None):
  33.         super().__init__(parent)
  34.         self.client = client
  35.         self.image_url = image_url
  36.         self.ui = Ui_CaptchaDialog()
  37.         self.ui.setupUi(self)
  38.         self.ui.refreshButton.clicked.connect(self.refresh)
  39.         self.loadImage()
  40.  
  41.     def loadImage(self):
  42.         data = self.client.get(self.image_url).content
  43.         pixmap = QPixmap()
  44.         pixmap.loadFromData(data)
  45.         self.ui.captchaLabel.setPixmap(pixmap)
  46.  
  47.     def refresh(self):
  48.         self.loadImage()
  49.         self.ui.captchaEdit.setText("")
  50.         self.ui.captchaEdit.setFocus()
  51.  
  52.  
  53. # Данные от официального приложения для iPhone
  54. CLIENT_ID = 3140623
  55. CLIENT_SECRET = 'VeWdmVclDCtn6ihuP1nt'
  56. SCOPE = 'notify,friends,photos,audio,video,docs,notes,pages,status,offers,' + \
  57. 'questions,wall,groups,messages,email,notifications,stats,ads,offline,nohttps'
  58.  
  59.  
  60. class DirectAuthDialog(QDialog):
  61.     def __init__(self,
  62.                  client,
  63.                  client_id=CLIENT_ID,
  64.                  client_secret=CLIENT_SECRET,
  65.                  scope=SCOPE,
  66.                  parent=None):
  67.         super().__init__(parent)
  68.         self.client = client
  69.         self.client_id = client_id
  70.         self.client_secret = client_secret
  71.         self.scope = scope
  72.         self.ui = Ui_AuthDialog()
  73.         self.ui.setupUi(self)
  74.         self.ui.loginButton.clicked.connect(self.login)
  75.         self.ui.showPasswordCheck.stateChanged.connect(self.showPassword)
  76.        
  77.     def showPassword(self, state):
  78.         mode = QLineEdit.Normal if state else QLineEdit.Password
  79.         self.ui.passwordEdit.setEchoMode(mode)
  80.  
  81.     def login(self):
  82.         username = self.ui.usernameEdit.text()
  83.         password = self.ui.passwordEdit.text()
  84.         error = ""
  85.  
  86.         if not username:
  87.             error = "Username field is required"
  88.         elif not password:
  89.             error = "Password field is required"
  90.         else:
  91.             self.ui.loginButton.setDisabled(True)
  92.  
  93.             params = dict(username=username,
  94.                           password=password,
  95.                           client_id=self.client_id,
  96.                           client_secret=self.client_secret,
  97.                           scope=self.scope,
  98.                           grant_type="password")
  99.            
  100.             try:
  101.                 self._login(params)
  102.             except AuthError as exc:
  103.                 error = str(exc)
  104.                
  105.             self.ui.loginButton.setEnabled(True)
  106.  
  107.         if error:
  108.             QMessageBox.warning(self, "Authorization Error", error)
  109.         else:
  110.             QDialog.accept(self)
  111.  
  112.     def _login(self, params):
  113.         url = 'https://oauth.vk.com/token'
  114.         r = self.client.post(url, params)
  115.         data = r.json()
  116.  
  117.         if 'error' in data:
  118.             error = AuthError(data)
  119.  
  120.             if error.reason == 'need_captcha':
  121.                 d = CaptchaDialog(self.client, error.captcha_img, self)
  122.  
  123.                 if d.exec_():
  124.                     params['captcha_key'] = d.ui.captchaEdit.text()
  125.                     params['captcha_sid'] = error.captcha_sid
  126.                     return self._login(params)
  127.  
  128.             raise error
  129.  
  130.         self.client.access_token = data['access_token']
  131.         self.client.token_expires = data['expires_in']
  132.  
  133.         if self.client.token_expires > 0:
  134.             self.client.token_expires += time.time()
  135.  
  136.         self.client.user_id = data.get('user_id')
  137.         self.client.secret_token = data.get('secret')
  138.  
  139. # vkapi/auth.py
  140. ################################################################################
  141. # Copyright (c) 2013-2015 Sergei Snegirev <tz4678@gmail.com>
  142. #
  143. # Permission is hereby granted, free of charge, to any person obtaining a copy
  144. # of this software and associated documentation files (the "Software"), to deal
  145. # in the Software without restriction, including without limitation the rights
  146. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  147. # copies of the Software, and to permit persons to whom the Software is
  148. # furnished to do so, subject to the following conditions:
  149. #
  150. # The above copyright notice and this permission notice shall be included in
  151. # all copies or substantial portions of the Software.
  152. #
  153. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  154. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  155. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  156. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  157. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  158. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  159. # SOFTWARE.
  160. import time
  161.  
  162. __author__ = "Sergei Snegirev <tz4678@gmail.com>"
  163. __license__ = "MIT"
  164.  
  165. # Данные от официального приложения для iPhone
  166. CLIENT_ID = 3140623
  167. CLIENT_SECRET = 'VeWdmVclDCtn6ihuP1nt'
  168. SCOPE = 'notify,friends,photos,audio,video,docs,notes,pages,status,offers,' + \
  169. 'questions,wall,groups,messages,email,notifications,stats,ads,offline,nohttps'
  170.  
  171.  
  172. class DirectAuth:
  173.     AUTH_URL = 'https://oauth.vk.com/token'
  174.  
  175.     def __init__(self,
  176.                  client,
  177.                  client_id=CLIENT_ID,
  178.                  client_secret=CLIENT_SECRET,
  179.                  scope=SCOPE):
  180.         self.client = client
  181.         self.client_id = client_id
  182.         self.client_secret = client_secret
  183.         self.scope = scope
  184.  
  185.     def auth(self, username, password):
  186.         params = dict(username=username,
  187.                       password=password,
  188.                       client_id=self.client_id,
  189.                       client_secret=self.client_secret,
  190.                       scope=self.scope,
  191.                       grant_type="password")
  192.  
  193.         def try_auth():
  194.             data = self.client.post(self.AUTH_URL, params).json()
  195.  
  196.             if 'error' in data:
  197.                 error = AuthError(data)
  198.  
  199.                 if error.reason == 'need_captcha':
  200.                     params['captcha_key'] = self.solveCaptcha(error.captcha_img)
  201.                     params['captcha_sid'] = error.captcha_sid
  202.                     return try_auth()
  203.  
  204.                 raise error
  205.  
  206.             self.client.access_token = data['access_token']
  207.             self.client.token_expires = data['expires_in']
  208.  
  209.             if self.client.token_expires > 0:
  210.                 self.client.token_expires += time.time()
  211.  
  212.             self.client.user_id = data.get('user_id')
  213.             self.client.secret_token = data.get('secret')
  214.  
  215.         try_auth()
  216.  
  217.     def solveCaptcha(self, image_url):
  218.         raise NotImplementedError("This method is not implemented yet")
  219.  
  220.  
  221. class AuthError(Exception):
  222.     def __init__(self, details):
  223.         super().__init__("{error}: {error_description}".format(**details) \
  224.             if 'error_description' in details else details['error'])
  225.         self.reason = details['error']
  226.         self.description = details.get('error_description')
  227.         self.captcha_img = details.get('captcha_img')
  228.         self.captcha_sid = details.get('captcha_sid')
  229.         self.redirect_uri = details.get('redirect_uri')
  230.  
  231. ################################################################################
  232.  
  233.      
  234. class UiClient(Client):
  235.     def __init__(self, widget=None, **kw):
  236.         super().__init__(**kw)
  237.         self.widget = widget
  238.  
  239.     def solveCaptcha(self, image_url):
  240.         d = CaptchaDialog(self, image_url, self.widget)
  241.  
  242.         if d.exec_():
  243.             return d.ui.captchaEdit.text()
  244.  
  245.         raise ClientError("Request canceled")
  246.  
  247.  
  248. def
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement