-
Notifications
You must be signed in to change notification settings - Fork 1
/
insta.py
184 lines (157 loc) · 6.73 KB
/
insta.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
#!/usr/bin/env Python3
from time import sleep
import json
import os
import autoit
import settings
import insta_scraper
from selenium import webdriver
# from selenium.webdriver.firefox.options import Options
# import ninegag
# Variables
driver = ""
p = os.getcwd()
def launch_inst():
global driver
print("Opening instagram")
# Chrome browser options
mobile_emulation = {"userAgent": "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/90.0.1025.166 Mobile Safari/535.19"}
opts = webdriver.ChromeOptions()
opts.add_argument("window-size=1,765")
opts.add_experimental_option("mobileEmulation", mobile_emulation)
driver = webdriver.Chrome(executable_path= p + "/chromedriver.exe", options=opts)
driver.set_window_size(600, 1000)
# Opens Instagram
main_url = "https://www.instagram.com"
driver.get(main_url)
sleep(10)
ordered_functions()
# FireFox Options
# chrome_options = Options()
# chrome_options.add_argument('--no-sandbox')
# chrome_options.add_argument('--disable-dev-shm-usage')
# chrome_options.add_argument(
# '--user-agent=Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) CriOS/56.0.2924.75 Mobile/14E5239e Safari/602.1')
# p = os.getcwd()
# browser = webdriver.Firefox(executable_path=p + "/geckodriver.exe", options=chrome_options)
# browser.set_window_size(600, 800)
# browser.get('https://www.instagram.com')
def login():
# Logs into instagram
print("Logging into Instagram")
driver.find_element_by_xpath("//button[contains(text(),'Log In')]").click()
# checks if user selected to login through facebook or regular method
if settings.login_type == "Facebook":
print("Going through Facebook")
driver.find_element_by_xpath("//button[contains(text(),'Continue with Facebook')]").click()
sleep(settings.wait_time)
driver.find_element_by_xpath("//input[@name='email']").send_keys(settings.username)
driver.find_element_by_xpath("//input[@name='pass']").send_keys(settings.password)
sleep(settings.wait_time)
driver.find_element_by_xpath("//button[@name='login']").click()
else:
sleep(settings.wait_time)
driver.find_element_by_xpath("//input[@name='username']").send_keys(settings.username)
driver.find_element_by_xpath("//input[@name='password']").send_keys(settings.password)
sleep(settings.wait_time)
driver.find_element_by_xpath("//button[@type='submit']").click()
def remove_popups():
print("Removing any popups")
try:
driver.find_element_by_xpath("//a[contains(text(),'Not Now')]").click()
except:
try:
driver.find_element_by_xpath("//button[contains(text(),'Not Now')]").click()
except:
try:
driver.find_element_by_xpath("//button[contains(text(),'Cancel')]").click()
except:
pass
def add_post():
# clicks add post button
try:
driver.find_element_by_xpath("//div[@role='menuitem']").click()
except:
pass
def post():
print("Adding post")
# Readies the content for instagram
with open('filesDict.json', encoding="utf8") as json_file:
# loads the data from the queue (filesDict.json)
data = json.load(json_file)
# if not empty
if bool(data):
# gets first item in filesDict.json and sets it as the next instagram upload
# also sets the image path and caption
image_path = os.getcwd() + "\\images\\" + data['dict'][0]['id'] + ".jpg"
# Enter your custom tags and caption here !
caption = "Now Go & Code some Python ."
# Loops and removes the first item since it has just been uploaded
for i in range(len(data)):
if data['dict'][i]["id"] == data['dict'][0]['id']:
del data['dict'][i]
break
# saves file without first item
with open('filesDict.json', 'w', encoding="utf8") as outfile:
json.dump(data, outfile)
# Opens File Explore window
print("Opening file explorer")
autoit.win_active("Open")
autoit.control_set_text("Open", "Edit1", image_path)
autoit.control_send("Open", "Edit1", "{ENTER}")
sleep(settings.wait_time)
# depending on aspect ratio, sometimes this button does not exist
try:
driver.find_element_by_xpath("//span[contains(text(),'Expand')]").click()
except:
pass
sleep(settings.wait_time)
# clicks through options and adds caption after file is added
driver.find_element_by_xpath("//button[contains(text(),'Next')]").click()
sleep(settings.wait_time)
caption_field = driver.find_element_by_xpath("//textarea[@aria-label='Write a caption…']")
caption_field.send_keys(caption)
driver.find_element_by_xpath("//button[contains(text(),'Share')]").click()
# Executes initial login and removing pop ups functions in order
def ordered_functions():
login()
sleep(settings.wait_time)
remove_popups()
sleep(settings.wait_time)
remove_popups()
sleep(settings.wait_time)
remove_popups()
def loop_posting():
while True:
# Loops through at a set interval and adds another photo depending on the method the user chose
if settings.post_source == "9gag":
ninegag.get_data()
else:
# if keep_checking is true it will keep checking for new images else it will stop the program
if settings.keep_checking or settings.counter == 0:
print("Who summonded me ?")
insta_scraper.get_data()
else:
print("Program is done getting and posting previous user posts")
break
with open('filesDict.json', encoding="utf8") as data_file:
settings.filesDict = json.load(data_file)
# checks if post limit has been reached and queue is not empty
if settings.filesDict['dict'] and settings.counter < settings.post_limit:
remove_popups()
sleep(settings.wait_time)
add_post()
sleep(settings.wait_time)
post()
sleep(settings.wait_time)
remove_popups()
settings.counter += 1
print("Posting again in: " + str(settings.post_time))
# if max post limit has been reached
elif settings.counter >= settings.post_limit:
print("The program has reached the max post limit of: " + str(settings.post_limit))
break
# if max post limit has not been reached but there are no new images
else:
print("Waiting for new images")
sleep(int(settings.post_time))