-
Notifications
You must be signed in to change notification settings - Fork 4
/
automation.py
103 lines (83 loc) · 2.95 KB
/
automation.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
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
import os, time
import getpass,sys
def setup():
global browser
PATH = os.environ['PATH']
pwd = os.getcwd()
os.environ['PATH'] = PATH + ':' + pwd
url = r'https://www.instagram.com/accounts/login/'
firefoxProfile = FirefoxProfile()
firefoxProfile.set_preference('permissions.default.stylesheet', 2)
firefoxProfile.set_preference('permissions.default.image', 2)
firefoxProfile.set_preference('dom.ipc.plugins.enabled.libflashplayer.so',
'false')
browser = webdriver.Firefox(firefoxProfile)
#browser.set_window_position(-10000000, 0) #move chrome away from view
print('Fetching login page..')
browser.get(url)
print('reached login page')
def login(username,password):
print('logging in..')
while True :
try:
element = browser.find_element_by_name('username')
break
except:
pass
element.clear()
element.send_keys(username)
element = browser.find_element_by_name('password')
element.clear()
element.send_keys(password)
element.send_keys(Keys.RETURN)
#check if the username and password are correct
loginTime=time.time()
while True:
if browser.current_url=='https://www.instagram.com/':
time.sleep(2)
break
#if login takes more than 10 seconds, tell user that his internet is slow
elif time.time()-loginTime>10:
print('Your Internet seems slow!\n',e)
browser.close()
sys.exit()
else:
try:
element=browser.find_element_by_xpath("//*[@id='slfErrorAlert']")
print('\n'+element.text)
print('Enter your credentials again!\n')
browser.close()
main()
sys.exit()
except:
pass
print('login complete')
def scroll_and_like(number_posts):
scroll_xpath=r"//span[text()='Like']"
print('Scrolling..')
count = 0
while (count < int(number_posts)):
try :
element = browser.find_element_by_xpath(scroll_xpath)
element.click()
count+=1
print ('Liked! Like count :', count)
except Exception as e:
browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")
def main():
keys = open('keys.txt',mode='rt')
username = keys.readline().strip()
password = keys.readline().strip()
number_posts = input('Enter the numer of posts you wish to like:\n')
start_time = time.time()
setup()
login(username,password)
scroll_and_like(number_posts)
#like(number_posts)
print('Time taken = ',time.time()-start_time)
browser.close()
if __name__=='__main__':
main()