Add proxy to selenium python script - python

I'm really new to python and
I have a python selenium script
#Developed by github.com/useragents
#This script was made for educational purposes. I am not responsible for your actions using this script. This code is a few months old, hence why it may not appear as professional but still works to this day.
try:
from selenium import webdriver
import time, os, ctypes, requests
from colorama import Fore, init
import warnings, selenium, platform
except ImportError:
input("Error while importing modules. Please install the modules in requirements.txt")
init(convert = True, autoreset = True)
warnings.filterwarnings("ignore", category=DeprecationWarning)
clear = "clear"
if platform.system() == "Windows":
clear = "cls"
os.system(clear)
ascii_text = f"""{Fore.RED}
████████▀▀▀████
████████────▀██
████████──█▄──█
███▀▀▀██──█████
█▀──▄▄██──█████
█──█████──█████
█▄──▀▀▀──▄█████
███▄▄▄▄▄███████ github.com/useragents
"""
class automator:
def __init__(self):
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_experimental_option("excludeSwitches", ["enable-logging"])
self.xpaths = {
"followers": "/html/body/div[4]/div[1]/div[3]/div/div[1]/div/button",
"likes": "/html/body/div[4]/div[1]/div[3]/div/div[2]/div/button",
"views": "/html/body/div[4]/div[1]/div[3]/div/div[4]/div/button",
"shares": "/html/body/div[4]/div[1]/div[3]/div/div[5]/div/button"
}
try:
self.driver = webdriver.Chrome(options = options)
except Exception as e:
self.error(f"Error trying to load web driver: {e}")
self.status = {}
self.sent = 0
self.cooldowns = 0
self.ratelimits = 0
def check_status(self):
for xpath in self.xpaths:
value = self.xpaths[xpath]
element = self.driver.find_element_by_xpath(value)
if not element.is_enabled():
self.status.update({xpath: "[OFFLINE]"})
else:
self.status.update({xpath: ""})
def check_for_captcha(self):
while True:
try:
if "Enter the word" not in self.driver.page_source:
return
except:
return
os.system(clear)
print(ascii_text)
print(f"{self.console_msg('Error')} Complete the CAPTCHA in the driver.")
time.sleep(1)
def console_msg(self, status):
colour = Fore.RED
if status == "Success":
colour = Fore.GREEN
return f" {Fore.WHITE}[{colour}{status}{Fore.WHITE}]"
def update_ascii(self):
options = f"""
{self.console_msg("1")} Follower Bot {Fore.RED}{self.status["followers"]}
{self.console_msg("2")} Like Video Bot {Fore.RED}{self.status["likes"]}
{self.console_msg("3")} View Bot {Fore.RED}{self.status["views"]}
{self.console_msg("4")} Share Bot {Fore.RED}{self.status["shares"]}
"""
return ascii_text + options
def check_url(self, url):
redirect = True
if "vm.tiktok.com/" in url:
redirect = False
if redirect:
if "/video/" not in url:
return False
session = requests.Session()
r = session.get(url, allow_redirects=redirect)
if redirect:
if r.status_code == 200:
return True
return False
location = r.headers["Location"]
if "/video" in location:
return True
return False
def convert(self, min, sec):
seconds = 0
if min != 0:
answer = int(min) * 60
seconds += answer
seconds += int(sec) + 15
return seconds
def check_submit(self, div):
remaining = f"/html/body/div[4]/div[{div}]/div/div/h4"
try:
element = self.driver.find_element_by_xpath(remaining)
except:
return None, None
if "READY" in element.text:
return True, True
if "seconds for your next submit" in element.text:
output = element.text.split("Please wait ")[1].split(" for")[0]
minutes = element.text.split("Please wait ")[1].split(" ")[0]
seconds = element.text.split("(s) ")[1].split(" ")[0]
sleep_duration = self.convert(minutes, seconds)
return sleep_duration, output
return element.text, None
def update_cooldown(self, sleep_time, bot, rl = False):
cooldown = sleep_time
while True:
time.sleep(1)
try:
cooldown -= 1
except TypeError:
break
self.update_title(bot, cooldown, rl)
if cooldown == 0:
break
def wait_for_ratelimit(self, arg, div):
time.sleep(1)
duration, output = self.check_submit(div)
if duration == True:
return
if output == None:
time.sleep(0.7)
self.wait_for_ratelimit(arg, div)
self.cooldowns += 1
self.update_cooldown(duration, arg)
def send_bot(self, video_url, bot, div):
try:
self.driver.find_element_by_xpath(self.xpaths[bot]).click()
time.sleep(0.5)
except:
pass
enter_link_xpath = f"/html/body/div[4]/div[{div}]/div/form/div/input"
link = self.driver.find_element_by_xpath(enter_link_xpath)
link.clear()
link.send_keys(video_url)
self.driver.find_element_by_xpath(f"/html/body/div[4]/div[{div}]/div/form/div/div/button").click() #Search button
time.sleep(0.8)
send_button_xpath = f"/html/body/div[4]/div[{div}]/div/div/div[1]/div/form/button"
try:
self.driver.find_element_by_xpath(send_button_xpath).click()
except selenium.common.exceptions.NoSuchElementException:
self.wait_for_ratelimit(bot, div)
self.driver.find_element_by_xpath(f"/html/body/div[4]/div[{div}]/div/form/div/div/button").click() #Search button
time.sleep(0.8)
self.driver.find_element_by_xpath(send_button_xpath).click()
time.sleep(3)
try:
s = self.driver.find_element_by_xpath(f"/html/body/div[4]/div[{div}]/div/div/span")
if "Too many requests" in s.text:
self.ratelimits += 1
self.update_cooldown(50, bot, True)
self.send_bot(video_url, bot, div)
elif "sent" in s.text:
sent = 100
if bot == "likes":
try:
sent = int(s.text.split(" Hearts")[0])
except IndexError:
sent = 30
if bot == "views":
sent = 2500
if bot == "shares":
sent = 500
self.sent += sent
else:
print(s.text)
except:
self.sent += sent
self.update_title(bot, "0")
self.wait_for_ratelimit(bot, div)
self.send_bot(video_url, bot, div)
def update_title(self, bot, remaining, rl = False):
if clear == "cls":
os.system(clear)
ctypes.windll.kernel32.SetConsoleTitleW(f"TikTok AIO | Sent: {self.sent} | Cooldown: {remaining}s | Developed by #useragents on Github")
print(ascii_text)
print(self.console_msg(self.sent) + f" Sent {bot}")
rl_cooldown = "0"
cooldown = "0"
if rl:
rl_cooldown = remaining
else:
cooldown = remaining
print(self.console_msg(self.cooldowns) + f" Cooldowns {Fore.WHITE}[{Fore.RED}{cooldown}s{Fore.WHITE}]")
print(self.console_msg(self.ratelimits) + f" Ratelimits {Fore.WHITE}[{Fore.RED}{rl_cooldown}s{Fore.WHITE}]")
def main(self):
if clear == "cls":
ctypes.windll.kernel32.SetConsoleTitleW("TikTok AIO | Developed by #useragents on Github")
self.driver.get("https://zefoy.com/")
time.sleep(2)
if "502 Bad Gateway" in self.driver.page_source:
os.system(clear)
print(ascii_text)
input(f"{self.console_msg('Error')} This website does not allow VPN or proxy services.")
os._exit(0)
self.check_for_captcha()
self.check_status()
self.start()
def error(self, error):
print(ascii_text)
print(f"{self.console_msg('Error')} {error}")
time.sleep(5)
os._exit(0)
def start(self):
os.system(clear)
print(self.update_ascii())
try:
option = int(input(f" {Fore.RED}> {Fore.WHITE}"))
except ValueError:
self.start()
if option == 1:
if self.status["followers"] != "":
return self.start()
div = 2
ver = "followers"
username = str(input(f"\n{self.console_msg('Console')} TikTok Username: #"))
print()
self.send_bot(username, ver, div)
return
elif option == 2:
if self.status["likes"] != "":
return self.start()
div = 3
ver = "likes"
elif option == 3:
if self.status["views"] != "":
return self.start()
div = 5
ver = "views"
elif option == 4:
if self.status["shares"] != "":
return self.start()
div = 6
ver = "shares"
else:
return self.start()
video_url = str(input(f"\n{self.console_msg('Console')} Video URL: "))
print()
check = self.check_url(video_url)
if not check:
return self.error("This URL does not exist.")
self.send_bot(video_url, ver, div)
obj = automator()
obj.main()
And tried to add https proxy with auth in this script
My proxy is 34.72.101.101:3127:user:BasAJSzAdAAWD
I tried to configure script to use proxy but it crashed one time and another time when I tried another way it worked but after some step proxy disappear

Related

ONLY one client receive message in multiple client's server

My target is print the message from function result on the client's screen. But only ONE client can received the message...
The part of client.py is here
def PlayGame(clientSocket, msg):
invalid = "NO!"
if ("/guess " in msg):
msg1 = msg.split(" ")[1]
print("Hi1\n")
if msg1 == "true" or msg1 == "false":
print("Hi11")
clientSocket.send(msg1.encode())
print(clientSocket.recv(1024).decode())
print("!")
return '1'
else:
clientSocket.send(invalid.encode())
print(clientSocket.recv(1024).decode())
print("2")
return '2'
elif msg == "":
return '2'
else:
clientSocket.send(invalid.encode())
print(clientSocket.recv(1024).decode())
print("3")
return '2'
def main(argv):
msg=""
while (PlayGame(clientSocket, msg)!=1):
msg = input()
Any part of the server.py
guess_box = []
guess = bool(random.randint(0, 1))
def result(connectionSocket, guess_box, addr, addr_l):
a = 0
if(guess_box[0] == guess_box[1]):
msg = "Tie!!"
connectionSocket.send(msg.encode())
return '2'
elif(guess_box[0] == guess):
msg = "Player 1 Wins!"
a+=1
connectionSocket.send(msg.encode())
return '2'
elif(guess_box[1] == guess):
msg = "Player 2 Wins!"
a+=1
connectionSocket.send(msg.encode())
return '2'
def TF(connectionSocket, var, guess_box, addr, addr_l):
msg = connectionSocket.recv(1024).decode()
print("recv:",msg)
if(msg == 'true'):
msg = 'True'
var = str(var)
msg = bool(msg == var)
guess_box.append(msg)
return 'ok'
elif(msg == 'false'):
msg = 'False'
var = str(var)
msg = bool(msg == var)
guess_box.append(msg)
return 'ok'
else:
print(msg)
statement = "4002 Unrecognized message!!"
connectionSocket.send(statement.encode())
return 'again'
class ServerThread(threading.Thread):
def __init__(self, client):
threading.Thread.__init__(self)
self.client = client
def run(self):
...
print("guess is:", guess)
while (len(guess_box) != 2):
TF(connectionSocket, guess, guess_box, addr, addr_l)
print("start")
result(connectionSocket, guess_box, addr, addr_l)
...
Regarding only the one problem you address:
print the message from function result on the client's screen. But only ONE client can received the message
The problem comes from the use of a different thread for each client. The thread which receives a guess as first stays in its
while (len(guess_box) != 2):
print(guess_box)
TF(connectionSocket, guess, guess_box)
loop and waits for another message, which doesn't come. The thread which receives a guess as second sends the result to its own client only.
I don't think there's a sensible way to fix this while keeping this dthreaded approach.
Can I change the structure of my code by using those functions I implemented?
Here's a substitute for the while True loop in server_run that doesn't require changes in those functions other than server_run.
from select import select
connections = []
room_connection = {}
for reads in iter(lambda: select([serverSocket]+connections, [], [])[0], []):
for ready in reads: # for each socket which select reports is readable
if ready == serverSocket: # it's the server socket, accept new client
connectionSocket, addr = serverSocket.accept()
connections.append(connectionSocket)# store the connection socket
while RecvFromClient(connectionSocket) == "NO": pass
else: # message (or disconnect) from a client
try: var = GameHallMsg(ready, ready, connections)
except socket.error: var = 'bye'
if var == 'bye': # client finished, remove from list
connections.remove(ready)
ready.close()
elif var == 'wait': # store first player's connection
room_connection[current_rm_num.pop()] = ready
elif var == 'NO':
rtn_msg_4 = "4002 Unrecognized message"
ready.send(rtn_msg_4.encode())
elif var == 'jump':
readyroom = current_rm_num.pop()
# find and notify other player in the room
other = room_connection.pop(readyroom)
rtn_msg_2 = "3012 Game started. Please guess true or false"
other.send(rtn_msg_2.encode())
print("guess is:", guess)
# query and inform both players
guess_box = []
while TF(ready, True, guess_box) != 'ok': pass
while TF(other, True, guess_box) != 'ok': pass
result(ready, guess_box, ('', 0), [0])
result(other, guess_box, ('', 1), [0, 1])
room[readyroom] = 0

Web Push Is Not Being Executed In Job Scheduler

The job scheduler is being executed but for some reason the web push is not activated.I tried calling the web push function without the job scheduler and it does trigger a notification on browser. However, nothing gets triggered when I call it in the add_job scheduler function. The GET reminders should be called after the job scheduler if the web push were to be activated FYI. Please help.
#app.route("/notification", methods = ['POST', 'PUT'])
#token_required
def setNotifications(current_user, token):
if request.method == 'POST':
form = json.loads(request.data.decode('UTF-8'))
subscription = form["subscription"]
subscriptionId = form["subscriptionId"]
dailyReminder = True if form['daily'] is True else False
weeklyReminder = True if form['weekly'] is True else False
yearlyReminder = True if form['yearly'] is True else False
createNotif = db.session.query(User).filter(User.id == current_user.id).first()
reminder = db.session.query(Reminder).filter(Reminder.subscriptionId == subscriptionId).first()
message = json.dumps({"token": token, "subscriptionId": subscriptionId})
# print("Printing message" + message)
VAPID_CLAIMS = {
"sub": "my email"
}
if(createNotif.subscription == " "):
createNotif.subscription = json.dumps(subscription)
db.session.commit()
try:
# print("entering code")
sched.add_job(lambda:modules.send_web_push(json.loads(createNotif.subscription), message, VAPID_PRIVATE_KEY, VAPID_CLAIMS), 'date', run_date = datetime.date(2021, 5,8))
sched.start()
# modules.send_web_push(json.loads(createNotif.subscription), message, VAPID_PRIVATE_KEY, VAPID_CLAIMS)
# print("started here", flush= True)
return jsonify({'success':1})
except Exception as e:
print("error",e)
print("Could not send notification", flush=True)
return jsonify({'failed':str(e)})
if request.method == 'PUT':
removeSubscription = db.session.query(User).filter(User.id == current_user.id).first()
removeSubscription.subscription = " "
db.session.commit()
return jsonify({"success": "push notification has been updated"}), 200

return to first function() at the end of last function()

If you scroll to the bottom of below; I am trying to return to the first function at the end of my last function using process_records()
. My attempt returns Undefined variable 'process_records'pylint(undefined-variable)
def process_records(self, records, map_data, completed=None, errors=None):
"""Code to execute after webdriver initialization."""
series_not_null = False
try:
num_attempt = 0
for record in records.itertuples(): # not working
print(record)
series_not_null = True
self.navigate_to_search(num_attempt)
self.navigate_to_member(mrn)
self.navigate_to_assessment()
self.add_assessment(record, map_data)
self.driver.switch_to.parent_frame() # not working
sleep(.5)
except Exception as exc:
if series_not_null:
errors = self.process_series_error(exc)
return completed, errors
def navigate_to_search(self, num_attempt):
"""Uses webdriver to navigate to Search tab and select Member Status=All"""
if num_attempt == 0:
page_change_until(self.driver, By.XPATH, './/*[text()="Search"]')
wait_ready_state(self.driver)
else:
self.driver.switch_to.parent_frame()
elem = wait_until(self.driver, By.XPATH, './/*[text()="Search"]')
is_disp_n = 0
while True:
if elem.is_displayed():
break
else:
self.driver.switch_to.parent_frame()
is_disp_n += 1
sleep(1)
if is_disp_n == 20:
raise Exception('Could not find Search tab after 20 tries.')
num_attempt += 1
radio_locator = (By.XPATH, './/*[#type="RADIO"][#value="All"]')
while True:
break_while_timer = datetime.now()
if datetime.now() - break_while_timer > timedelta(seconds=20):
break_while = True
break
try:
if wait_until(self.driver, *radio_locator).is_selected():
pass
else:
wait_until(self.driver, *radio_locator).click()
break
except Exception:
sleep(1)
def navigate_to_member(self, mrn):
"""Finds member"""
wait_until(self.driver, By.XPATH, './/*[#name="MemberIdItem_1"]').clear()
wait_until(self.driver, By.XPATH, './/*[#name="MemberIdItem_1"]').send_keys(f'{mrn}'+Keys.ENTER)
page_change_until(self.driver, By.XPATH, f'.//*[text()="{mrn}"]')
wait_ready_state(self.driver)
def navigate_to_assessment(self):
"""Navigates to the appropriate contact log"""
self.driver.find_element_by_css_selector("div[eventproxy^='memberAssessment']").click() #clicks assessment icon
element = self.driver.find_element_by_xpath(f"//div[contains(text(), '{self.assessment_type}')]")
actions = ActionChains(self.driver)
actions.move_to_element(element).perform()
self.driver.find_element_by_xpath(f"//div[contains(text(), '{self.assessment_type}')]").click()
self.driver.find_element_by_css_selector("div[eventproxy^='createSelectedAssessmentsButton']").click()
def add_assessment(self, record, map_data):
"""Create contact log"""
qna_frame = self.driver.find_element_by_css_selector("iframe[id^='iccc']")
self.driver.switch_to.frame(qna_frame)
pages = self.driver.find_element_by_css_selector("ul[class='nav nav-pills nav-stacked qna-tabs']")
pages = pages.find_elements_by_css_selector("a")
for page in pages:
page.click()
# for record in records.itertuples(): #attempt
questions = self.driver.find_elements_by_css_selector("fieldset")
questions = [question for question in questions if question.text not in ("", " ", None)]
for question in questions[1:]:
q_text = question.find_element_by_css_selector("span[class='question-text ng-binding']").text
questionType = map_data.loc[map_data['question_text'] == q_text, 'question_type'].item()
answer = map_data.loc[map_data['question_text'] == q_text, 'map'].item()
answer = getattr(record, answer)
if answer not in ("", " ", "NaT", "NaN", None):
if questionType == 'checks':
self.choose_checks(question, answer)
else:
try:
if questionType == 'text':
self.driver.implicitly_wait(0)
(question.find_element_by_css_selector("textarea").send_keys(str(answer))
if
question.find_elements_by_css_selector("textarea")
else
question.find_element_by_css_selector("input").send_keys(answer))
self.driver.implicitly_wait(15)
elif questionType == 'date':
try:
answer = answer.strftime('%m/%d/%Y')
question.find_element_by_css_selector("input").send_keys(answer)
# page.click()
except Exception as e:
raise Errors.RequiredDataError('Issues with Assessment Date -- {}'.format(e))
elif questionType == 'radio':
question.find_element_by_css_selector("input[value='{}']".format(answer)).click()
except:
continue
else:
pass
self.driver.find_element_by_css_selector("#publishButton").click()
sleep(3)
WebDriverWait(self.driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".btn.btn-mini.btn-primary"))).click()
process_records()
I have also tried:
self.process_records()
This whole code-block seems to be inside a class. So you should be using self.process_records() to call the function.
The process_records method expects a few positional arguments (apart from self) - you'll probably need to pass those to the function too.

Queue runs successfully! Exits instead of continue, after Break

I am using this script to resolve thousands of domains. It runs successfully, and ends when the queue is empty. I am trying to it to break out of the loop and continue the script by printing.
How do I get this code to break out of the loop, ans print, when the queue is empty?
q = queue.Queue()
for name in names:
q.put(name)
def async_dns():
s = adns.init()
while True:
try:
dname = q.get(False)
q.task_done()
except queue.Empty:
return
response = s.synchronous(dname,adns.rr.NS)[0]
if response == 0:
dot_net.append("Y")
print(dname + ", is Y")
elif response == 300 or response == 30 or response == 60:
dot_net.append("N")
print(dname + ", is N")
threads = []
for i in range(20):
t = threading.Thread(target=async_dns)
threads.append(t)
t.start()
print("Done !!")
You could simply move the code that does the dns lookup and prints the result into the body of the try/except block:
def async_dns():
s = adns.init()
while True:
try:
dname = q.get(False)
response = s.synchronous(dname,adns.rr.NS)[0]
if response == 0:
dot_net.append("Y")
print(dname + ", is Y")
elif response == 300 or response == 30 or response == 60:
dot_net.append("N")
print(dname + ", is N")
q.task_done()
except queue.Empty:
return
Now when the queue is empty a queue.Empty will be raised and the exception handler will simply exit the thread function, otherwise it will print out the dns values.

Stopping a thread from another class using flags - Python/ChromeDriver

Threads are quite difficult for me. I can't quite wrap my head around how to stop a thread when some event occurs. Its difficult for me, because in the event occurs at the end of a for loop and also involves a browser.
This method here is supposed to continuously check for a flag
def Stop(self, event):
myClass.worker.terminate = True
if myClass.worker != None:
while myClass.browser!=None:
if myClass.worker.taskfinished:
myClass.worker.stop()
myClass.browser.quit()
myClass.worker = None
break
else:
pass
This condition occurs at the bottom of this for loop which is part of a different method:
for i in range(scraperow, numrows):
myClass.taskfinished = False
domain = sheet.cell_value(i, 2)
prevdomain = sheet.cell_value(i-1,2)
name = sheet.cell_value(i,0)
result = ""
if domain==prevdomain and result=="CATCH-ALL":
worksheet.write(i, 3, "Catch all")
wbook.save(file)
else:
wait = WebDriverWait(self.browser, 500)
inputName = wait.until(EC.presence_of_element_located((By.ID, "form-search-name")))
inputName.send_keys(name)
inputDomain = wait.until(EC.presence_of_element_located((By.ID, "form-search-domain")))
inputDomain.send_keys(domain)
norbertsearch = wait.until(EC.presence_of_element_located((By.NAME, "search")))
norbertsearch.click()
starttime = time.time()
wait = WebDriverWait(self.browser, 500)
selector = "div[class=\"text-center displayed\"] h3"
rslt = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, (selector))))
result = rslt.text
if result == "SUCCESS":
waittwo = WebDriverWait(self.browser, 5)
email = rslt.find_element_by_xpath('following-sibling::div/a')
email = email.get_attribute('href')
email = email.split(":")[1]
worksheet.write(i, 3, email)
wbook.save(file)
elif result == "NOTHING FOUND":
worksheet.write(i, 3, "Nothing found")
wbook.save(file)
elif result == "I'M TIRED":
worksheet.write(i, 3, "Over search limit")
wbook.save(file)
dlg = wx.MessageBox("Norbert is tired and has exhausted his searches for the day. Try again later.", "Ask Alfred", wx.OK | wx.ICON_INFORMATION)
break
elif result == "OH MAN, I BROKE MY ANKLE":
worksheet.write(i, 3, "Server error")
wbook.save(file)
elif result == "CATCH-ALL":
worksheet.write(i, 3, "Catch-all")
wbook.save(file)
myClass.taskfinished = True
Unfortunately its blocking up the thread, making things kind of hairy. How can I gracefully have the thread wait until its gotten a result and then stop the thread?

Categories