Advertisement
Guest User

Whats wrong?

a guest
Nov 26th, 2014
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.09 KB | None | 0 0
  1. import os #File exsistance
  2. import xml.etree.cElementTree as ETX # for config file
  3. import requests # for login and html
  4. from bs4 import BeautifulSoup # to parse html
  5.  
  6. class GradesParser(object):
  7. Sesh = requests.session()
  8. LoggedIn = False
  9. Config_File = "config.xml"
  10. def __init__(self, AutoRun=False):
  11. #Check for config
  12. #Create config (If one doesnt exsist)
  13. #Read Config for user/password
  14. #Login
  15. #GET page with grades
  16. #Parse Grades into good format
  17. #Output to file or screen
  18. #Logout
  19. if AutoRun:
  20. self.CheckConfig()
  21. x = self.ReadConfig()
  22. if self.Login(x.get('username'), x.get('password')):
  23. self.LogOut()
  24. else:
  25. pass
  26.  
  27.  
  28. def CheckConfig(self):
  29. #check if config file exists at 'location'
  30. #If a config file doesnt exsist, create one CreateConfig()
  31. if not os.path.isfile(GradesParser.Config_File): #Check for config file exsistance
  32. username = input("Please enter your username for config file creation: ")
  33. password = input("Password: ")
  34. self.CreateConfig(username, password) #Pass info to createconfig()
  35. else:
  36. return 0
  37.  
  38.  
  39. def CreateConfig(self, user, password):
  40. #Create config file at 'location', with info
  41. #Supplied when the function is called
  42. root = ETX.Element("Grades") #Creates root tag
  43.  
  44. username = ETX.SubElement(root, "username") #Created user key
  45. username.text = user #Adds username to key
  46.  
  47. passw = ETX.SubElement(root, "password") #Creates password key
  48. passw.text = password #Adds password to key
  49.  
  50. tree = ETX.ElementTree(root)#Ends root tag
  51. print(tree.write(GradesParser.Config_File))
  52.  
  53.  
  54. def ReadConfig(self):
  55. #Read config file at 'location' using XML
  56. #Return object of variables
  57. tree = ETX.parse(GradesParser.Config_File) #Opens config file
  58. root = tree.getroot() #Gets config File root
  59. data = {root[0].tag:root[0].text, root[1].tag:root[1].text}
  60. #Loads The first and second <tag>key</tag> from the config file
  61. #into a dictionary object
  62. return data
  63.  
  64. def Login(self, user, passw):
  65.  
  66. payload = dict(password=passw, email=user)
  67. Logd = GradesParser.Sesh.post('https://www.teacherease.com/common/LoginResponse.aspx', data=payload)
  68. LoginPage = requests.get('http://www.teacherease.com/parents/main.aspx') #Get defualt login page
  69. Restricted = GradesParser.Sesh.get('http://www.teacherease.com/parents/main.aspx') #Attempt to access a restricted page
  70. #How it works:
  71. #If the login fails, it redirects you to the defualt login page
  72. #Id the login succedes, it will redirect you to the parent home
  73. #So If you get a copy of the default login page, and check it
  74. #Against the text coming from the restricted page, you can tell
  75. #Wether the login failed or succeeded
  76. #Kind of a shitty slow way to check, but oh well
  77. if LoginPage.text == Restricted.text:
  78. print("FATAL: Login Failed!")
  79. return 1
  80. else:
  81. GradesParser.LoggedIn = True
  82. return True
  83.  
  84. def LogOut(self):
  85. #CHECK LOGGED IN
  86. #Use the requests library to GET
  87. #the logout url
  88. if self.CheckLoggedIn():
  89. GradesParser.Sesh.get("http://www.teacherease.com/common/logout.aspx")
  90. GradesParser.LoggedIn = False
  91. exit()
  92. else:
  93. print("Error: You must be logged in first.")
  94. return 1
  95.  
  96. def CheckLoggedIn(self):
  97. #Set a global var to true or false
  98. #And check wether true or false
  99. if GradesParser.LoggedIn:
  100. return True
  101. else:
  102. return False
  103.  
  104. def GetGradeData(self):
  105. #CHECK LOGGED IN
  106. #Use the request library to get the html from
  107. #One of the restricted webpages
  108. #return the HTML from the page
  109. if self.CheckLoggedIn:
  110. return repr(GradesParser.Sesh.get("http://www.teacherease.com/parents/StudentProgressSummary.aspx?s=2557880").text)
  111. else:
  112. print("Error: You must be logged in first.")
  113. return 1
  114.  
  115. def ParseGradeData(self, HTML):
  116. #Take html, parse it using BeautifulSoup
  117. #To get only the relevant information
  118. #and save it to a dictionay
  119. #Return the dictionary
  120. soup = BeautifulSoup(HTML) #Define Object
  121. Table = soup.body.table.find_all("table")[2]
  122. #the third nested table in body.table contains the
  123. #list of grades
  124. Entries = Table.find_all("tr")
  125. # the grades are stored in tr tags, there are 17 in this webpage
  126. #only 16 are useful though, the first one is junk
  127. Grades = [] #List object used to store data
  128.  
  129. for sibling in Entries:
  130. Grades.append(sibling.text)
  131. #Final step!!
  132. #Need to clean up the list and remove the junk 1st entry
  133. del Grades[0]
  134. #aaaaaaaaaaaaaaaaaaannddddd
  135. return Grades
  136.  
  137. x = GradesParser()
  138. x.CheckConfig()
  139. y = x.ReadConfig()
  140.  
  141. data = []
  142. if x.Login(y.get('username'), y.get('password')):
  143. for i in x.ParseGradeData(x.GetGradeData()):
  144. data.append(i)
  145. x.LogOut()
  146.  
  147. print(data)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement