-
Notifications
You must be signed in to change notification settings - Fork 65
/
instadm.py
365 lines (309 loc) · 15.2 KB
/
instadm.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager as CM
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from random import randint, uniform
from time import time, sleep
import logging
import sqlite3
DEFAULT_IMPLICIT_WAIT = 30
class InstaDM(object):
def __init__(self, username, password, headless=True, instapy_workspace=None, profileDir=None):
self.selectors = {
"accept_cookies": "//button[text()='Allow essential and optional cookies']",
"accept_cookies_post_login": "//button[text()='Allow all cookies']",
"home_to_login_button": "//button[text()='Log In']",
"username_field": "username",
"password_field": "password",
"button_login": "//button/*[text()='Log In']",
"login_check": "//*[@aria-label='Home'] | //button[text()='Save Info'] | //button[text()='Not Now']",
"search_user": "queryBox",
"select_user": '//div[text()="{}"]',
"name": "((//div[@aria-labelledby]/div/span//img[@data-testid='user-avatar'])[1]//..//..//..//div[2]/div[2]/div)[1]",
"next_button": "//button/*[text()='Next']",
"textarea": "//textarea[@placeholder]",
"send": "//button[text()='Send']"
}
# Selenium config
options = webdriver.ChromeOptions()
if profileDir:
options.add_argument("user-data-dir=profiles/" + profileDir)
if headless:
options.add_argument("--headless")
mobile_emulation = {
"userAgent": 'Mozilla/5.0 (Linux; Android 4.0.3; HTC One X Build/IML74K) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/99.0.4844.51 Mobile Safari/535.19'
}
options.add_experimental_option("mobileEmulation", mobile_emulation)
self.driver = webdriver.Chrome(executable_path=CM().install(), options=options)
self.driver.set_window_position(0, 0)
self.driver.set_window_size(414, 736)
# Instapy init DB
self.instapy_workspace = instapy_workspace
self.conn = None
self.cursor = None
if self.instapy_workspace is not None:
self.conn = sqlite3.connect(self.instapy_workspace + "InstaPy/db/instapy.db")
self.cursor = self.conn.cursor()
cursor = self.conn.execute("""
SELECT count(*)
FROM sqlite_master
WHERE type='table'
AND name='message';
""")
count = cursor.fetchone()[0]
if count == 0:
self.conn.execute("""
CREATE TABLE "message" (
"username" TEXT NOT NULL UNIQUE,
"message" TEXT DEFAULT NULL,
"sent_message_at" TIMESTAMP
);
""")
try:
self.login(username, password)
except Exception as e:
logging.error(e)
print(str(e))
def login(self, username, password):
# homepage
self.driver.get('https://instagram.com/?hl=en')
self.__random_sleep__(3, 5)
if self.__wait_for_element__(self.selectors['accept_cookies'], 'xpath', 10):
self.__get_element__(self.selectors['accept_cookies'], 'xpath').click()
self.__random_sleep__(3, 5)
if self.__wait_for_element__(self.selectors['home_to_login_button'], 'xpath', 10):
self.__get_element__(self.selectors['home_to_login_button'], 'xpath').click()
self.__random_sleep__(5, 7)
# login
logging.info(f'Login with {username}')
self.__scrolldown__()
if not self.__wait_for_element__(self.selectors['username_field'], 'name', 10):
print('Login Failed: username field not visible')
else:
self.driver.find_element_by_name(self.selectors['username_field']).send_keys(username)
self.driver.find_element_by_name(self.selectors['password_field']).send_keys(password)
self.__get_element__(self.selectors['button_login'], 'xpath').click()
self.__random_sleep__()
if self.__wait_for_element__(self.selectors['login_check'], 'xpath', 10):
print('Login Successful')
if self.__wait_for_element__(self.selectors['accept_cookies_post_login'], 'xpath', 10):
self.__get_element__(self.selectors['accept_cookies_post_login'], 'xpath').click()
self.__random_sleep__(2, 4)
else:
print('Login Failed: Incorrect credentials')
def createCustomGreeting(self, greeting):
# Get username and add custom greeting
if self.__wait_for_element__(self.selectors['name'], "xpath", 10):
user_name = self.__get_element__(self.selectors['name'], "xpath").text
if user_name:
greeting = greeting + " " + user_name + ", \n\n"
else:
greeting = greeting + ", \n\n"
return greeting
def typeMessage(self, user, message):
# Go to page and type message
if self.__wait_for_element__(self.selectors['next_button'], "xpath"):
self.__get_element__(self.selectors['next_button'], "xpath").click()
self.__random_sleep__()
if self.__wait_for_element__(self.selectors['textarea'], "xpath"):
self.__type_slow__(self.selectors['textarea'], "xpath", message)
self.__random_sleep__()
if self.__wait_for_element__(self.selectors['send'], "xpath"):
self.__remove_browser_unsupported_banner_if_exists()
self.__get_element__(self.selectors['send'], "xpath").click()
self.__random_sleep__(3, 5)
print('Message sent successfully')
def sendMessage(self, user, message, greeting=None):
logging.info(f'Send message to {user}')
print(f'Send message to {user}')
self.driver.get('https://www.instagram.com/direct/new/?hl=en')
self.__random_sleep__(5, 7)
try:
self.__wait_for_element__(self.selectors['search_user'], "name")
self.__type_slow__(self.selectors['search_user'], "name", user)
self.__random_sleep__(7, 10)
if greeting != None:
greeting = self.createCustomGreeting(greeting)
# Select user from list
elements = self.driver.find_elements_by_xpath(self.selectors['select_user'].format(user))
if elements and len(elements) > 0:
elements[0].click()
self.__random_sleep__()
if greeting != None:
self.typeMessage(user, greeting + message)
else:
self.typeMessage(user, message)
if self.conn is not None:
self.cursor.execute('INSERT INTO message (username, message) VALUES(?, ?)', (user, message))
self.conn.commit()
self.__random_sleep__(50, 60)
return True
# In case user has changed his username or has a private account
else:
print(f'User {user} not found! Skipping.')
return False
except Exception as e:
logging.error(e)
return False
def sendGroupMessage(self, users, message):
logging.info(f'Send group message to {users}')
print(f'Send group message to {users}')
self.driver.get('https://www.instagram.com/direct/new/?hl=en')
self.__random_sleep__(5, 7)
try:
usersAndMessages = []
for user in users:
if self.conn is not None:
usersAndMessages.append((user, message))
self.__wait_for_element__(self.selectors['search_user'], "name")
self.__type_slow__(self.selectors['search_user'], "name", user)
self.__random_sleep__()
# Select user from list
elements = self.driver.find_elements_by_xpath(self.selectors['select_user'].format(user))
if elements and len(elements) > 0:
elements[0].click()
self.__random_sleep__()
else:
print(f'User {user} not found! Skipping.')
self.typeMessage(user, message)
if self.conn is not None:
self.cursor.executemany("""
INSERT OR IGNORE INTO message (username, message) VALUES(?, ?)
""", usersAndMessages)
self.conn.commit()
self.__random_sleep__(50, 60)
return True
except Exception as e:
logging.error(e)
return False
def sendGroupIDMessage(self, chatID, message):
logging.info(f'Send group message to {chatID}')
print(f'Send group message to {chatID}')
self.driver.get('https://www.instagram.com/direct/inbox/')
self.__random_sleep__(5, 7)
# Definitely a better way to do this:
actions = ActionChains(self.driver)
actions.send_keys(Keys.TAB*2 + Keys.ENTER).perform()
actions.send_keys(Keys.TAB*4 + Keys.ENTER).perform()
if self.__wait_for_element__(f"//a[@href='/direct/t/{chatID}']", 'xpath', 10):
self.__get_element__(f"//a[@href='/direct/t/{chatID}']", 'xpath').click()
self.__random_sleep__(3, 5)
try:
usersAndMessages = [chatID]
if self.__wait_for_element__(self.selectors['textarea'], "xpath"):
self.__type_slow__(self.selectors['textarea'], "xpath", message)
self.__random_sleep__()
if self.__wait_for_element__(self.selectors['send'], "xpath"):
self.__get_element__(self.selectors['send'], "xpath").click()
self.__random_sleep__(3, 5)
print('Message sent successfully')
if self.conn is not None:
self.cursor.executemany("""
INSERT OR IGNORE INTO message (username, message) VALUES(?, ?)
""", usersAndMessages)
self.conn.commit()
self.__random_sleep__(50, 60)
return True
except Exception as e:
logging.error(e)
return False
def __get_element__(self, element_tag, locator):
"""Wait for element and then return when it is available"""
try:
locator = locator.upper()
dr = self.driver
if locator == 'ID' and self.is_element_present(By.ID, element_tag):
return WebDriverWait(dr, 15).until(lambda d: dr.find_element_by_id(element_tag))
elif locator == 'NAME' and self.is_element_present(By.NAME, element_tag):
return WebDriverWait(dr, 15).until(lambda d: dr.find_element_by_name(element_tag))
elif locator == 'XPATH' and self.is_element_present(By.XPATH, element_tag):
return WebDriverWait(dr, 15).until(lambda d: dr.find_element_by_xpath(element_tag))
elif locator == 'CSS' and self.is_element_present(By.CSS_SELECTOR, element_tag):
return WebDriverWait(dr, 15).until(lambda d: dr.find_element_by_css_selector(element_tag))
elif locator == 'CLASS' and self.is_element_present(By.CLASS_NAME, element_tag):
return WebDriverWait(dr, 15).until(lambda d: dr.find_element_by_class_name(element_tag))
else:
logging.info(f"Error: Incorrect locator = {locator}")
except Exception as e:
logging.error(e)
logging.info(f"Element not found with {locator} : {element_tag}")
return None
def is_element_present(self, how, what):
"""Check if an element is present"""
try:
self.driver.find_element(by=how, value=what)
except NoSuchElementException:
return False
return True
def __wait_for_element__(self, element_tag, locator, timeout=30):
"""Wait till element present. Max 30 seconds"""
result = False
self.driver.implicitly_wait(0)
locator = locator.upper()
for i in range(timeout):
initTime = time()
try:
if locator == 'ID' and self.is_element_present(By.ID, element_tag):
result = True
break
elif locator == 'NAME' and self.is_element_present(By.NAME, element_tag):
result = True
break
elif locator == 'XPATH' and self.is_element_present(By.XPATH, element_tag):
result = True
break
elif locator == 'CSS' and self.is_element_present(By.CSS_SELECTORS, element_tag):
result = True
break
else:
logging.info(f"Error: Incorrect locator = {locator}")
except Exception as e:
logging.error(e)
print(f"Exception when __wait_for_element__ : {e}")
sleep(1 - (time() - initTime))
else:
print(f"Timed out. Element not found with {locator} : {element_tag}")
self.driver.implicitly_wait(DEFAULT_IMPLICIT_WAIT)
return result
def __type_slow__(self, element_tag, locator, input_text=''):
"""Type the given input text"""
try:
self.__wait_for_element__(element_tag, locator, 5)
element = self.__get_element__(element_tag, locator)
actions = ActionChains(self.driver)
actions.click(element).perform()
for index, s in enumerate(input_text):
# https://stackoverflow.com/questions/53901388/how-do-i-manipulate-shiftenter-instead-of-n/53909017#53909017
if s == '\n' and index != len(input_text) - 1:
element.send_keys(Keys.SHIFT, s)
else:
element.send_keys(s)
sleep(uniform(0.25, 0.75))
except Exception as e:
logging.error(e)
print(f'Exception when __typeSlow__ : {e}')
def __random_sleep__(self, minimum=10, maximum=20):
t = randint(minimum, maximum)
logging.info(f'Wait {t} seconds')
sleep(t)
def __scrolldown__(self):
self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
def teardown(self):
self.driver.close()
self.driver.quit()
def __remove_browser_unsupported_banner_if_exists(self):
element = self.__get_element__('rh7Wz', 'CLASS')
if element is not None:
self.driver.execute_script("""
var element = arguments[0];
element.parentNode.removeChild(element);
""", element)
element = self.__get_element__('vohlx', 'CLASS')
if element is not None:
self.driver.execute_script("""
var element = arguments[0];
element.parentNode.removeChild(element);
""", element)