-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtweet_net_speed.py
145 lines (104 loc) · 6.23 KB
/
tweet_net_speed.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
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
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver import common
import dotenv
import os
import time
dotenv.load_dotenv()
def main():
net_speed = NetSpeedTwitter()
speed = net_speed.get_site_speed()
print(speed)
tweet = f"My internet speed report:\ndownload speed: {speed[0]} mb/s\nupload speed: {speed[1]} mb/s\nping: {speed[2]}ms\nISP: #{speed[3]} "
net_speed.twitter_login()
net_speed.twitter_tweet(tweet)
class NetSpeedTwitter:
def __init__(self) -> None:
self.chrome_options = webdriver.ChromeOptions()
self.chrome_options.add_experimental_option('detach', True)
self.driver = webdriver.Chrome()#options=self.chrome_options)
self.wait = WebDriverWait(self.driver, 60)
#_internet speed tester() -> [download_speed, upload_speed, ping, ISP]
def get_site_speed(self):
#_get the website
url = 'https://www.speedtest.net/'
self.driver.get(url=url)
time.sleep(2)
#_cookie confirmer
try:
time.sleep(3)
self.btn_locator('button#onetrust-accept-btn-handler').click()
except:
pass
#_locating the clickable start butn
start = self.btn_locator('a.js-start-test')
#_call js function for running the test.
self.driver.execute_script("arguments[0].click();", start)
#_wait for connection
self.wait.until(expected_conditions.invisibility_of_element_located((common.by.By.CSS_SELECTOR, 'div.connecting-message')))
time.sleep(5)
#_wait untill the speed test is running
self.wait.until(expected_conditions.invisibility_of_element_located((common.by.By.CSS_SELECTOR, 'canvas.gauge-speed-text')))
#_get isp with xpath_locator
isp = self.xpath_locator('//*[@id="container"]/div/div[3]/div/div/div/div[2]/div[3]/div[3]/div/div[4]/div/div[2]/div/div/div/div[2]')
#_get other parameters
download = self.driver.find_element(common.by.By.CSS_SELECTOR, 'span.download-speed')
upload = self.driver.find_element(common.by.By.CSS_SELECTOR, 'span.upload-speed')
ping = self.driver.find_element(common.by.By.CSS_SELECTOR, 'span.ping-speed')
return [download.text, upload.text, ping.text, isp.text]
#_twitter post creator(tweet_content) -> None
def twitter_tweet(self, tweet_phrase):
input_xpath = '//*[@id="react-root"]/div/div/div[2]/main/div/div/div/div[1]/div/div[3]/div/div[2]/div[1]/div/div/div/div[2]/div[1]/div/div/div/div/div/div/div/div/div/div/label/div[1]/div/div/div/div/div/div[2]/div/div/div/div'
post_btn_css = '#react-root > div > div > div.css-175oi2r.r-1f2l425.r-13qz1uu.r-417010.r-18u37iz > main > div > div > div > div.css-175oi2r.r-14lw9ot.r-jxzhtn.r-13l2t4g.r-1ljd8xs.r-1phboty.r-16y2uox.r-184en5c.r-61z16t.r-11wrixw.r-1jgb5lz.r-13qz1uu.r-1ye8kvj > div > div.css-175oi2r.r-14lw9ot.r-184en5c > div > div.css-175oi2r.r-14lw9ot.r-1h8ys4a > div:nth-child(1) > div > div > div > div.css-175oi2r.r-1iusvr4.r-16y2uox.r-1777fci.r-1h8ys4a.r-1bylmt5.r-13tjlyg.r-7qyjyx.r-1ftll1t > div.css-175oi2r.r-14lw9ot.r-jumn1c.r-xd6kpl.r-gtdqiz.r-ipm5af.r-184en5c > div:nth-child(2) > div > div > div > div.css-175oi2r.r-sdzlij.r-1phboty.r-rs99b7.r-lrvibr.r-19u6a5r.r-2yi16.r-1qi8awa.r-ymttw5.r-1loqt21.r-o7ynqc.r-6416eg.r-1ny4l3l > div'
#_type the tweet content
tweet_input = self.xpath_locator(input_xpath)
self.input_filler(tweet_input, tweet_phrase)
#_tweet content
self.btn_locator(post_btn_css).click()
#_login to twitter account() -> None
def twitter_login(self):
password = os.getenv('TWITTER_PASSWORD')
email = os.getenv('TWITTER_EMAIL')
user = os.getenv('TWITTER_USER')
url = "https://twitter.com/"
#_open twitter.com
self.driver.get(url)
#_locating te log in button and click
login_btn_css = '#react-root > div > div > div.css-175oi2r.r-1f2l425.r-13qz1uu.r-417010 > main > div > div > div.css-175oi2r.r-tv6buo.r-791edh.r-1euycsn > div.css-175oi2r.r-1777fci.r-nsbfu8.r-1qmwkkh > div > div.css-175oi2r > div.css-175oi2r.r-2o02ov > a'
login_btn = self.btn_locator(login_btn_css)
login_btn.click()
#_locating mail input
mail_input_xpath = '//*[@id="layers"]/div[2]/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div/div/div/div[5]/label/div/div[2]/div/input'
element = self.xpath_locator(mail_input_xpath)
self.input_filler(element, email)
element.send_keys(common.keys.Keys.RETURN)
#_looking for username input field
username_input_xpath = '//*[@id="layers"]/div[2]/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div[1]/div/div[2]/label/div/div[2]/div/input'
element = self.xpath_locator(username_input_xpath)
self.input_filler(element, user)
element.send_keys(common.keys.Keys.RETURN)
#_looking for password field
password_input_xpath = '//*[@id="layers"]/div[2]/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div[1]/div/div/div[3]/div/label/div/div[2]/div[1]/input'
element = self.xpath_locator(password_input_xpath)
print('')
self.input_filler(element, password)
element.send_keys(common.keys.Keys.RETURN)
#_element locator by xath(xpath_address) -> element_object
def xpath_locator(self, xpath_address):
#_find and locate desired element
return self.wait.until(expected_conditions.presence_of_element_located((common.by.By.XPATH, xpath_address)))
#_clickable button locator(css_address) -> clickable_object
def btn_locator(self, css_selector):
#_wait and locate the desired clickable button
return self.wait.until(expected_conditions.element_to_be_clickable((common.by.By.CSS_SELECTOR, css_selector)))
#_input filler(input_obj, str(input_value) -> True|False
def input_filler(self, element_obj, input_value):
#_type the passed value to target input one character at a time
try:
[element_obj.send_keys(i) for i in input_value]
return True
except:
return False
if __name__=="__main__":
main()