Advertisement
youshallnotpass

Untitled

Apr 5th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. from selenium import webdriver
  2. from selenium.webdriver.common.keys import Keys
  3. import time
  4. from datetime import datetime, timedelta
  5. #email lib
  6. from email.mime.multipart import MIMEMultipart
  7. from email.mime.text import MIMEText
  8. import smtplib
  9. import subprocess
  10. # create message object instance
  11. msg = MIMEMultipart()
  12. #user = "23838005962994"
  13. #pwd = "7312Lowe"
  14.  
  15. report_message = "Library report\n\n"
  16. anythingReturn = "False"
  17. bookNo = 0
  18. NextRunDate = datetime.now() + timedelta(days=12)
  19.  
  20.  
  21. def waitPageTitle():
  22. pageloadedok = False
  23. while not pageloadedok:
  24. driver.get("https://www.hkpl.gov.hk/en/login.html")
  25. timeout = 10
  26. while (not ("Hong Kong Public Libraries" in driver.title)) and (timeout>0):
  27. time.sleep(1)
  28. timeout = timeout-1
  29. if "Hong Kong Public Libraries" in driver.title:
  30. print("Yah it is ok")
  31. pageloadedok = True
  32. else:
  33. print("Timed out")
  34. time.sleep(60)
  35. driver.refresh()
  36. time.sleep(60)
  37.  
  38. def renewLibraryBooks(username,password,patronName):
  39. global NextRunDate
  40. global bookNo
  41. global report_message
  42. report_message = report_message + "\n Hi! " + str(patronName) + "\n\n"
  43. daysBeforeDue = 0
  44. anythingToRenew = 0
  45. pageloadedok = False
  46. # assert "Hong Kong Public Libraries" in driver.title
  47. waitPageTitle()
  48. elem = driver.find_element_by_id("account")
  49. elem.send_keys(username)
  50. time.sleep(0.5)
  51. elem = driver.find_element_by_id("password")
  52. elem.send_keys(password)
  53. elem.send_keys(Keys.RETURN)
  54. time.sleep(1)
  55. driver.get("https://webcat.hkpl.gov.hk/wicket/bookmarkable/com.vtls.chamo.webapp.component.patron.PatronAccountPage?theme=WEB&locale=en")
  56. assert "My Account | Chamo" in driver.title
  57. table = driver.find_element_by_id("checkout")
  58. rows = table.find_elements_by_tag_name("tr")
  59. for row in rows:
  60. cells = row.find_elements_by_tag_name("td")
  61.  
  62. if len(cells) == 6:
  63. #print(cells[4].text)
  64. bookNo = bookNo + 1
  65. report_message = report_message + str(bookNo) + ")" + cells[1].text + " : "
  66. print(cells[1].text)
  67. s = cells[5].text
  68. timesRenewed = s[:1]
  69. if int(timesRenewed) == 4:
  70. needReturn = True
  71. anythingReturn = "True"
  72. report_message = report_message + "THIS BOOK NEEDS TO BE RETURNED:" + needReturn
  73. needReturn = True
  74. dueDateString = cells[4].text
  75. dueDateDate = datetime.strptime(dueDateString,"%Y-%m-%d")
  76. #print(dueDateDate)
  77. #print(datetime.strftime(dueDateDate,"%B %d, %y"))
  78. if datetime.now() < dueDateDate:
  79. #print("No need to renew")
  80. report_message = report_message + "No need to renew\n"
  81. if dueDateDate < NextRunDate:
  82. NextRunDate = dueDateDate
  83.  
  84. else:
  85. #print("need to renew")
  86. report_message = report_message + "need to renew\n"
  87. box = cells[0].find_element_by_tag_name("input")
  88. box.click()
  89. time.sleep(1)
  90. anythingToRenew = anythingToRenew + 1
  91. print("fin loop")
  92. print(report_message)
  93. if anythingToRenew > 0:
  94. renewButton = driver.find_element_by_id("button.renew")
  95. renewButton.click()
  96. time.sleep(1)
  97. driver.get("https://webcat.hkpl.gov.hk/auth/logout?theme=WEB&locale=en")
  98. assert "Hong Kong Public Libraries" in driver.title
  99. time.sleep(1)
  100. logout = driver.find_element_by_css_selector("#content > div > div > div.butn > button:nth-child(1)")
  101. logout.click()
  102. time.sleep(5)
  103.  
  104. #start of program
  105. driver = webdriver.Chrome()
  106. #renewLibraryBooks(23838005962994,"7312Lowe","Raymond")
  107. renewLibraryBooks(23838010711451,"7312Lowe","妈妈")
  108. #renewLibraryBooks(23838027232400,"7312Lowe","Edward")
  109. #renewLibraryBooks(23838026171781,"7312Lowe","Gerald")
  110.  
  111. message = report_message
  112. # setup the parameters of the message
  113. emailPassword = "library2018"
  114. msg['From'] = "library@edwardtlowe.com"
  115. msg['To'] = "lowee1@wis.edu.hk,raymondclowe@gmail.com,wwtsoi11@gmail.com"
  116. msg['Subject'] = "Library report" + " Date:" + datetime.strftime(datetime.now(),"%B %d, %y") + "Need to Return:" + anythingReturn
  117. # add in the message body
  118. msg.attach(MIMEText(message, 'plain'))
  119.  
  120. #create server
  121. server = smtplib.SMTP('mail.edwardtlowe.com: 26')
  122.  
  123. server.starttls()
  124.  
  125. # Login Credentials for sending the mail
  126. server.login(msg['From'], emailPassword)
  127.  
  128.  
  129. # send the message via the server.
  130. server.sendmail(msg['From'], ["lowee1@wis.edu.hk","raymondclowe@gmail.com","wwtsoi11@gmail.com"], msg.as_string())
  131.  
  132. server.quit()
  133.  
  134. print (("successfully sent email to %s:" % (msg['To'])))
  135. # print(cells[2].text)
  136. # for cell in cells:
  137. # print(cell.text)
  138. # print(row.text)
  139. print("changing task scheduler to NextRunDate:",NextRunDate)
  140.  
  141. taskstring = "schtasks /change /tn \"Daily run library renewal and sleep\" /ru Student /rp yhnphsom /sd " + datetime.strftime(NextRunDate,"%d-%m-%Y")
  142.  
  143. print(taskstring)
  144.  
  145. subprocess.call(taskstring)
  146.  
  147.  
  148. time.sleep(10)
  149. driver.close()
  150. exit()
  151. #End of program
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement