Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from instagram_private_api import Client
- from Instagram.classes.misc import Proxy
- from Instagram.config import PATH_TO_DATA
- import os
- import pickle
- AccountObjs = [] # This only exists to skip errors while in dev phase
- # TODO Conver AccountObjs list to use some real persistent live data system
- def find_account(username):
- """ Searches a list of _RUNNING_ Account Objs' and returns them
- if their username matches the function username argument """
- for AccountObj in AccountObjs:
- if AccountObj.username == username:
- return AccountObj
- return None
- def stop_account(username, password):
- """ Safely stops a running Account Obj """
- AccountObj = find_account(username)
- if AccountObj is None:
- return None
- AccountObj.stop()
- def remove_account(username, password):
- """ Deletes the accounts stored data """
- stop_account(username, password)
- path = os.path.join(PATH_TO_DATA, "accounts", username)
- if os.path.exists(path):
- try:
- os.rmdir(path)
- except Exception as e:
- print(str(e))
- def load_account(username, password):
- """ Return an Account Obj using pickle """
- dir_path = os.path.join(PATH_TO_DATA, "accounts")
- file_path = os.path.join(dir_path, username) + ".pickle"
- if os.path.isfile(file_path):
- try:
- info = pickle.load(open(file_path, 'rb'))
- if info["username"] == username and info["password"] == password:
- return Account(*[info[key] for key in list(info.keys)])
- else:
- print("Wrong username or password")
- except EOFError as e:
- print("Data file for ", username, "is empty")
- else:
- print("no account with username {0} in saved accounts")
- return None
- def save_account(AccountObj):
- """ Save data from an Account Obj to a .pickle file """
- dir_path = os.path.join(PATH_TO_DATA, "accounts")
- file_path = os.path.join(dir_path, AccountObj.username) + ".pickle"
- if not os.path.exists(dir_path):
- try:
- os.makedirs(dir_path)
- except Exception as e:
- print(str(e))
- pickle.dump(AccountObj.info, open(file_path, 'wb'))
- def add_account(username, password):
- """ Tries to load an Account Obj from stored data.
- if none is found, create an Account Obj, save it and return it """
- AccountObj = load_account(username, password)
- if AccountObj is None:
- try:
- AccountObj = Account(username, password)
- except Exception as e:
- print(str(e))
- return None
- save_account(AccountObj)
- AccountObjs.append(AccountObj)
- def get_account_info(username):
- """ Returns a dict containing information about a Account Obj """
- AccountObj = find_account(username)
- if AccountObj is None:
- return None
- return AccountObj.info
- class Account:
- def __init__(self, username, password, proxy=None):
- self.username = username
- self.password = password
- self.proxy = proxy
- if self.proxy is None:
- try:
- self.api = Client(self.username, self.password)
- except Exception as e:
- print(str(e))
- else:
- if type(self.proxy) is list:
- self.proxy = Proxy(*proxy)
- try:
- self.api = Client(self.username, self.password, self.proxy.address)
- except Exception as e:
- print(str(e))
- self.info = {"username": self.username,
- "password": self.password,
- "proxy": self.proxy}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement