Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from selenium import webdriver
- from selenium.webdriver.common.keys import Keys
- import time
- from datetime import datetime, timedelta
- #email lib
- from email.mime.multipart import MIMEMultipart
- from email.mime.text import MIMEText
- import smtplib
- import subprocess
- # create message object instance
- msg = MIMEMultipart()
- #user = "23838005962994"
- #pwd = "7312Lowe"
- report_message = "Library report\n\n"
- anythingReturn = "False"
- bookNo = 0
- NextRunDate = datetime.now() + timedelta(days=12)
- def waitPageTitle():
- pageloadedok = False
- while not pageloadedok:
- driver.get("https://www.hkpl.gov.hk/en/login.html")
- timeout = 10
- while (not ("Hong Kong Public Libraries" in driver.title)) and (timeout>0):
- time.sleep(1)
- timeout = timeout-1
- if "Hong Kong Public Libraries" in driver.title:
- print("Yah it is ok")
- pageloadedok = True
- else:
- print("Timed out")
- time.sleep(60)
- driver.refresh()
- time.sleep(60)
- def renewLibraryBooks(username,password,patronName):
- global NextRunDate
- global bookNo
- global report_message
- report_message = report_message + "\n Hi! " + str(patronName) + "\n\n"
- daysBeforeDue = 0
- anythingToRenew = 0
- pageloadedok = False
- # assert "Hong Kong Public Libraries" in driver.title
- waitPageTitle()
- elem = driver.find_element_by_id("account")
- elem.send_keys(username)
- time.sleep(0.5)
- elem = driver.find_element_by_id("password")
- elem.send_keys(password)
- elem.send_keys(Keys.RETURN)
- time.sleep(1)
- driver.get("https://webcat.hkpl.gov.hk/wicket/bookmarkable/com.vtls.chamo.webapp.component.patron.PatronAccountPage?theme=WEB&locale=en")
- assert "My Account | Chamo" in driver.title
- table = driver.find_element_by_id("checkout")
- rows = table.find_elements_by_tag_name("tr")
- for row in rows:
- cells = row.find_elements_by_tag_name("td")
- if len(cells) == 6:
- #print(cells[4].text)
- bookNo = bookNo + 1
- report_message = report_message + str(bookNo) + ")" + cells[1].text + " : "
- print(cells[1].text)
- s = cells[5].text
- timesRenewed = s[:1]
- if int(timesRenewed) == 4:
- needReturn = True
- anythingReturn = "True"
- report_message = report_message + "THIS BOOK NEEDS TO BE RETURNED:" + needReturn
- needReturn = True
- dueDateString = cells[4].text
- dueDateDate = datetime.strptime(dueDateString,"%Y-%m-%d")
- #print(dueDateDate)
- #print(datetime.strftime(dueDateDate,"%B %d, %y"))
- if datetime.now() < dueDateDate:
- #print("No need to renew")
- report_message = report_message + "No need to renew\n"
- if dueDateDate < NextRunDate:
- NextRunDate = dueDateDate
- else:
- #print("need to renew")
- report_message = report_message + "need to renew\n"
- box = cells[0].find_element_by_tag_name("input")
- box.click()
- time.sleep(1)
- anythingToRenew = anythingToRenew + 1
- print("fin loop")
- print(report_message)
- if anythingToRenew > 0:
- renewButton = driver.find_element_by_id("button.renew")
- renewButton.click()
- time.sleep(1)
- driver.get("https://webcat.hkpl.gov.hk/auth/logout?theme=WEB&locale=en")
- assert "Hong Kong Public Libraries" in driver.title
- time.sleep(1)
- logout = driver.find_element_by_css_selector("#content > div > div > div.butn > button:nth-child(1)")
- logout.click()
- time.sleep(5)
- #start of program
- driver = webdriver.Chrome()
- #renewLibraryBooks(23838005962994,"7312Lowe","Raymond")
- renewLibraryBooks(23838010711451,"7312Lowe","妈妈")
- #renewLibraryBooks(23838027232400,"7312Lowe","Edward")
- #renewLibraryBooks(23838026171781,"7312Lowe","Gerald")
- message = report_message
- # setup the parameters of the message
- emailPassword = "library2018"
- msg['From'] = "library@edwardtlowe.com"
- msg['To'] = "lowee1@wis.edu.hk,raymondclowe@gmail.com,wwtsoi11@gmail.com"
- msg['Subject'] = "Library report" + " Date:" + datetime.strftime(datetime.now(),"%B %d, %y") + "Need to Return:" + anythingReturn
- # add in the message body
- msg.attach(MIMEText(message, 'plain'))
- #create server
- server = smtplib.SMTP('mail.edwardtlowe.com: 26')
- server.starttls()
- # Login Credentials for sending the mail
- server.login(msg['From'], emailPassword)
- # send the message via the server.
- server.sendmail(msg['From'], ["lowee1@wis.edu.hk","raymondclowe@gmail.com","wwtsoi11@gmail.com"], msg.as_string())
- server.quit()
- print (("successfully sent email to %s:" % (msg['To'])))
- # print(cells[2].text)
- # for cell in cells:
- # print(cell.text)
- # print(row.text)
- print("changing task scheduler to NextRunDate:",NextRunDate)
- taskstring = "schtasks /change /tn \"Daily run library renewal and sleep\" /ru Student /rp yhnphsom /sd " + datetime.strftime(NextRunDate,"%d-%m-%Y")
- print(taskstring)
- subprocess.call(taskstring)
- time.sleep(10)
- driver.close()
- exit()
- #End of program
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement