Advertisement
Guest User

Untitled

a guest
May 24th, 2018
864
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.37 KB | None | 0 0
  1. from selenium import webdriver
  2. from selenium.webdriver.common.keys import Keys
  3. import time
  4. import sys
  5. import os
  6. import smtplib
  7.  
  8. print "\nChecking student center for new grades:"
  9. sys.stdout.write('.')
  10. sys.stdout.flush()
  11.  
  12. driver = webdriver.PhantomJS()
  13.  
  14. try:
  15.     f = [i.strip() for i in open('login_info.txt').readlines()]
  16.     username = f[0]
  17.     password = f[1]
  18.     prevCredits = f[2]
  19. except Exception as e:
  20.     print "\n\n*** Your login_info.txt isn't setup correctly! ***"
  21.     print "You need a login_info.txt in this folder in the following format:\n"
  22.     print "netid"
  23.     print "password"
  24.     print "credits you know are graded already"
  25.     print "\ne.g.\n"
  26.     print "abc123\np@ssword123\n4.000"
  27.     exit()
  28.  
  29. sys.stdout.write('.')
  30. sys.stdout.flush()
  31.  
  32. NETWORK_SLOWNESS = 1
  33.  
  34. driver.get("https://studentcenter.cornell.edu")
  35.  
  36. usernameinput = driver.find_element_by_name("netid")
  37. passwdinput = driver.find_element_by_name("password")
  38.  
  39. usernameinput.send_keys(username)
  40. passwdinput.send_keys(password)
  41. driver.find_element_by_name("Submit").click()
  42.  
  43. sys.stdout.write('.')
  44. sys.stdout.flush()
  45.  
  46. time.sleep(NETWORK_SLOWNESS)
  47. time.sleep(NETWORK_SLOWNESS)
  48. if driver.title == "Cornell University Web Login":
  49.     print "\n\n*** Looks like your username and password are wrong. Double check and try again. ***"
  50.     exit()
  51.  
  52. sys.stdout.write('.')
  53. sys.stdout.flush()
  54.  
  55. found = False
  56. while (not found):
  57.     # print "Finding iframe"
  58.     sys.stdout.flush()
  59.     time.sleep(NETWORK_SLOWNESS)
  60.     try:
  61.         iframe = driver.find_element_by_id("ptifrmtgtframe")
  62.         driver.switch_to.frame(iframe)
  63.         found = True
  64.     except Exception as e:
  65.         # print e
  66.         pass
  67.  
  68. sys.stdout.write('.')
  69. sys.stdout.flush()
  70.  
  71. found = False
  72. dropdown = None
  73. while (not found):
  74.     # print "Finding dropdown"
  75.     sys.stdout.flush()
  76.     time.sleep(NETWORK_SLOWNESS)
  77.     try:
  78.         dropdown = driver.find_element_by_id("DERIVED_SSS_SCL_SSS_MORE_ACADEMICS")
  79.         found = True
  80.     except Exception as e:
  81.         # print e
  82.         pass
  83.  
  84.  
  85. sys.stdout.write('.')
  86. sys.stdout.flush()
  87.  
  88. # print "Selecting Grades"
  89. sys.stdout.flush()
  90. dropdown.click()
  91. dropdown.send_keys("G")
  92. dropdown.send_keys(Keys.RETURN)
  93. # dropdown.send_keys(Keys.TAB)
  94. # driver.switch_to.active_element.send_keys(Keys.RETURN)
  95. time.sleep(NETWORK_SLOWNESS)
  96. driver.find_element_by_id("DERIVED_SSS_SCL_SSS_GO_1").click()
  97.  
  98. sys.stdout.write('.')
  99. sys.stdout.flush()
  100.  
  101. driver.switch_to.default_content()
  102. found = False
  103. while (not found):
  104.     # print "Finding iframe"
  105.     sys.stdout.flush()
  106.     time.sleep(NETWORK_SLOWNESS)
  107.     try:
  108.         iframe = driver.find_element_by_id("ptifrmtgtframe")
  109.         driver.switch_to.frame(iframe)
  110.         found = True
  111.     except Exception as e:
  112.         # print e
  113.         pass
  114.  
  115. sys.stdout.write('.')
  116. sys.stdout.flush()
  117.  
  118. # print "Changing Term"
  119. sys.stdout.flush()
  120. time.sleep(NETWORK_SLOWNESS*2)
  121. driver.find_element_by_id("DERIVED_SSS_SCT_SSS_TERM_LINK").click()
  122.  
  123. driver.switch_to.default_content()
  124. found = False
  125. while (not found):
  126.     # print "Finding iframe"
  127.     sys.stdout.flush()
  128.     time.sleep(NETWORK_SLOWNESS)
  129.     try:
  130.         iframe = driver.find_element_by_id("ptifrmtgtframe")
  131.         driver.switch_to.frame(iframe)
  132.         found = True
  133.     except Exception as e:
  134.         # print e
  135.         pass
  136.  
  137. sys.stdout.write('.')
  138. sys.stdout.flush()
  139.  
  140. # print "Choosing last semester"
  141. sys.stdout.flush()
  142. driver.find_element_by_id("SSR_DUMMY_RECV1$sels$1$$0").click()
  143. driver.find_element_by_id("DERIVED_SSS_SCT_SSR_PB_GO").click()
  144.  
  145. driver.switch_to.default_content()
  146. found = False
  147. while (not found):
  148.     # print "Finding iframe"
  149.     sys.stdout.flush()
  150.     time.sleep(NETWORK_SLOWNESS)
  151.     try:
  152.         iframe = driver.find_element_by_id("ptifrmtgtframe")
  153.         driver.switch_to.frame(iframe)
  154.         found = True
  155.     except Exception as e:
  156.         # print e
  157.         pass
  158.  
  159. print ".\n"
  160. sys.stdout.flush()
  161.  
  162. currCred = driver.find_element_by_id("STATS_ENRL$2").get_attribute('innerHTML')
  163. table = driver.find_element_by_id("TERM_CLASSES$scroll$0").get_attribute('innerHTML')
  164.  
  165. if (float(currCred) == float(prevCredits)):
  166.     print "No new grades, carry on with your life."
  167. else:
  168.     F = open("login_info.txt","w")
  169.     F.write(username+"\n")
  170.     F.write(password+"\n")
  171.     F.write(currCred+"\n")
  172.     print "New grades are posted! I've updated the login_info.txt so you won't be notified again."
  173.     print "Current graded credits: "+str(currCred)
  174.  
  175.     # gmail_user = username+'@cornell.edu'
  176.     # gmail_password = password
  177.  
  178.     # sent_from = gmail_user
  179.     # to = [gmail_user]
  180.     # subject = 'New Grades'
  181.     body = "<html>\n<head>\n</head>\n<body>\n"+table+"\n</body>\n</html>"
  182.  
  183.     # email_text = """\
  184.     # From: %s
  185.     # To: %s
  186.     # Subject: %s
  187.  
  188.     # %s
  189.     # """ % (sent_from, ", ".join(to), subject, body)
  190.  
  191.     # try:
  192.     #     server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
  193.     #     server.ehlo()
  194.     #     server.login(gmail_user, gmail_password)
  195.     #     server.sendmail(sent_from, to, email_text)
  196.     #     server.close()
  197.  
  198.     #     print 'Email sent with your grades attached!'
  199.     # except Exception as e:
  200.     #     print "The email didn't send, check StudentCenter."
  201.     #     raise e
  202.  
  203.     G = open("grades.html","w")
  204.     G.write(body)
  205.  
  206.     print "Your grades are in grades.html, open it with a browser."
  207.  
  208. driver.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement