-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend_msg.py
More file actions
295 lines (241 loc) · 11.1 KB
/
send_msg.py
File metadata and controls
295 lines (241 loc) · 11.1 KB
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
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import *
import time
from utility_methods.utility_methods import *
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
import random
import json
import urllib.request
import os
class InstaBot:
def __init__(self, username=None, password=None, arg1=False):
""""
Creates an instance of InstaBot class.
Args:
username:str: The username of the user, if not specified, read from configuration.
password:str: The password of the user, if not specified, read from configuration.
arg1: boolean: True for launching chrome headless/ False for not
"""
self.username = 'kasper_ostbergg'
self.password = 'Money1798'
self.login_url = config['IG_URLS']['LOGIN']
self.nav_user_url = config['IG_URLS']['NAV_USER']
self.get_tag_url = config['IG_URLS']['SEARCH_TAGS']
options = webdriver.ChromeOptions()
options.add_argument("--window-size=400,850")
options.add_argument('disable-infobars')
options.add_argument('--lang=en')
mobile_emulation = {"deviceName": "iPhone 7"}
options.add_experimental_option("mobileEmulation", mobile_emulation)
if arg1:
options.add_argument('headless')
self.driver = webdriver.Chrome(options=options, executable_path='./chromedriver.exe')
self.logged_in = False
# tools
def add_msgd_users(self, msgd_user):
# load messaged users
msgd_users = self.data_handling('./messaged_users.json')
# append user to list
msgd_users.append(msgd_user)
# save new list
with open('./messaged_users.json', 'w') as file:
json.dump(msgd_users, file)
def high_value_accounts(self):
dir_path = input('Enter the path to desired folder to iterate over (either local or absolute): ')
name = input('Enter the unique name of your refined list: ')
# iterate over files in dir and create object
directory = os.fsencode(dir_path)
master_object = {}
for file in os.listdir(directory):
filename = os.fsdecode(file)
if filename.endswith(".json"):
json_file = dir_path + "/" + filename
json_object = self.data_handling(json_file)
master_object = self.mergeDict(master_object, json_object)
# handle numbers
for key, value in master_object.items():
# check if post is video
if 'post_is_video' in value:
pass
else:
for innerKey, innerValue in master_object[key].items():
if type(innerValue) is int:
pass
elif type(innerValue) is str:
master_object[key][innerKey] = int(innerValue.replace(',', ''))
else:
pass
# refine the accounts by a lot of statements
refined_accounts = {}
for key, value in master_object.items():
# check if post is video
if 'post_is_video' in value:
# videos always perform well
refined_accounts.update({key: value})
else:
like_ratio = round((value['post_likes'] / value['followers']), 4)
# refine the accounts by a lot of statements
if like_ratio > 0.15:
refined_accounts.update({key: value})
if value['post_likes'] > 5000 and value['followers'] < 25000:
refined_accounts.update({key: value})
if value['post_likes'] > 25000 and value['followers'] < 50000:
refined_accounts.update({key: value})
self.save_usernames(refined_accounts, 'refined_list/{}'.format(name))
def mergeDict(self, dict1, dict2):
# Merge dictionaries and keep values of common keys in list
dict3 = {**dict1, **dict2}
for key, value in dict3.items():
if key in dict1 and key in dict2:
if value == dict1[key]:
pass
else:
likes = [int(value["post_likes"].replace(',', '')), int(dict1[key]['post_likes'].replace(',', ''))]
followers = [int(value["followers"].replace(',', '')),
int(dict1[key]["followers"].replace(',', ''))]
dict3[key] = {'post_likes': max(likes), 'followers': max(followers)}
return dict3
def does_json_exist(self, filename):
try:
f = open('./JSON_lists/{}.json'.format(filename))
f.close()
return True
except IOError:
print("Success, filename doesn't exist")
return False
def data_handling(self, path):
jsonFile = open(path)
jsonString = jsonFile.read()
jsonData = json.loads(jsonString)
return jsonData
def save_usernames(self, variable, filename):
if self.does_json_exist(filename):
pass
else:
with open('./JSON_lists/{}.json'.format(filename), 'w') as ftp:
json.dump(list(variable.keys()), ftp)
def random_time(self, amount=2):
if amount == 1:
return round((random.random() + 1), 4)
elif amount == 2:
return round((random.random() * 5 + 15), 2)
elif amount == 3:
return round((random.random() * 10 + 30), 2)
elif amount == 4:
return round((random.random() * 20 + 40), 2)
def wait_for_element(self, xpath):
try:
WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.XPATH, xpath)))
except TimeoutException:
print("Wat da hel!")
def does_element_exist(self, xpath):
try:
self.driver.find_element_by_xpath(xpath)
return True
except NoSuchElementException:
return False
def add_to_homescreen(self):
if self.does_element_exist('//div[@role="presentation"]'):
self.driver.find_element_by_xpath('//button[contains(text(), "Cancel")]').click()
else:
pass
def remember_me(self):
if self.does_element_exist('//button[contains(text(), "Not Now")]'):
self.driver.find_element_by_xpath('//button[contains(text(), "Not Now")]').click()
else:
pass
def activate_notifications(self):
self.wait_for_element('//div[@role="presentation"]//button[contains(text(), "Not Now")]')
if self.does_element_exist('//div[@role="presentation"]'):
self.driver.find_element_by_xpath(
'//div[@role="presentation"]//button[contains(text(), "Not Now")]').click()
else:
pass
# actions
def login(self):
"""
Logs a user into Instagram via the web portal
"""
self.driver.get(self.login_url)
time.sleep(self.random_time())
login_btn = self.driver.find_element_by_xpath(
'//*[@id="react-root"]/section/main/article/div/div/div/form/div[7]/button')
username_input = self.driver.find_element_by_name('username')
password_input = self.driver.find_element_by_name('password')
username_input.send_keys(self.username)
password_input.send_keys(self.password)
login_btn.click()
# wait for header to load
self.wait_for_element('//header/div/h1/div') # random element to wait for
time.sleep(5.32)
# check for remember me popup + remove it
self.remember_me()
time.sleep(5.7)
# wait for homepage to load
self.wait_for_element('//a[@href="/explore/"]') # random element to wait for
time.sleep(4.6)
# check for add to homescreen popup + remove it
self.add_to_homescreen()
time.sleep(2.7)
def msg_users(self, user, message="Hey, would you be interested in selling you account?"):
print(user)
time.sleep(self.random_time(3))
self.wait_for_element('//h1[contains(text(), "Direct")]/../div[2]/button')
self.driver.find_element_by_xpath('//h1[contains(text(), "Direct")]/../div[2]/button').click()
time.sleep(self.random_time())
self.wait_for_element('//input[@placeholder="Search..."]')
self.driver.find_element_by_xpath('//input[@placeholder="Search..."]').send_keys(user)
time.sleep(self.random_time())
self.wait_for_element('//*[@id="react-root"]/section/div[2]/div/div[2]/div[1]')
element = self.driver.find_elements_by_xpath('//div[contains(text(), "{}")]/../../../..'.format(user))
element[0].click()
self.wait_for_element('//h1[contains(text(), "New Message")]/..//button')
if self.does_element_exist('//h1[contains(text(), "New Message")]/..//button[@disabled]'):
pass
elif user not in self.data_handling('messaged_users.json'):
time.sleep(self.random_time())
self.wait_for_element('//button[contains(text(), "Next")]')
self.driver.find_element_by_xpath('//button[contains(text(), "Next")]').click()
time.sleep(self.random_time())
self.wait_for_element('//textarea[@placeholder="Message..."]')
self.driver.find_element_by_xpath('//textarea[@placeholder="Message..."]').send_keys(message)
time.sleep(self.random_time(1))
self.wait_for_element('//button[contains(text(), "Send")]')
self.driver.find_element_by_xpath('//button[contains(text(), "Send")]').click()
# add to messaged users list to prevent double messaging
self.add_msgd_users(user)
time.sleep(self.random_time())
self.driver.find_element_by_xpath('//a[@href="/direct/new/"]').click()
time.sleep(1)
self.wait_for_element('//a[@href="/direct/inbox/"]')
self.driver.find_element_by_xpath('//a[@href="/direct/inbox/"]').click()
time.sleep(self.random_time(1))
else:
print('Double message prevented. User: {}'.format(user))
# action invokers
def start_messaging(self, json_list):
with open(json_list) as f:
users = json.load(f)
# navigate to inbox
self.wait_for_element('//a[@href="/direct/inbox/"]')
self.driver.find_element_by_xpath('//a[@href="/direct/inbox/"]').click()
time.sleep(self.random_time())
# check for notifications popup + remove it
self.activate_notifications()
# load different messages to be sent
jsonFile = open('./messages.json')
jsonString = jsonFile.read()
jsonData = json.loads(jsonString)
for user in users:
self.msg_users(user, jsonData[0])
if __name__ == '__main__':
config_file_path = './config.ini'
logger_file_path = './bot.log'
config = init_config(config_file_path)
bot = InstaBot()
bot.login()
bot.start_messaging('./JSON_lists/refined_list/Veeti/sicko_list.json')