forked from ohld/igbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
140 lines (117 loc) · 4.18 KB
/
test.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import os
import sys
import time
import json
import pprint
from instabot import Bot
sys.path.append(os.path.join(sys.path[0], '../'))
COOKIE_FNAME = 'cookie.txt'
def _print_bot_last_state(bot):
"""Just pretty print the bot last state."""
pprint.pprint(bot.last_response, indent=4)
pprint.pprint(bot.last_response.headers, indent=4)
pprint.pprint(bot.last_json, indent=4)
def _get_challenge_choices(last_json):
"""Analise the Json response and get possible choices."""
choices = []
# Checkpoint challenge
if last_json.get('step_name', '') == 'select_verify_method':
choices.append("Checkpoint challenge received")
if 'phone_number' in last_json['step_data']:
choices.append('0 - Phone')
if 'email' in last_json['step_data']:
choices.append('1 - Email')
# Login alert challenge.
# TODO: TESTS NEEDED
if last_json.get('step_name', '') == 'delta_login_review':
choices.append("Login attempt challenge received")
choices.append('0 - It was me')
choices.append('0 - It wasn\'t me')
# If no choices found, use 1 as default.
# TODO: TESTS NEEDED
if not choices:
choices.append(
'"{}" challenge received'.format(
last_json.get('step_name', 'Unknown')))
choices.append('0 - Default')
return choices
def _reset_challenge(_bot):
"""Is recommended to reset the challenge at the beginning."""
challenge_url = _bot.last_json['challenge']['api_path'][1:]
reset_url = challenge_url.replace('/challenge/', '/challenge/reset/')
try:
_bot.send_request(reset_url, login=True)
except Exception as e:
_bot.logger.error(e)
return False
return True
def _solve_checkpoint_challenge(_bot):
"""Solve the annoying checkpoint_challenge"""
# --- Start challenge
time.sleep(3)
challenge_url = _bot.last_json['challenge']['api_path'][1:]
try:
_bot.send_request(
challenge_url, None, login=True, with_signature=False)
except Exception as e:
_bot.logger.error(e)
return False
# --- Choose and send back the choice
# TODO: Sometimes ask to confirm phone or email.
# TODO: TESTS NEEDED
time.sleep(3)
choices = _get_challenge_choices(_bot.last_json)
for choice in choices:
print(choice)
code = input('Insert choice:\n')
data = json.dumps({'choice': code})
try:
_bot.send_request(challenge_url, data, login=True)
except Exception as e:
_bot.logger.error(e)
return False
# Print output for testing
_print_bot_last_state(_bot)
# --- Wait for the code, insert the code
time.sleep(3)
print("A code has been sent to the method selected, please check.")
code = input('Insert code:\n')
data = json.dumps({'security_code': code})
try:
_bot.send_request(challenge_url, data, login=True)
except Exception as e:
_bot.logger.error(e)
return False
# Print output for testing
_print_bot_last_state(_bot)
# --- If user logged in, save cookie, otherwise PASS
worked = (
('logged_in_user' in _bot.last_json)
and (_bot.last_json.get('action', '') == 'close')
and (_bot.last_json.get('status', '') == 'ok'))
if worked:
# IMPORTANT, save the cookie at this step!
_bot.save_cookie(COOKIE_FNAME)
return True
else:
_bot.logger.error('Not possible to log in. Reset and try again')
return False
bot = Bot()
try:
bot.login(use_cookie=False)
bot.logger.info('User logged successfully, no Challenge required')
bot = Bot()
user_id = bot.get_user_id_from_username("lego")
user_info = bot.get_user_info(user_id)
print(user_info['biography'])
exit()
except Exception as e:
bot.logger.error(e)
if bot.last_json.get('error_type', '') == 'checkpoint_challenge_required':
print("Checkpoint_challenge found, attempting to solve...")
success = _solve_checkpoint_challenge(bot)
if success:
bot.login(cookie_fname=COOKIE_FNAME)
else:
print("Unknown challenge found, share the next output to get support")
_print_bot_last_state(bot)