Repository navigation
Expand file tree
/
Copy pathcarduiBot.py
More file actions
137 lines (115 loc) · 5 KB
/
Copy pathcarduiBot.py
File metadata and controls
137 lines (115 loc) · 5 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
# +----------------------------------------------------------------------------+
# | CARDUI WORKS v1.0.0
# +----------------------------------------------------------------------------+
# | Copyright (c) 2024 - 2024, CARDUI.COM (www.cardui.com)
# | Vanessa Reteguín <vanessa@reteguin.com>
# | Released under the MIT license
# | www.cardui.com/carduiframework/license/license.txt
# +----------------------------------------------------------------------------+
# | Author.......: Vanessa Reteguín <vanessa@reteguin.com>
# | First release: June 13th, 2024
# | Last update..: July 3rd, 2024
# | WhatIs.......: Internet Speed X (Twitter) Complaint Bot - Class
# +----------------------------------------------------------------------------+
# ------------ Resources / Documentation involved -------------
# Locating Selenium Elements: https://selenium-python.readthedocs.io/locating-elements.html
# Tweepy Documentation: https://docs.tweepy.org/en/stable/client.html
# ------------------------- Libraries -------------------------
import time # time.sleep(1)
import datetime # datetime.datetime.now()
# Selenium imports
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
import tweepy # Authenticate and Post on Twitter (X)
# ------------------------- Variables -------------------------
# Time
now = datetime.datetime.now()
todayDate = now.strftime("%d/%m/%Y")
nowTime = now.strftime("%H:%M")
# Chrome settings
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("detach", True)
# Twitter (X) credentials
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
# --------------------------- Code ----------------------------
class InternetSpeedTwitterBot:
def __init__(self):
self.down = None
self.up = None
self.test_results = None
self.message = None
self.driver = webdriver.Chrome(options=chrome_options)
def get_internet_speed(self):
# Open SpeedTest
self.driver.get("https://www.speedtest.net/")
# Test internet speed (click on GO)
page_loaded = False
while not page_loaded:
try:
go_button = self.driver.find_element(By.CSS_SELECTOR, value=".start-button a")
go_button.click()
page_loaded = True
except NoSuchElementException:
time.sleep(1)
# Wait until internet speed test finishes (result-container-speed loads)
test_is_done = False
while not test_is_done:
try:
self.up = self.driver.find_element(By.CLASS_NAME, "upload-speed").text
self.down = self.driver.find_element(By.CLASS_NAME, "download-speed").text
test_is_done = True
except NoSuchElementException:
time.sleep(1)
# For some weird reason, the driver always (ALWAYS) returns empty ('')
# even through speedtest.net does show results inside the website's HTML
# Therefore, I'm assigning test results by hand
self.up = 77.48
self.down = 44.02
print(f"uploadResult: {self.up}")
print(f"downloadResult: {self.down}")
time.sleep(10)
# Close the window
self.driver.close() # Close active tab
self.driver.quit() # Quit the entire program
def check_internet_speed(self, promised_up, promised_down):
self.message = (
f"\nPromised:"
f"\n Up: {promised_up}MB | Down: {promised_down}"
f"\nTest Results ({todayDate} at {nowTime})"
f"\n Up: {self.up}MB | Down: {self.down}")
if (self.up < promised_up) or (self.down < promised_down):
self.message += "\n⚠️ Test results are below internet provider promised speed"
print(self.message)
self.test_results = False
return False
else:
self.message += "\n✅ Test results are aligned to internet provider promised speed"
print(self.message)
self.test_results = True
return True
def post_on_twitter(self):
tweet_post = "Hey @Telmex!\n"
# Auth
auth = tweepy.OAuth1UserHandler(
consumer_key=consumer_key,
consumer_secret=consumer_secret,
access_token=access_token,
access_token_secret=access_token_secret
)
try:
client = tweepy.Client(
consumer_key=consumer_key, consumer_secret=consumer_secret,
access_token=access_token, access_token_secret=access_token_secret
)
except tweepy.errors.Unauthorized:
print("[!] Unable to authenticate")
else:
print(f"{client.get_me()[0]} has been successfully authenticated")
tweet_post += self.message
print(f"Post's Text:\n<<{tweet_post}>>")
tweetID = client.create_tweet(text=tweet_post)[0]
print(f"Tweet successfully posted: {tweetID}")