-
Notifications
You must be signed in to change notification settings - Fork 0
/
instagram_upload.py
137 lines (129 loc) · 5.77 KB
/
instagram_upload.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
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait as wait
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from os import listdir
from time import sleep
print("Enter username: ", end='')
uname = input()
print("Enter password: ", end='')
pword = input()
print("Enter a suitable caption for all uploads: ")
CAPTION = input()
#CAPTION = "Follow me for more\n\n#memesdarkerthan2020 #dankmemes #darkmemes #darkhumor #edgymemes #memesfordays #memesforfeens #dankmemesgang #nicememes #funnytexts #memestgram #darkmemes #legendarymemes #memegod #memepage #memepages #psatmemes #memer #softmemes #memefactory #dailymeme #highschoolmemes #bossmemesquad #memepage #funnyaccount #dankdank #memeaccount #memesdaily #memes "
INTERVAL = 1
cap = 4
print("Loading webdriver...")
opts = Options()
opts.add_argument("--headless")
driver = webdriver.Firefox(options = opts)
count = 1
#logging in
driver.get("https://www.instagram.com")
print("Logging in...")
wait(driver, 20).until(EC.element_to_be_clickable((By.NAME, "username"))).send_keys(uname)
driver.find_element(By.NAME, "password").send_keys(pword)
driver.find_element(By.XPATH, "//button[@type='submit']").click()
sleep(3)
driver.get("https://www.instagram.com")
try:
wait(driver, 35).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='_a9-- _ap36 _a9_1']"))).click() #Not Now button
except:
print("Warning: Notification popup not detected.")
files = listdir("./instagram_uploads")
print("Starting..")
while(True):
already_uploaded = []
with open(f"./instagram_upload.var", "r") as f:
lines = f.readlines()
if len(lines) >= 1:
already_uploaded = [x.replace("\n", "") for x in lines]
for already_uploaded_file in already_uploaded:
if already_uploaded_file in files:
files.remove(already_uploaded_file)
try:
for file in files:
driver.find_element(By.XPATH, "//*[contains(text(), 'Create')]").click()
if(file.endswith('.mp4')):
wait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//input[@type='file']"))).send_keys("K:\\dev\\rip\\uploads\\"+file)
wait(driver, 60).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'OK')]"))).click()
else:
wait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//input[@type='file']"))).send_keys("K:\\dev\\rip\\uploads\\"+file)
wait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[contains(text(), 'Next')]")))
btns = driver.find_elements(By.XPATH, "//button[@class=' _acan _acao _acas _aj1- _ap30']")
c=0
for i in btns:
try:
c=1
i.click()
except:
c=0
if c==1:
break
sleep(1)
btns = driver.find_elements(By.XPATH, "//span[contains(text(), 'Original')]")
c=0
for i in btns:
try:
c=1
i.click()
except:
c=0
if c==1:
break
sleep(0.7)
wait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[contains(text(), 'Next')]"))).click()
wait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[contains(text(), 'Next')]"))).click()
sleep(1)
for char in CAPTION:
if (char == '\n'):
driver.find_element(By.XPATH, "//div[@role='textbox']").send_keys(Keys.RETURN)
else:
driver.find_element(By.XPATH, "//div[@role='textbox']").send_keys(char)
sleep(0.03)
sleep(1)
driver.find_element(By.XPATH, "//div[contains(text(), 'Share')]").click()
sleep(4.5)
already_uploaded.append(file)
with open(f"./instagram_upload.var", "a") as f:
f.write(f"{file}\n")
btns = driver.find_elements(By.XPATH, "//div[@class='x1i10hfl x972fbf xcfux6l x1qhh985 xm0m39n x9f619 xe8uvvx xdj266r x11i5rnm xat24cr x1mh8g0r x16tdsg8 x1hl2dhg xggy1nq x1a2a7pz x6s0dn4 xjbqb8w x1ejq31n xd10rxx x1sy0etr x17r0tee x1ypdohk x78zum5 xl56j7k x1y1aw1k x1sxyh0 xwib8y2 xurb0ha xcdnw81']")
c=0
for i in btns:
try:
c=1
i.click()
except:
c=0
if c==1:
break
if(count>=cap):
break
print(f"Upload complete: {file} ({count}/{min(cap, len(files))})")
count+=1
sleep(INTERVAL)
print(f"Task completed: Uploaded {count} out of {min(cap, len(files))} files.")
break
except KeyboardInterrupt:
print(f"Task interrupted: Uploaded {count-1} out of {min(cap, len(files))} files.")
print("Do you want to terminate the task? (y/n): ", end='')
d = input()
if d=="y":
print("Task terminated.")
break
else:
print("Continuing...")
continue
except:
print("Encountered an error. Retrying...")
driver.get("https://www.instagram.com") #reload page
try:
wait(driver, 35).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='_a9-- _ap36 _a9_1']"))).click() #Not Now button
except:
print("Warning: Notification popup not detected.")
sleep(3)
continue
driver.quit()