-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwitterbot.py
More file actions
148 lines (120 loc) · 4.37 KB
/
Copy pathtwitterbot.py
File metadata and controls
148 lines (120 loc) · 4.37 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
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.options import Options
'''Uncomment the below line when running in linux'''
# from pyvirtualdisplay import Display
import time, os
class Twitterbot:
def __init__(self, email, password, username):
"""Constructor
Arguments:
email {string} -- registered twitter email
password {string} -- password for the twitter account
"""
self.email = email
self.password = password
self.username = username
# initializing chrome options
chrome_options = Options()
# adding the path to the chrome driver and
# integrating chrome_options with the bot
self.bot = webdriver.Chrome(
executable_path = os.path.join(os.getcwd(), 'chromedriver'),
options = chrome_options
)
def login(self):
"""
Method for signing in the user
with the provided email and password.
"""
bot = self.bot
# fetches the login page
bot.get('https://twitter.com / login')
# adjust the sleep time according to your internet speed
time.sleep(5)
email = bot.find_element_by_xpath(
'//*[@name="text"]'
)
# sends the email to the email input
email.send_keys(self.email)
time.sleep(1)
# enter our username if requested for extra authentication
try:
username = bot.find_element_by_xpath(
'//*[@name="text"]'
)
username.send_keys(self.username)
time.sleep(5)
except:
print("No extra security feature requested.")
password = bot.find_element_by_xpath(
'//*[@name="password"]'
)
# sends the password to the password input
password.send_keys(self.password)
# executes RETURN key action
password.send_keys(Keys.RETURN)
time.sleep(2)
def like_retweet(self, hashtag):
"""
This function automatically retrieves
the tweets and then likes and retweets them
Arguments:
hashtag {string} -- twitter hashtag
"""
bot = self.bot
'''
# fetches the latest tweets with the provided hashtag
bot.get(
'https://twitter.com / search?q =% 23' + \
hashtag+'&src = typed_query&f = live'
)
time.sleep(3)
# using set so that only unique links
# are present and to avoid unnecessary repetition
links = set()
# obtaining the links of the tweets
for _ in range(100):
# executing javascript code
# to scroll the webpage
bot.execute_script(
'window.scrollTo(0, document.body.scrollHeight)'
)
time.sleep(4)
# using list comprehension
# for adding all the tweets link to the set
# this particular piece of code might
# look very complicated but the only reason
# I opted for list comprehension because is
# lot faster than traditional loops
[
links.add(elem.get_attribute('href'))\
for elem in bot.find_elements_by_xpath("//a[@dir ='auto']")
]
# traversing through the generated links
for link in links:
# opens individual links
bot.get(link)
time.sleep(4)
try:
# retweet button selector
bot.find_element_by_css_selector(
'.css-18t94o4[data-testid ="retweet"]'
).click()
# initializes action chain
actions = ActionChains(bot)
# sends RETURN key to retweet without comment
actions.send_keys(Keys.RETURN).perform()
# like button selector
bot.find_element_by_css_selector(
'.css-18t94o4[data-testid ="like"]'
).click()
# adding higher sleep time to avoid
# getting detected as bot by twitter
time.sleep(10)
except:
time.sleep(2)
'''
# fetches the main homepage
bot.get('https://twitter.com/')