Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- import os
- import re
- import sys
- import time
- import requests
- API_URL = 'https://allocate.timetable.monash.edu/aplus-2016/rest/student/'
- HOMEPAGE_URL = 'https://allocate.timetable.monash.edu/aplus-2016/student/'
- USERNAME = 'jccam5'
- PASSWORD = 'PASSWORD'
- PREFERENCES = {
- 'FIT1047_CL_S1_DAY': {
- 'Laboratory': 22,
- },
- }
- class AllocatePlus:
- def __init__(self, session, data):
- self.session = session
- self.data = data
- def update_data(self):
- self.data = self.session.get(self.get_api_url('{student[student_code]}/')).json()
- def get_api_url(self, url, *args, **kwargs):
- return API_URL + url.format(*args, **kwargs, **self.data)
- def send_preference(self, subject_code, activity_group_code, activity_code):
- return self.session.post(self.get_api_url('changeActivity/'), data={
- 'token': 'a',
- 'student_code': self.data['student']['student_code'],
- 'subject_code': subject_code,
- 'activity_group_code': activity_group_code,
- 'activity_code': activity_code
- }).json()
- def get_preference(self, subject_code, activity_group_code):
- raise NotImplementedError("get_preference")
- @classmethod
- def login(cls, username, password):
- s = requests.Session()
- login = s.post(API_URL + 'login', data={'username': username, 'password': password})
- s.params.update({'ss': login.json()['token']})
- homepage = s.get(HOMEPAGE_URL)
- data = json.loads(re.search(r'^data=([^;]+);$', homepage.text, re.M).group(1))
- return cls(s, data)
- if __name__ == '__main__':
- if not USERNAME or not PASSWORD:
- print("Set USERNAME and PASSWORD in the file")
- sys.exit(1)
- exists = os.path.exists('already_set.json')
- if exists:
- with open('already_set.json') as f:
- already_set = json.load(f)
- else:
- already_set = {}
- ap = AllocatePlus.login(USERNAME, PASSWORD)
- for unit in PREFERENCES:
- for activity in PREFERENCES[unit]:
- if exists:
- set_yet = unit in already_set and activity in already_set[unit]
- else:
- set_yet = ap.get_preference(unit, activity) == PREFERENCES[unit][activity]
- if not set_yet:
- if unit not in not_set:
- not_set[unit] = {}
- not_set[unit][activity] = PREFERENCES[unit][activity]
- i = 0
- while not_set:
- i += 1
- for unit in not_set:
- for activity in not_set[unit]:
- try:
- print(unit, activity, i)
- result = ap.send_preference(unit, activity, not_set[unit][activity])
- if result['success']:
- if unit not in already_set:
- already_set[unit] = {}
- already_set[unit][activity] = not_set[unit][activity]
- del not_set[unit][activity]
- if not not_set[unit]:
- del not_set[unit]
- with open('already_set.json', 'w') as f:
- json.dump(already_set, f)
- except Exception as e:
- print("Found exception", e)
- time.sleep(sleep_time)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement