Get the internet speed checekd, open twitter to write about it. Use class to remove the repetition.
This was a meaningful project. I struggled a lot with OOP and execution. I finally wrote all the code without checking Angela's solution code.
I didn't need to return the speed result at def get_internet_speed() . I should've set up innitial value as 0 for both self.up and self.down at def init(). Later, at def get_internet_speed(), update the value with scraped text. Then, tweet_at_provider() can refer to the same value from init().
(Compare my code and Angela's below)
My code
class InternetSpeedTwitterBot:
def __init__(self):
self.chrome_driver_path = CHROME_DRIVER_PATH
self.driver = webdriver.Chrome(executable_path=self.chrome_driver_path)
self.down = PROMISED_DOWN
self.up = PROMISED_UP
self.speed = self.get_internet_speed()
def get_internet_speed(self):
~
return {"down":f"{down.text}", "up":f"{up.text}"}
def tweet_at_provider(self, down, up):
write.send_keys(
f"Hey Internet Provider,
why is my internet speed {down} down/ {up} up,
When I pay for {self.down}/ {self.up}?")
bot = InternetSpeedTwitterBot()
bot.tweet_at_provider(down=bot.speed["down"], up=bot.speed["up"])
Angela's
class InternetSpeedTwitterBot:
def __init__(self, driver_path):
self.driver = webdriver.Chrome(executable_path=driver_path)
self.up = 0
self.down = 0
def get_internet_speed(self):
self.up = self.driver.find_element_by_xpath('//*[@id="container"]~.text
self.down = self.driver.find_element_by_xpath('//*[@id="container"]~.text
def tweet_at_provider(self):
tweet = f"Hey Internet Provider,
why is my internet speed {self.down}down/{self.up}up
when I pay for {PROMISED_DOWN}down/{PROMISED_UP}up?"
tweet_compose.send_keys(tweet)
bot = InternetSpeedTwitterBot(CHROME_DRIVER_PATH)
bot.get_internet_speed()
bot.tweet_at_provider()
from selenium import webdriver
import time
PROMISED_DOWN = 1000
PROMISED_UP = 500
CHROME_DRIVER_PATH = "\chromedriver.exe"
TWITTER_EMAIL = "-@gmail.com"
TWITTER_PASSWORD="-"
id = "L"
class InternetSpeedTwitterBot:
def __init__(self):
self.chrome_driver_path = CHROME_DRIVER_PATH
self.driver = webdriver.Chrome(executable_path=self.chrome_driver_path)
self.down = PROMISED_DOWN
self.up = PROMISED_UP
self.speed = self.get_internet_speed()
def get_internet_speed(self):
self.driver.get("https://www.speedtest.net/")
time.sleep(5)
#hit GO
go = self.driver.find_element_by_css_selector("div.start-button")
go.click()
time.sleep(70)
#get speed checked
down = self.driver.find_element_by_class_name("download-speed")
up = self.driver.find_element_by_class_name("upload-speed")
print(f"down: {down.text}")
print(f"up: {up.text}")
return {"down":f"{down.text}", "up":f"{up.text}"}
def tweet_at_provider(self, down, up):
self.driver.get("https://twitter.com/")
time.sleep(10)
# hit GO
go = self.driver.find_element_by_link_text("로그인")
go.click()
time.sleep(3)
# login
email = self.driver.find_element_by_name("session[username_or_email]")
email.send_keys(TWITTER_EMAIL)
password = self.driver.find_element_by_name("session[password]")
password.send_keys(TWITTER_PASSWORD)
time.sleep(3)
login = self.driver.find_element_by_xpath("//*[@id='react-root']/div/div/div[2]/main/div/div/div[2]/form/div/div[3]")
login.click()
time.sleep(5)
#2 2nd login for verification
email_2 = self.driver.find_element_by_name("session[username_or_email]")
email_2.send_keys(id)
password_2 = self.driver.find_element_by_name("session[password]")
password_2.send_keys(TWITTER_PASSWORD)
time.sleep(3)
login_2 = self.driver.find_element_by_xpath("//*[@id='react-root']/div/div/div[2]/main/div/div/div[2]/form/div/div[3]")
login_2.click()
time.sleep(5)
# write
write = self.driver.find_element_by_xpath(
"//*[@id='react-root']/div/div/div[2]/main/div/div/div/div/div/div[2]/div/div[2]/div[1]/div/div/div/div[2]"
"/div[1]/div/div/div/div/div/div/div/div/div/div[1]/div/div/div/div[2]/div/div/div/div")
write.send_keys(f"Hey Internet Provider, why is my internet speed {down} down/ {up} up, When I pay for {self.down}/ {self.up}?")
time.sleep(10)
tweet = self.driver.find_element_by_xpath(
"//*[@id='react-root']/div/div/div[2]/main/div/div/div/div/div/div[2]/div/div[2]/div[1]/div/div/div/div[2]"
"/div[4]/div/div/div[2]/div[3]/div/span/span")
tweet.click()
bot = InternetSpeedTwitterBot()
bot.tweet_at_provider(down=bot.speed["down"], up=bot.speed["up"])
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
PROMISED_DOWN = 150
PROMISED_UP = 10
CHROME_DRIVER_PATH = YOUR CHROME DRIVER PATH
TWITTER_EMAIL = YOUR TWITTER EMAIL
TWITTER_PASSWORD = YOUR TWITTER PASSWORD
class InternetSpeedTwitterBot:
def __init__(self, driver_path):
self.driver = webdriver.Chrome(executable_path=driver_path)
self.up = 0
self.down = 0
def get_internet_speed(self):
self.driver.get("https://www.speedtest.net/")
# accept_button = self.driver.find_element_by_id("_evidon-banner-acceptbutton")
# accept_button.click()
# time.sleep(3)
go_button = self.driver.find_element_by_css_selector(".start-button a")
go_button.click()
time.sleep(60)
self.up = self.driver.find_element_by_xpath('//*[@id="container"]/div/div[3]/div/div/div/div[2]/div[3]/div[3]/div/div[3]/div/div/div[2]/div[1]/div[2]/div/div[2]/span').text
self.down = self.driver.find_element_by_xpath('//*[@id="container"]/div/div[3]/div/div/div/div[2]/div[3]/div[3]/div/div[3]/div/div/div[2]/div[1]/div[3]/div/div[2]/span').text
def tweet_at_provider(self):
self.driver.get("https://twitter.com/login")
time.sleep(2)
email = self.driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div[2]/main/div/div/div[1]/form/div/div[1]/label/div/div[2]/div/input')
password = self.driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div[2]/main/div/div/div[1]/form/div/div[2]/label/div/div[2]/div/input')
email.send_keys(TWITTER_EMAIL)
password.send_keys(TWITTER_PASSWORD)
time.sleep(2)
password.send_keys(Keys.ENTER)
time.sleep(5)
tweet_compose = self.driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div[2]/main/div/div/div/div/div/div[2]/div/div[2]/div[1]/div/div/div/div[2]/div[1]/div/div/div/div/div/div/div/div/div/div[1]/div/div/div/div[2]/div/div/div/div')
tweet = f"Hey Internet Provider, why is my internet speed {self.down}down/{self.up}up when I pay for {PROMISED_DOWN}down/{PROMISED_UP}up?"
tweet_compose.send_keys(tweet)
time.sleep(3)
tweet_button = self.driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div[2]/main/div/div/div/div/div/div[2]/div/div[2]/div[1]/div/div/div/div[2]/div[4]/div/div/div[2]/div[3]')
tweet_button.click()
time.sleep(2)
self.driver.quit()
bot = InternetSpeedTwitterBot(CHROME_DRIVER_PATH)
bot.get_internet_speed()
bot.tweet_at_provider()