-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
85 lines (63 loc) · 2.84 KB
/
bot.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
from selenium import webdriver
from time import sleep
import random
from secret import username, password
from tweets import duckTweets
class TwitterBot:
def __init__(self):
self.driver = webdriver.Chrome()
def logIn(self, username, password):
self.driver.get("https://twitter.com")
sleep(2)
logInxPath = "/html/body/div[1]/div/div[1]/div[1]/div[2]/div[2]/div/a[2]"
logIn = self.driver.find_element_by_xpath(logInxPath)
logIn.click()
sleep(2)
uNamexPath = "/html/body/div[1]/div[2]/div/div/div[1]/form/fieldset/div[1]/input"
uName = self.driver.find_element_by_xpath(uNamexPath)
uName.send_keys(username)
sleep(2)
pWordxPath = "/html/body/div[1]/div[2]/div/div/div[1]/form/fieldset/div[2]/input"
pWord = self.driver.find_element_by_xpath(pWordxPath)
pWord.send_keys(password)
sleep(2)
logxPath = "/html/body/div[1]/div[2]/div/div/div[1]/form/div[2]/button"
log = self.driver.find_element_by_xpath(logxPath)
log.click()
sleep(5)
def retweet(self):
driver = self.driver
#Find tweet xpath, rt if above a certain rt count
tweets = driver.find_elements_by_xpath("//div[@data-testid='tweet']")
rtButtons = driver.find_elements_by_xpath("//div[@data-testid='retweet']")
#Doesn't work yet, rtButtons not returning anything
for rtButton in rtButtons:
rtButton.click()
sleep(2)
confirm = driver.find_element_by_xpath("//div[@data-testid='retweetConfirm']")
confirm.click()
sleep(2)
def findQuotes(self):
driver = self.driver
driver.get("https://www.brainyquote.com/topics/duck-quotes")
quotes = driver.find_elements_by_xpath()
def tweet(self):
driver = self.driver
#Select tweet button
tweetButton = driver.find_element_by_xpath("/html/body/div/div/div/div/header/div/div/div/div/div[3]/a/div")
tweetButton.click()
#Select blank, send tweet
blankLine = driver.find_element_by_xpath("/html/body/div/div/div/div[1]/div/div/div/div/div[2]/div[2]/div/div[3]/div/div/div[1]/div/div/div/div[2]/div[1]/div/div/div/div/div/div/div/div[1]/div[1]/div/div/div/div[2]/div/div/div/div")
tweet = random.choice(duckTweets)
blankLine.send_keys(tweet)
#Submit tweet
submitButton = driver.find_element_by_xpath("/html/body/div/div/div/div[1]/div/div/div/div/div[2]/div[2]/div/div[3]/div/div/div[1]/div/div/div/div[2]/div[2]/div/div/div[2]/div[4]/div/span/span")
submitButton.click()
#scrolling:
# for i in range(1, 2):
# driver.execute_script('window.scrollTo(0, document.body.scrollHeight)')
# sleep(2)
bot = TwitterBot()
bot.logIn(username, password)
#bot.findQuotes()
bot.retweet()