-
Notifications
You must be signed in to change notification settings - Fork 20
/
prepare.py
83 lines (70 loc) · 2.7 KB
/
prepare.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python
import os
import sys
import getpass
SECRET_FILE = "secret.txt"
def add_credentials():
with open(SECRET_FILE, "a") as f:
print("Enter your login: ")
f.write(str(sys.stdin.readline().strip()) + ":")
print("Enter your password: ")
f.write(getpass.getpass() + "\n")
def get_credentials(username=None):
"Returns login and password stored in SECRET_FILE"
while not check_secret():
pass
while True:
with open(SECRET_FILE, "r") as f:
lines = [line.strip().split(":", 2) for line in f.readlines()]
if username is not None:
for login, password in lines:
if login == username.strip():
return login, password
print("Which account do you want to use? (Type number)")
for ind, (login, password) in enumerate(lines):
print("%d: %s" % (ind + 1, login))
print("%d: %s" % (0, "add another account."))
print("%d: %s" % (-1, "delete all accounts."))
try:
ind = int(sys.stdin.readline())
if ind == 0:
add_credentials()
continue
if ind == -1:
delete_credentials()
check_secret()
continue
if ind - 1 in list(range(len(lines))):
return lines[ind - 1]
except Exception:
print("Wrong input. I need the number of account to use.")
def check_secret():
while True:
if os.path.exists(SECRET_FILE):
with open(SECRET_FILE, "r") as f:
try:
login, password = f.readline().strip().split(":")
if len(login) < 4 or len(password) < 6:
print("Data in 'secret.txt' file is invalid. "
"We will delete it and try again.")
os.remove(SECRET_FILE)
else:
return True
except Exception:
print("Your file is broken. We will delete it "
"and try again.")
os.remove(SECRET_FILE)
else:
print("We need to create a text file '%s' where "
"we will store your login and password from Instagram." % SECRET_FILE)
print("Don't worry. It will be stored locally.")
while True:
add_credentials()
print("Do you want to add another account? (y/n)")
if "y" not in sys.stdin.readline():
break
def delete_credentials():
if os.path.exists(SECRET_FILE):
os.remove(SECRET_FILE)
if __name__ == "__main__":
check_secret()