Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """import sys, time
- for i in sys.path:
- print(i)
- time.sleep(1000)"""
- '''
- Define simple function to send messages using magtifun and selenium
- Refine later, to work without selenium webdriver(useing post method, without browser)
- We are not going to make number checking very complicated, so if something goes wrong, include viewing live argument, to see what happens exactly in browser
- In message we are just checking its length (not 0 and no more than 445 character) and in number only that it contains only numbers(maybe refine later)
- each argument should have string type
- # + sign in numbers for now are not allowed, think, if it will cause problems, or not...
- '''
- import time
- def wait_():
- # replace return before function
- import time
- for i in range(10000):
- time.sleep(0.1)
- #import
- from selenium import webdriver
- #Login information --> later use separate file or other method to do this
- show_browser = True # do we want to see the browser window (how to also remove black windo pop up(?))
- close_after = True # close or not window after sending finished
- username = "598618485"
- password = "jE5jvEnB"
- ###individual case
- ##number = "598618485"
- ##message_text = "Hi, Python is awasome!"
- #multiple case
- number = ["598618485"] * 2
- message_text = ["Hi, Python is awesome!_" + str(i+1) for i in range(2)]
- # check message text for limit at magtifun - 445 characters
- # and other not standard case scenarios, to overcome problems
- # define checking lists
- if isinstance(number, list) and isinstance(message_text,list) and len(number) == len(message_text): # few at once case
- numbers_to_check, messages_to_check = number, message_text
- elif isinstance(number, str) and isinstance(message_text,str): # one number, one message case
- numbers_to_check, messages_to_check = [number], [message_text]
- else: # something not good happens case
- print("\n", "| There was a problem, maybe number of messages and numbers are not same,".center(70),"\n")
- print("or their structure is not correct, check and try again. |".center(70))
- wait_() #wait
- # check
- for individual_message, individual_number in zip(messages_to_check, numbers_to_check):
- if len(individual_message) == 0 or len(individual_message) > 445:
- print("\n", "| Sorry, message should be from 1 to 445 characters, not |{}|. |".format(len(individual_message)).center(70, "="), "\n")
- #return --> in function
- wait_() #wait
- elif len(individual_message.strip()) == 0: #message also needs to be not only spaces(magtifun says so)
- print("\n", "| Sorry, message should contain at least one character different than a space. |".center(70, "="), "\n")
- #return --> in function
- wait_() #wait
- elif not individual_number.isdigit(): # isdigit returns True, if number contains minimum 1 character and characters are only numbers
- print("\n", "| Sorry, number format |{}| is not valid. |".format(individual_number).center(70, "="), "\n")
- wait_() #wait
- ##
- ##
- #initialize browser
- #make headless, if necessary
- if not show_browser:
- #import
- from selenium.webdriver.chrome.options import Options
- # add headless option
- options_ = Options(); options_.add_argument("--headless")
- #initialize
- browser = webdriver.Chrome(chrome_options = options_)
- else:
- driverLocation = "chromedriver"
- browser = webdriver.Chrome(driverLocation)
- # open page
- browser.get(r"http:/magtifun.ge")
- # get username and password fields
- username_field = browser.find_element_by_xpath(r'//*[@id="user"]')
- password_field = browser.find_element_by_xpath(r'//*[@id="password"]')
- # type user information and log in
- username_field.send_keys(username); password_field.send_keys(password)
- browser.find_element_by_xpath(r'/html/body/div[1]/center/div[1]/div[2]/div[1]/form/table/tbody/tr/td[2]/input').click()
- # check how many free sms we have left
- balance = browser.find_element_by_xpath(r'/html/body/div[1]/center/div[1]/div[2]/div[1]/form/div[2]/span/span').text
- # if balance is not enough (more than 1, if we want to send few at once messages later, but for now work on individual sms sending)
- # in case of function, stop execution, print that fact and return False, now just sleep
- if eval(balance) < 1:
- print("Sorry, you have not enough balance |{}| to send message.".format(balance).center(70, "="))
- #return False --> in function
- wait_() # wait
- ##else:
- # send multiple messages to multiple numbers, if lists are used as an arguments, otherwies, send to one, for this use previously defined check lists
- for number_, sms_ in zip(numbers_to_check,messages_to_check):
- # get number and message fields
- numbers_field = browser.find_element_by_xpath(r'//*[@id="recipient"]')
- message_field = browser.find_element_by_xpath(r'//*[@id="message_body"]')
- send_button = browser.find_element_by_xpath(r'//*[@id="sms_form"]/p[6]/input')
- # type individual number and message
- numbers_field.send_keys(number_); message_field.send_keys(sms_)
- # send and refresh page
- send_button.click(); browser.refresh() # --> test it
- #print
- print("\n","| Your message to {} was successfully sent. |".format(number_).center(70, "*"), "\n")
- # get number of messages left again
- balance_final = browser.find_element_by_xpath(r'/html/body/div[1]/center/div[1]/div[2]/div[1]/form/div[2]/span/span').text
- print("\n", "| You have left {} messages. |".format(balance_final).center(70, "="),"\n")
- print("-" * 72, "\n")
- #if balance is left 0, print about it and stop process
- if not eval(balance): #if balance is not different from 0
- print("\n", "| Sorry, you have not left enough balance |{}| to send other messages. |".format(balance).center(70, "="), "\n")
- ### fill these fields and press send
- ##numbers_field.send_keys(number); message_field.send_keys(message_text)
- ##send_button.click()
- #close browser if appropriate argument is set to True
- if close_after:
- browser.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement