Advertisement
Guest User

Untitled

a guest
Aug 31st, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.55 KB | None | 0 0
  1. import requests
  2. import random
  3. import vk_api
  4. import time
  5. from queue import Queue
  6. from sqlalchemy.sql.expression import func
  7.  
  8. import database
  9.  
  10.  
  11. class Account:
  12.     def __init__(self, pool, login, password, token):
  13.         self.pool = pool
  14.         self.vk = None
  15.  
  16.         self.login = login
  17.         self.password = password
  18.         self.token = token
  19.  
  20.  
  21.     def get_vk(self):
  22.         if self.vk:
  23.             return self.vk
  24.  
  25.         token = self.__get_token()
  26.         if not token:
  27.             self.is_valid = False
  28.             return None
  29.        
  30.         # vk = vk_api.VkApi(token=token,app_id=2274003,client_secret="hHbZxrka2uZ6jB1inYsH")
  31.         vk = vk_api.VkApi(login=self.login,password=self.password)
  32.         vk.token = token
  33.         self.vk = vk
  34.         return vk
  35.  
  36.    
  37.     def __get_token(self):
  38.         if self.token:
  39.             return {'access_token': self.token}
  40.        
  41.         response = requests.get('https://oauth.vk.com/access_token?grant_type=password&client_id=2274003&client_secret=hHbZxrka2uZ6jB1inYsH&username='+self.login+'&password='+self.password, proxies=self.pool.get_random_proxy()).json()
  42.         print(response)
  43.         if 'error' in response:
  44.             return None
  45.         else:
  46.             self.token = response["access_token"]
  47.             return response
  48.  
  49.    
  50.     def __enter__(self):
  51.         return self.get_vk()
  52.  
  53.  
  54.     def __exit__(self, type, value, tb):
  55.         if tb is None:
  56.             self.pool.release(self)
  57.         else:
  58.             raise value
  59.  
  60.  
  61.  
  62. class AccountPool:
  63.     def __init__(self, session):
  64.         self.accounts_queue = Queue()
  65.         self.session = session
  66.  
  67.         db_accounts = session.query(database.Account).filter(database.Account.valid == True).order_by(func.random()).all()
  68.         for a in db_accounts:
  69.             account = Account(self, a.login, a.password, a.access_token)
  70.             if account.get_vk():
  71.                 a.access_token = account.token
  72.                 a.valid = True
  73.                 self.accounts_queue.put(account)
  74.             else:
  75.                 a.valid = False
  76.         session.commit()
  77.         print("[AccountPool]", "Initialized")
  78.  
  79.  
  80.     def take(self):
  81.         res = self.accounts_queue.get()
  82.         return res
  83.    
  84.  
  85.     def release(self, account):
  86.         self.accounts_queue.put(account)
  87.    
  88.  
  89.     def get_random_proxy(self):
  90.         rand = random.randrange(0, self.session.query(database.Proxy).count())
  91.         row = self.session.query(database.Proxy)[rand]
  92.         return { 'http': 'http://' + row.ip, 'https': 'http://'+row.ip }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement