Advertisement
Guest User

Untitled

a guest
Oct 16th, 2018
762
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.71 KB | None | 0 0
  1. from instagram_private_api import Client
  2. from Instagram.classes.misc import Proxy
  3. from Instagram.config import PATH_TO_DATA
  4.  
  5. import os
  6. import pickle
  7.  
  8. AccountObjs = [] #  This only exists to skip errors while in dev phase
  9.  
  10. # TODO  Conver AccountObjs list to use some real persistent live data system
  11.  
  12.  
  13. def find_account(username):
  14.     """ Searches a list of _RUNNING_ Account Objs' and returns them
  15.        if their username matches the function username argument """
  16.  
  17.     for AccountObj in AccountObjs:
  18.         if AccountObj.username == username:
  19.             return AccountObj
  20.  
  21.     return None
  22.  
  23.  
  24. def stop_account(username, password):
  25.     """ Safely stops a running Account Obj """
  26.  
  27.     AccountObj = find_account(username)
  28.  
  29.     if AccountObj is None:
  30.         return None
  31.  
  32.     AccountObj.stop()
  33.  
  34.  
  35. def remove_account(username, password):
  36.     """ Deletes the accounts stored data """
  37.  
  38.     stop_account(username, password)
  39.  
  40.     path = os.path.join(PATH_TO_DATA, "accounts", username)
  41.  
  42.     if os.path.exists(path):
  43.         try:
  44.             os.rmdir(path)
  45.         except Exception as e:
  46.             print(str(e))
  47.  
  48.  
  49. def load_account(username, password):
  50.     """ Return an Account Obj using pickle """
  51.  
  52.     dir_path = os.path.join(PATH_TO_DATA, "accounts")
  53.     file_path = os.path.join(dir_path, username) + ".pickle"
  54.  
  55.     if os.path.isfile(file_path):
  56.         try:
  57.             info = pickle.load(open(file_path, 'rb'))
  58.             if info["username"] == username and info["password"] == password:
  59.                 return Account(*[info[key] for key in list(info.keys)])
  60.             else:
  61.                 print("Wrong username or password")
  62.         except EOFError as e:
  63.             print("Data file for ", username, "is empty")
  64.     else:
  65.         print("no account with username {0} in saved accounts")
  66.  
  67.     return None
  68.  
  69.  
  70. def save_account(AccountObj):
  71.     """ Save data from an Account Obj to a .pickle file """
  72.  
  73.     dir_path = os.path.join(PATH_TO_DATA, "accounts")
  74.     file_path = os.path.join(dir_path, AccountObj.username) + ".pickle"
  75.  
  76.     if not os.path.exists(dir_path):
  77.         try:
  78.             os.makedirs(dir_path)
  79.         except Exception as e:
  80.             print(str(e))
  81.  
  82.     pickle.dump(AccountObj.info, open(file_path, 'wb'))
  83.  
  84.  
  85. def add_account(username, password):
  86.     """ Tries to load an Account Obj from stored data.
  87.        if none is found, create an Account Obj, save it and return it """
  88.  
  89.     AccountObj = load_account(username, password)
  90.  
  91.     if AccountObj is None:
  92.         try:
  93.             AccountObj = Account(username, password)
  94.         except Exception as e:
  95.             print(str(e))
  96.             return None
  97.  
  98.         save_account(AccountObj)
  99.  
  100.     AccountObjs.append(AccountObj)
  101.  
  102.  
  103. def get_account_info(username):
  104.     """ Returns a dict containing information about a Account Obj """
  105.  
  106.     AccountObj = find_account(username)
  107.  
  108.     if AccountObj is None:
  109.         return None
  110.  
  111.     return AccountObj.info
  112.  
  113.  
  114. class Account:
  115.  
  116.     def __init__(self, username, password, proxy=None):
  117.         self.username = username
  118.         self.password = password
  119.  
  120.         self.proxy = proxy
  121.  
  122.         if self.proxy is None:
  123.             try:
  124.                 self.api = Client(self.username, self.password)
  125.             except Exception as e:
  126.                 print(str(e))
  127.         else:
  128.             if type(self.proxy) is list:
  129.                 self.proxy = Proxy(*proxy)
  130.             try:
  131.                 self.api = Client(self.username, self.password, self.proxy.address)
  132.             except Exception as e:
  133.                 print(str(e))
  134.  
  135.         self.info = {"username": self.username,
  136.                     "password": self.password,
  137.                     "proxy": self.proxy}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement