from
selenium
import
webdriver
from
selenium.webdriver.common.keys
import
Keys
from
selenium.webdriver
import
ActionChains
from
selenium.webdriver.chrome.options
import
Options
import
time, os
class
Twitterbot:
def
__init__(
self
, email, password):
self
.email
=
email
self
.password
=
password
chrome_options
=
Options()
self
.bot
=
webdriver.Chrome(
executable_path
=
os.path.join(os.getcwd(),
'chromedriver'
),
options
=
chrome_options
)
def
login(
self
):
bot
=
self
.bot
time.sleep(
3
)
email
=
bot.find_element_by_xpath(
'//*[@id ="react-root"]/div / div / div[2]/main / div / div / form / div / div[1]/label / div / div[2]/div / input'
)
password
=
bot.find_element_by_xpath(
'//*[@id ="react-root"]/div / div / div[2]/main / div / div / form / div / div[2]/label / div / div[2]/div / input'
)
email.send_keys(
self
.email)
password.send_keys(
self
.password)
password.send_keys(Keys.RETURN)
time.sleep(
2
)
def
like_retweet(
self
, hashtag):
bot
=
self
.bot
bot.get(
hashtag
+
'&src = typed_query&f = live'
)
time.sleep(
3
)
links
=
set
()
for
_
in
range
(
100
):
bot.execute_script(
'window.scrollTo(0, document.body.scrollHeight)'
)
time.sleep(
4
)
[
links.add(elem.get_attribute(
'href'
))\
for
elem
in
bot.find_elements_by_xpath(
"//a[@dir ='auto']"
)
]
for
link
in
links:
bot.get(link)
time.sleep(
4
)
try
:
bot.find_element_by_css_selector(
'.css-18t94o4[data-testid ="retweet"]'
).click()
actions
=
ActionChains(bot)
actions.send_keys(Keys.RETURN).perform()
bot.find_element_by_css_selector(
'.css-18t94o4[data-testid ="like"]'
).click()
time.sleep(
10
)
except
:
time.sleep(
2
)