Advertisement
Guest User

Untitled

a guest
Jul 20th, 2016
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.37 KB | None | 0 0
  1. import json
  2. import os
  3. import re
  4. import sys
  5. import time
  6.  
  7. import requests
  8.  
  9.  
  10. API_URL = 'https://allocate.timetable.monash.edu/aplus-2016/rest/student/'
  11. HOMEPAGE_URL = 'https://allocate.timetable.monash.edu/aplus-2016/student/'
  12.  
  13. USERNAME = 'jccam5'
  14. PASSWORD = 'PASSWORD'
  15.  
  16. PREFERENCES = {
  17.     'FIT1047_CL_S1_DAY': {
  18.         'Laboratory': 22,
  19.     },
  20. }
  21.  
  22. class AllocatePlus:
  23.     def __init__(self, session, data):
  24.         self.session = session
  25.         self.data = data
  26.  
  27.     def update_data(self):
  28.         self.data = self.session.get(self.get_api_url('{student[student_code]}/')).json()
  29.  
  30.     def get_api_url(self, url, *args, **kwargs):
  31.         return API_URL + url.format(*args, **kwargs, **self.data)
  32.  
  33.     def send_preference(self, subject_code, activity_group_code, activity_code):
  34.         return self.session.post(self.get_api_url('changeActivity/'), data={
  35.             'token': 'a',
  36.             'student_code': self.data['student']['student_code'],
  37.             'subject_code': subject_code,
  38.             'activity_group_code': activity_group_code,
  39.             'activity_code': activity_code
  40.         }).json()
  41.  
  42.     def get_preference(self, subject_code, activity_group_code):
  43.         raise NotImplementedError("get_preference")
  44.  
  45.     @classmethod
  46.     def login(cls, username, password):
  47.         s = requests.Session()
  48.         login = s.post(API_URL + 'login', data={'username': username, 'password': password})
  49.         s.params.update({'ss': login.json()['token']})
  50.         homepage = s.get(HOMEPAGE_URL)
  51.         data = json.loads(re.search(r'^data=([^;]+);$', homepage.text, re.M).group(1))
  52.         return cls(s, data)
  53.  
  54. if __name__ == '__main__':
  55.     if not USERNAME or not PASSWORD:
  56.         print("Set USERNAME and PASSWORD in the file")
  57.         sys.exit(1)
  58.  
  59.     exists = os.path.exists('already_set.json')
  60.  
  61.     if exists:
  62.         with open('already_set.json') as f:
  63.             already_set = json.load(f)
  64.     else:
  65.         already_set = {}
  66.  
  67.     ap = AllocatePlus.login(USERNAME, PASSWORD)
  68.  
  69.     for unit in PREFERENCES:
  70.         for activity in PREFERENCES[unit]:
  71.             if exists:
  72.                 set_yet = unit in already_set and activity in already_set[unit]
  73.             else:
  74.                 set_yet = ap.get_preference(unit, activity) == PREFERENCES[unit][activity]
  75.  
  76.             if not set_yet:
  77.                 if unit not in not_set:
  78.                     not_set[unit] = {}
  79.  
  80.                 not_set[unit][activity] = PREFERENCES[unit][activity]
  81.  
  82.     i = 0
  83.  
  84.     while not_set:
  85.         i += 1
  86.         for unit in not_set:
  87.             for activity in not_set[unit]:
  88.                 try:
  89.                     print(unit, activity, i)
  90.                     result = ap.send_preference(unit, activity, not_set[unit][activity])
  91.  
  92.                     if result['success']:
  93.                         if unit not in already_set:
  94.                             already_set[unit] = {}
  95.  
  96.                         already_set[unit][activity] = not_set[unit][activity]
  97.  
  98.                         del not_set[unit][activity]
  99.  
  100.                         if not not_set[unit]:
  101.                             del not_set[unit]
  102.  
  103.                         with open('already_set.json', 'w') as f:
  104.                             json.dump(already_set, f)
  105.                 except Exception as e:
  106.                     print("Found exception", e)
  107.                 time.sleep(sleep_time)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement