Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- import random
- import vk_api
- import time
- from queue import Queue
- from sqlalchemy.sql.expression import func
- import database
- class Account:
- def __init__(self, pool, login, password, token):
- self.pool = pool
- self.vk = None
- self.login = login
- self.password = password
- self.token = token
- def get_vk(self):
- if self.vk:
- return self.vk
- token = self.__get_token()
- if not token:
- self.is_valid = False
- return None
- # vk = vk_api.VkApi(token=token,app_id=2274003,client_secret="hHbZxrka2uZ6jB1inYsH")
- vk = vk_api.VkApi(login=self.login,password=self.password)
- vk.token = token
- self.vk = vk
- return vk
- def __get_token(self):
- if self.token:
- return {'access_token': self.token}
- 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()
- print(response)
- if 'error' in response:
- return None
- else:
- self.token = response["access_token"]
- return response
- def __enter__(self):
- return self.get_vk()
- def __exit__(self, type, value, tb):
- if tb is None:
- self.pool.release(self)
- else:
- raise value
- class AccountPool:
- def __init__(self, session):
- self.accounts_queue = Queue()
- self.session = session
- db_accounts = session.query(database.Account).filter(database.Account.valid == True).order_by(func.random()).all()
- for a in db_accounts:
- account = Account(self, a.login, a.password, a.access_token)
- if account.get_vk():
- a.access_token = account.token
- a.valid = True
- self.accounts_queue.put(account)
- else:
- a.valid = False
- session.commit()
- print("[AccountPool]", "Initialized")
- def take(self):
- res = self.accounts_queue.get()
- return res
- def release(self, account):
- self.accounts_queue.put(account)
- def get_random_proxy(self):
- rand = random.randrange(0, self.session.query(database.Proxy).count())
- row = self.session.query(database.Proxy)[rand]
- return { 'http': 'http://' + row.ip, 'https': 'http://'+row.ip }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement