daily pastebin goal
21%
SHARE
TWEET

Untitled

a guest Dec 23rd, 2017 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import requests
  2. from bs4 import BeautifulSoup
  3.  
  4.  
  5. class HomeAccess:
  6.  
  7.         def __init__(self, username, password):
  8.             self.username = username
  9.             self.password = password
  10.             self.auth_token = self.get_login_cookie()
  11.             print(self.auth_token)
  12.             schedule_html = self.get_schedule_html()
  13.             raw_classes = self.get_class_list_raw(schedule_html)
  14.             self.classes = self.get_class_list(raw_classes)
  15.             #for x in self.classes:
  16.                 #print(x.semester)
  17.                 #print(x.period)
  18.                # print(x.class_name)
  19.                 #print(x.teacher)
  20.  
  21.  
  22.  
  23.         def get_login_cookie(self):
  24.             url = 'https://istudent.d303.org/HomeAccess/Account/LogOn?ReturnUrl=/HomeAccess'
  25.             cookies = dict([('ASP.NET_SessionId', "gr4crbvhemballgpta3yx2qv;"), ('visid_incap_236520', 'XgwZ7PqTRoCLwNNXFlSmF4EJHloAAAAAQUIPAAAAAADEH32F7QrNaN8No1/sHeiu'), ("SPIHACSiteCode", "")])
  26.             headers = {'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0",
  27.                        "Upgrade-Insecure-Requests": "1",
  28.                        "Content-Type": "application/x-www-form-urlencoded",
  29.                        "Referer": "https://istudent.d303.org/HomeAccess/Account/LogOn?ReturnUrl=%2fHomeAccess%2f",
  30.                        "host": "istudent.d303.org",
  31.                        "Connection": "keep-alive",
  32.                        "Accept-Language": "en-US,en;q=0.5",
  33.                        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
  34.                        }
  35.             data = {"Database": "10",
  36.                     "LogOnDetails.Username": self.username,
  37.                     "LogOnDetails.Password": self.password,
  38.                     }
  39.             r = requests.post(url, cookies=cookies, headers=headers, data=data)
  40.             return r.history[0].cookies.get(name='.AuthCookie', domain='istudent.d303.org', path='/')
  41.  
  42.         def get_schedule_html(self):
  43.             url = 'https://istudent.d303.org/HomeAccess/Content/Student/Classes.aspx'
  44.             headers = {"Accept": 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  45.                        "Accept-Encoding": "gzip",
  46.                        "Connection": "keep-alive",
  47.                        "Host" : "istudent.d303.org",
  48.                        "Referer": "https://istudent.d303.org/HomeAccess/Classes/Classwork",
  49.                        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0"
  50.                        }
  51.             cookies = dict([('ASP.NET_SessionId', "gr4crbvhemballgpta3yx2qv;"), ('visid_incap_236520', 'XgwZ7PqTRoCLwNNXFlSmF4EJHloAAAAAQUIPAAAAAADEH32F7QrNaN8No1/sHeiu'), ("SPIHACSiteCode", ""), ('.AuthCookie', self.auth_token)])
  52.             r = requests.get(url, cookies=cookies, headers=headers)
  53.             return r.content
  54.  
  55.         @staticmethod
  56.         def get_class_list_raw(schedule_html):
  57.             soup = BeautifulSoup(schedule_html, 'lxml')
  58.             classes = soup.find_all('tr', {'class': 'sg-asp-table-data-row'})
  59.             return classes
  60.  
  61.         # pass a BeautifulSoup result set, returns list of type Class
  62.         def get_class_list(self, raw_list_html):
  63.             class_list = []
  64.             for x in raw_list_html:
  65.                 class_list.append(Class(self.get_class_name_from_raw(x), self.get_class_period_from_raw(x), self.get_class_semester_from_raw(x), self.get_teacher_from_raw(x)))
  66.             return class_list
  67.  
  68.  
  69.         @staticmethod
  70.         def get_class_period_from_raw(raw_class_html):
  71.             soup = BeautifulSoup(str(raw_class_html), 'lxml')
  72.             period = soup.find_all('td')[2]
  73.             period = str(period).replace('<td>','')
  74.             period = period.replace('</td>', '')
  75.             period = period.replace(' ', '')
  76.             return period
  77.  
  78.         @staticmethod
  79.         def get_class_semester_from_raw(raw_class_html):
  80.             raw_class_html = str(raw_class_html)
  81.             if 'Q1' in raw_class_html:
  82.                 return 1
  83.             elif 'Q3' in raw_class_html:
  84.                 return 2
  85.  
  86.         @staticmethod
  87.         def get_class_name_from_raw(raw_class_html):
  88.             soup = BeautifulSoup(str(raw_class_html), 'lxml')
  89.             name = soup.a
  90.             name = str(name)
  91.             name = name[name.index(";\">")+3:name.index("</a>")]
  92.             return name
  93.  
  94.         @staticmethod
  95.         def get_teacher_from_raw(raw_class_html):
  96.             soup = BeautifulSoup(str(raw_class_html), 'lxml')
  97.             teacherTd = soup.findAll('td')[3]
  98.             teacher = str(teacherTd).replace('<td>','')
  99.             teacher = teacher.replace('</td>', '')
  100.             return teacher
  101.  
  102.         def get_class(self, semester, period):
  103.             if int(period) in range(4,7):
  104.                 period = str(period)
  105.                 period = period + 'A-' + period + 'B'
  106.             for x in self.classes:
  107.                 if x.period == period and x.semester == semester:
  108.                     return x
  109.             return 'None'
  110.  
  111.  
  112. class Class:
  113.  
  114.     def __init__(self, class_name, period, semester, teacher):
  115.         self.class_name = class_name
  116.         self.period = period
  117.         self.semester = semester
  118.         self.teacher = teacher
  119.  
  120. home = HomeAccess(username, password)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top