Cloud Computing Course

142 lines
6.0 KiB

4 years ago
  1. import sys
  2. import time
  3. import datetime
  4. from selenium import webdriver
  5. from selenium.webdriver.common.action_chains import ActionChains
  6. from selenium.webdriver.chrome.options import Options
  7. import smtplib
  8. from email.mime.text import MIMEText
  9. from email.header import Header
  10. from email.utils import formataddr
  11. from pyvirtualdisplay import Display
  12. display = Display(visible=0, size=(800, 800))
  13. display.start()
  14. if __name__ == "__main__":
  15. #prepare info
  16. username=sys.argv[1]
  17. password=sys.argv[2]
  18. verificationCode="1111"
  19. area=sys.argv[4]
  20. email=sys.argv[3]
  21. #login page and num of seats
  22. if area=='F1_A':
  23. url=('http://seats.lib.ecnu.edu.cn/web/seat3?area=8&segment=1404341&day='+datetime.datetime.now().strftime('%Y-%m-%d')
  24. +'&startTime='+datetime.datetime.now().strftime('%H:%M')+'&endTime=23:50')
  25. seatsNum=76
  26. elif area=='F1_B':
  27. url=('http://seats.lib.ecnu.edu.cn/web/seat3?area=40&segment=1404354&day='+datetime.datetime.now().strftime('%Y-%m-%d')
  28. +'&startTime='+datetime.datetime.now().strftime('%H:%M')+'&endTime=23:50')
  29. seatsNum=150
  30. elif area=='F1_digital_reading_area':
  31. url=('http://seats.lib.ecnu.edu.cn/web/seat3?area=7&segment=1404328&day='+datetime.datetime.now().strftime('%Y-%m-%d')
  32. +'&startTime='+datetime.datetime.now().strftime('%H:%M')+'&endTime=23:50')
  33. seatsNum=24
  34. elif area=='F1_study_area':
  35. url=('http://seats.lib.ecnu.edu.cn/web/seat3?area=3&segment=1404315&day='+datetime.datetime.now().strftime('%Y-%m-%d')
  36. +'&startTime='+datetime.datetime.now().strftime('%H:%M')+'&endTime=23:50')
  37. seatsNum=48
  38. elif area=='F3_foreign_books':
  39. url=('http://seats.lib.ecnu.edu.cn/web/seat3?area=14&segment=1404198&day='+datetime.datetime.now().strftime('%Y-%m-%d')
  40. +'&startTime='+datetime.datetime.now().strftime('%H:%M')+'&endTime=21:55')
  41. seatsNum=66
  42. elif area=='F3_newspaper':
  43. url=('http://seats.lib.ecnu.edu.cn/web/seat3?area=13&segment=1404185&day='+datetime.datetime.now().strftime('%Y-%m-%d')
  44. +'&startTime='+datetime.datetime.now().strftime('%H:%M')+'&endTime=21:55')
  45. seatsNum=68
  46. elif area=='F4_liberal_art_books_A':
  47. url=('http://seats.lib.ecnu.edu.cn/web/seat3?area=16&segment=1404224&day='+datetime.datetime.now().strftime('%Y-%m-%d')
  48. +'&startTime='+datetime.datetime.now().strftime('%H:%M')+'&endTime=21:55')
  49. seatsNum=72
  50. elif area=='F4_liberal_art_books_B':
  51. url=('http://seats.lib.ecnu.edu.cn/web/seat3?area=17&segment=1404237&day='+datetime.datetime.now().strftime('%Y-%m-%d')
  52. +'&startTime='+datetime.datetime.now().strftime('%H:%M')+'&endTime=21:55')
  53. seatsNum=90
  54. elif area=='F4_liberal_art_books_C':
  55. url=('http://seats.lib.ecnu.edu.cn/web/seat3?area=18&segment=1404250&day='+datetime.datetime.now().strftime('%Y-%m-%d')
  56. +'&startTime='+datetime.datetime.now().strftime('%H:%M')+'&endTime=21:55')
  57. seatsNum=98
  58. elif area=='F5_liberal_art_books':
  59. url=('http://seats.lib.ecnu.edu.cn/web/seat3?area=20&segment=1404276&day='+datetime.datetime.now().strftime('%Y-%m-%d')
  60. +'&startTime='+datetime.datetime.now().strftime('%H:%M')+'&endTime=21:55')
  61. seatsNum=154
  62. else:
  63. url=('http://seats.lib.ecnu.edu.cn/web/seat3?area=22&segment=1404302&day='+datetime.datetime.now().strftime('%Y-%m-%d')
  64. +'&startTime='+datetime.datetime.now().strftime('%H:%M')+'&endTime=21:55')
  65. seatsNum=18
  66. options = webdriver.ChromeOptions()
  67. options.add_argument('--headless')
  68. options.add_argument('--disable-gpu')
  69. options.add_argument("--no-sandbox")
  70. options.add_argument('window-size=1920x3000')
  71. #initiate a explorer object
  72. window=webdriver.Chrome(executable_path ='/usr/bin/chromedriver',options=options)
  73. window.get(url)
  74. time.sleep(3)
  75. #close dialog box
  76. ActionChains(window).move_by_offset(1, 1).click().perform()
  77. #login
  78. window.find_element_by_xpath('/html/body/div[2]/div[1]/ul/li[6]/a').click()
  79. #input username, password and verification code
  80. inputUsername=window.find_element_by_name("username")
  81. inputPassword=window.find_element_by_name("password")
  82. inputVerificationCode=window.find_element_by_name("verify")
  83. inputUsername.send_keys(username)
  84. inputPassword.send_keys(password)
  85. inputVerificationCode.send_keys(verificationCode)
  86. #click login button
  87. window.find_element_by_class_name("ui-dialog-autofocus").click()
  88. time.sleep(2)
  89. ActionChains(window).move_by_offset(1, 1).click().perform()
  90. time.sleep(2)
  91. ActionChains(window).move_by_offset(1, 1).click().perform()
  92. #traverse to book a seat
  93. flag=0
  94. timenow=datetime.datetime.now()
  95. while datetime.datetime.now()<(timenow+datetime.timedelta(hours=1)):
  96. for i in range(1,seatsNum+1):
  97. xPath=('//*[@id="floor"]/ul/li['+str(i)+']')
  98. print(xPath)
  99. if window.find_element_by_xpath(xPath).get_attribute("class")=='seat ava-icon':
  100. window.find_element_by_xpath(xPath).click()
  101. time.sleep(3)
  102. window.find_element_by_class_name('ui-dialog-autofocus').click()
  103. flag=1
  104. break
  105. if flag == 1:
  106. break
  107. window.refresh()
  108. time.sleep(8)
  109. #notification
  110. sender='anthonyyoung_ecnu@foxmail.com'
  111. pwd='cleedzbuzlgrbgba'
  112. receiver=email
  113. if flag==1:
  114. msg=MIMEText('您的预定已成功!请到图书馆个人中心查看!','plain','utf-8')
  115. msg['From']=formataddr(["One-touch Book!",sender])
  116. msg['To']=formataddr(["",receiver])
  117. msg['Subject']="已为您保留座位"
  118. else:
  119. msg=MIMEText('您的预定超时!','plain','utf-8')
  120. msg['From']=formataddr(["One-touch Book!",sender])
  121. msg['To']=formataddr(["",receiver])
  122. msg['Subject']="预定失败"
  123. server=smtplib.SMTP_SSL("smtp.qq.com", 465)
  124. server.login(sender, pwd)
  125. server.sendmail(sender,[receiver,],msg.as_string())
  126. server.quit()