-
Notifications
You must be signed in to change notification settings - Fork 0
/
account.py
121 lines (103 loc) · 4.17 KB
/
account.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import json
import time
from retry import retry
from steamcom.client import SteamClient
from steamcom.exceptions import SessionIsInvalid, LoginFailed
from exceptions import UserExit
from configuration import (delay_between_check_account_sessions,
save_password, login_attempts)
class Account:
def __init__(self, file_name):
self.file_name = file_name
account_data = self._read_maFile()
self.username = account_data['account_name']
self.password = account_data['password'] if 'password' \
in account_data else ''
self.shared_secret = account_data['shared_secret']
self.identity_secret = account_data['identity_secret']
self.session = account_data['Session']
self.steam_client = SteamClient(
self.username, self.password,
self.shared_secret, self.identity_secret
)
self.save_password = True if save_password == 'yes' else False
self.ask_about_password_saving = True if save_password == 'ask'\
else False
def update_maFile(self):
account_data = self._read_maFile()
self.session = self.steam_client.extract_session()
account_data['Session'] = self.session
if self.save_password:
account_data['password'] = self.password
with open(f'maFiles/{self.file_name}.maFile', 'w') as file:
json.dump(account_data, file)
def _read_maFile(self) -> dict:
with open(f'maFiles/{self.file_name}.maFile', 'r') as file:
data = file.read()
return json.loads(data)
def get_accounts(files):
accounts = []
for file in files:
if '.maFile' in file:
accounts.append(Account(file.replace('.maFile', '')))
return accounts
def check_account_sessions(accounts, flag_mode=False):
'''Logging in if the session is not valid'''
for account in accounts:
if account.steam_client.was_login_executed:
continue
try:
account.steam_client.load_session(account.session)
print(f'Account {account.username} session restored')
except SessionIsInvalid:
print(f'Saved session account {account.username} is invalid,',
'we login again...')
account_login_router(account, flag_mode)
if accounts[-1] != account:
time.sleep(delay_between_check_account_sessions)
def account_login_router(account, flag_mode=False):
while True:
if account.password == '':
ask_for_password(account)
try:
account_login(account)
return
except (LoginFailed, KeyError) as exc:
login_error_handling(account, exc, flag_mode)
@retry(LoginFailed, tries=login_attempts,
delay=delay_between_check_account_sessions)
def account_login(account):
print(f'Attempt to log in to account {account.username}')
account.steam_client.login()
print(f'Logged into the {account.username} account')
account.update_maFile()
print(f'Updated maFile from account {account.username}')
def login_error_handling(account, exc, flag_mode=False):
while True:
print(f'\nAn error occurred during login in {account.username}: {exc}')
exit_text = 'Back to account selection' if flag_mode is False \
else 'Exit'
print('0.', exit_text)
print('1. Try again')
print('2. Change password')
user_response = input('Write: ')
if user_response == '0':
if flag_mode:
exit('\nBye bye')
raise UserExit
elif user_response == '1':
return
elif user_response == '2':
account.password = ''
return
else:
print(f'\n{user_response} not found')
def ask_for_password(account):
account.password = input(
f'Enter password for account {account.username}: '
)
account.steam_client.password = account.password
if account.ask_about_password_saving:
save_password = input('Save password in maFile? Write 1 if yes,'
' any other character if not: ')
account.save_password = True if save_password == '1' else False