Skip to content
This repository has been archived by the owner on Feb 25, 2024. It is now read-only.

base_path option fixes #1424

Merged
merged 2 commits into from
Apr 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions instabot/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,9 @@ def login(
is_threaded=False,
):
if password is None:
username, password = get_credentials(username=username)
username, password = get_credentials(
base_path=self.base_path, username=username
)

set_device = generate_all_uuids = True
self.set_user(username, password)
Expand Down Expand Up @@ -352,7 +354,7 @@ def login(
"Failed to login go to instagram and change your password"
)
self.save_failed_login()
delete_credentials()
delete_credentials(self.base_path)
return False

def two_factor_auth(self):
Expand Down Expand Up @@ -402,7 +404,7 @@ def save_successful_login(self):

def save_failed_login(self):
self.logger.info("Username or password is incorrect.")
delete_credentials()
delete_credentials(self.base_path)
sys.exit()

def solve_challenge(self):
Expand Down Expand Up @@ -588,7 +590,7 @@ def send_request(
self.logger.error(
"Since we hit 15 minutes of time outs, we have to restart. Removing session and cookies. Please relogin."
)
delete_credentials()
delete_credentials(self.base_path)
sys.exit()
timeout_minutes += 5
self.logger.warning(
Expand All @@ -612,7 +614,7 @@ def send_request(
self.logger.error(
"Failed to login go to instagram and change your password"
)
delete_credentials()
delete_credentials(self.base_path)
# PERFORM Interactive Two-Factor Authentication
if response_data.get("two_factor_required"):
try:
Expand Down
27 changes: 17 additions & 10 deletions instabot/api/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
import os
import sys

current_path = os.path.abspath(os.getcwd())
SECRET_FILE = current_path + "/config/secret.txt"
DEFAULT_SECRET_DIR = os.path.abspath(os.getcwd())


def add_credentials():
def get_credential_file(base_path=DEFAULT_SECRET_DIR):
return base_path + "/config/secret.txt"


def add_credentials(base_path):
SECRET_FILE = get_credential_file(base_path)
with open(SECRET_FILE, "a") as f:
print("Enter your login: ")
f.write(str(sys.stdin.readline().strip()) + ":")
Expand All @@ -19,7 +23,8 @@ def add_credentials():
f.write(getpass.getpass() + "\n")


def get_credentials(username=None):
def get_credentials(base_path, username=None):
SECRET_FILE = get_credential_file(base_path)
"""Returns login and password stored in `secret.txt`."""
while not check_secret():
pass
Expand All @@ -42,19 +47,20 @@ def get_credentials(username=None):
try:
ind = int(sys.stdin.readline())
if ind == 0:
add_credentials()
add_credentials(base_path)
continue
elif ind == -1:
delete_credentials()
check_secret()
delete_credentials(base_path)
check_secret(base_path)
continue
elif 0 <= ind - 1 < len(lines):
return lines[ind - 1]
except Exception:
print("Wrong input, enter the number of the account to use.")


def check_secret():
def check_secret(base_path):
SECRET_FILE = get_credential_file(base_path)
while True:
if os.path.exists(SECRET_FILE):
with open(SECRET_FILE, "r") as f:
Expand All @@ -80,13 +86,14 @@ def check_secret():
)
print("Don't worry. It will be stored locally.")
while True:
add_credentials()
add_credentials(base_path)
print("Do you want to add another account? (y/n)")
if "y" not in sys.stdin.readline():
break


def delete_credentials():
def delete_credentials(base_path):
SECRET_FILE = get_credential_file(base_path)
if os.path.exists(SECRET_FILE):
os.remove(SECRET_FILE)

Expand Down
3 changes: 1 addition & 2 deletions instabot/bot/bot_checkpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
import pickle
from datetime import datetime

current_path = os.path.abspath(os.getcwd())
CHECKPOINT_PATH = current_path + "/config/{fname}.checkpoint"
CHECKPOINT_PATH = "config/{fname}.checkpoint"


class Checkpoint(object):
Expand Down