Advertisement
Guest User

magtiFun_send_message.py

a guest
May 30th, 2018
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.98 KB | None | 0 0
  1. """import sys, time
  2.  
  3. for i in sys.path:
  4.     print(i)
  5.    
  6. time.sleep(1000)"""
  7.  
  8. '''
  9.    Define simple function to send messages using magtifun and selenium
  10.    Refine later, to work without selenium webdriver(useing post method, without browser)
  11.  
  12.    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
  13.    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)
  14.  
  15.    each argument should have string type
  16.  
  17.    # + sign in numbers for now are not allowed, think, if it will cause problems, or not...
  18.  
  19. '''
  20. import time
  21.  
  22. def wait_():
  23.     # replace return before function
  24.     import time
  25.     for i in range(10000):
  26.         time.sleep(0.1)
  27.  
  28. #import
  29. from selenium import webdriver
  30. #Login information  --> later use separate file or other method to do this
  31. show_browser = True     # do we want to see the browser window (how to also remove black windo pop up(?))
  32. close_after = True # close or not window after sending finished
  33. username = "598618485"
  34. password = "jE5jvEnB"
  35.  
  36. ###individual case
  37. ##number = "598618485"
  38. ##message_text = "Hi, Python is awasome!"
  39.  
  40. #multiple case
  41. number = ["598618485"] * 2
  42. message_text = ["Hi, Python is awesome!_" + str(i+1) for i in range(2)]
  43.  
  44. # check message text for limit at magtifun - 445 characters
  45. # and other not standard case scenarios, to overcome problems
  46. # define checking lists
  47. if isinstance(number, list) and isinstance(message_text,list) and len(number) == len(message_text): # few at once case
  48.     numbers_to_check, messages_to_check = number, message_text
  49. elif isinstance(number, str) and isinstance(message_text,str): # one number, one message case
  50.     numbers_to_check, messages_to_check = [number], [message_text]
  51. else:  # something not good happens case
  52.     print("\n", "| There was a problem, maybe number of messages and numbers are not same,".center(70),"\n")
  53.     print("or their structure is not correct, check and try again. |".center(70))
  54.     wait_() #wait
  55. # check
  56. for individual_message, individual_number in zip(messages_to_check, numbers_to_check):
  57.     if len(individual_message) == 0 or len(individual_message) > 445:
  58.         print("\n", "| Sorry, message should be from 1 to 445 characters, not |{}|. |".format(len(individual_message)).center(70, "="), "\n")
  59.         #return --> in function
  60.         wait_() #wait
  61.     elif len(individual_message.strip()) == 0:  #message also needs to be not only spaces(magtifun says so)
  62.         print("\n", "| Sorry, message should contain at least one character different than a space. |".center(70, "="), "\n")
  63.         #return --> in function
  64.         wait_() #wait
  65.  
  66.     elif not individual_number.isdigit(): # isdigit returns True, if number contains minimum 1 character and characters are only numbers
  67.         print("\n", "| Sorry, number format |{}| is not valid. |".format(individual_number).center(70, "="), "\n")
  68.         wait_() #wait
  69.  
  70.  
  71.  
  72.  
  73. ##
  74. ##
  75. #initialize browser
  76. #make headless, if necessary
  77. if not show_browser:
  78.     #import
  79.     from selenium.webdriver.chrome.options import Options
  80.     # add headless option
  81.     options_ = Options(); options_.add_argument("--headless")
  82.     #initialize
  83.     browser = webdriver.Chrome(chrome_options = options_)
  84. else:
  85.     driverLocation = "chromedriver"
  86.     browser = webdriver.Chrome(driverLocation)
  87. # open page
  88. browser.get(r"http:/magtifun.ge")
  89. # get username and password fields
  90. username_field = browser.find_element_by_xpath(r'//*[@id="user"]')
  91. password_field = browser.find_element_by_xpath(r'//*[@id="password"]')
  92. # type user information and log in
  93. username_field.send_keys(username); password_field.send_keys(password)
  94. 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()
  95. # check how many free sms we have left
  96. balance = browser.find_element_by_xpath(r'/html/body/div[1]/center/div[1]/div[2]/div[1]/form/div[2]/span/span').text
  97. # 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)
  98. # in case of function, stop execution, print that fact and return False, now just sleep
  99. if eval(balance) < 1:
  100.     print("Sorry, you have not enough balance  |{}| to send message.".format(balance).center(70, "="))
  101.     #return False --> in function
  102.     wait_()  # wait
  103. ##else:
  104. # send multiple messages to multiple numbers, if lists are used as an arguments, otherwies, send to one, for this use previously defined check lists
  105. for number_, sms_ in zip(numbers_to_check,messages_to_check):
  106.     # get number and message fields
  107.     numbers_field = browser.find_element_by_xpath(r'//*[@id="recipient"]')
  108.     message_field = browser.find_element_by_xpath(r'//*[@id="message_body"]')
  109.     send_button = browser.find_element_by_xpath(r'//*[@id="sms_form"]/p[6]/input')
  110.     # type individual number and message
  111.     numbers_field.send_keys(number_); message_field.send_keys(sms_)
  112.     # send and refresh page
  113.     send_button.click(); browser.refresh()  # --> test it
  114.     #print
  115.     print("\n","| Your message to {} was successfully sent. |".format(number_).center(70, "*"), "\n")
  116.     # get number of messages left again
  117.     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
  118.     print("\n", "| You have left {} messages. |".format(balance_final).center(70, "="),"\n")
  119.     print("-" * 72, "\n")
  120.  
  121.     #if balance is left 0, print about it and stop process
  122.     if not eval(balance): #if balance is not different from 0
  123.         print("\n", "| Sorry, you have not left enough balance |{}| to send other messages. |".format(balance).center(70, "="), "\n")
  124. ### fill these fields and press send
  125. ##numbers_field.send_keys(number); message_field.send_keys(message_text)
  126. ##send_button.click()
  127.  
  128.  
  129.  
  130. #close browser if appropriate argument is set to True
  131. if close_after:
  132.     browser.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement