def login(session, student_id, password, captcha, identification='student'): response = session.post(LOGIN_URL, data={'who': identification, 'id': student_id, 'pwd': password, 'yzm': captcha, 'submit': '%C8%B7 %B6%A8'}) info('retrieving main page') d = pq(response.text) msg = d('td[height="25"][width="401"]').text() if msg: if WRONG_CAPTCHA_TRAIT in msg: raise WrongCaptchaError elif INVALID_ID_OR_PASSWORD_TRAIT in msg: raise InvalidIDOrPasswordError selectors = ('td.line[height="20"][width="70%"]', 'td[height="30"][colspan="6"][align="center"]', 'td.line[height="20"][colspan="3"]') return map(lambda selector: d(selector).text(), selectors)
def get_captcha(session): response = session.get(CAPTCHA_URL) info('retrieving captcha') img_data = response.content return img_data
def insert_day(self, schedules, echo=True): for schedule in schedules: event = gdata.calendar.data.CalendarEventEntry() event.title = atom.data.Title(text=schedule['title']) event.when.append(gdata.calendar.data.When (start=schedule['start_date'], end=schedule['end_date'])) if echo: info('inserting %s at %s to %s' % (schedule['title'], schedule['start_date'], schedule['end_date'])) self.client.InsertEvent(event, self.current_calendar.content.src)
def __init__(self, calendar_timezone, calendar_location): gmail_account = get_account_info(configs[CONFIG_KEY_GMAIL_ACCOUNT], GMAIL_PROMPT) gmail_pwd = get_account_info(configs[CONFIG_KEY_GMAIL_PWD], PASSWORD_PROMPT, True) self.client = gdata.calendar.client.CalendarClient( source=GoogleCalendarProxy.app_source ) try: info('logging in as %s' % gmail_account) self.client.ClientLogin(gmail_account, gmail_pwd, self.client.source) for calendar in self.client.GetOwnCalendarsFeed().entry: if calendar.summary and\ calendar.summary.text == configs[CONFIG_KEY_THIS_CALENDAR_SUMMARY]: if configs[CONFIG_KEY_IF_THIS_CALENDAR_FOUND_THEN_PERFORM_DELETE]: info('previously created calendar %s found,' 'deleting' % calendar.title.text) self.client.Delete(calendar.GetEditLink().href) self.current_calendar = self.client.InsertCalendar( new_calendar=self.gen_calender( configs[CONFIG_KEY_THIS_CALENDAR_TITLE], configs[CONFIG_KEY_THIS_CALENDAR_SUMMARY], configs[CONFIG_KEY_THIS_CALENDAR_COLOR], calendar_timezone, calendar_location ) ) else: self.current_calendar = calendar break else: self.current_calendar = self.client.InsertCalendar( new_calendar=self.gen_calender( configs[CONFIG_KEY_THIS_CALENDAR_TITLE], configs[CONFIG_KEY_THIS_CALENDAR_SUMMARY], configs[CONFIG_KEY_THIS_CALENDAR_COLOR], calendar_timezone, calendar_location ) ) dbg('calendar id = %s' % self.current_calendar.content.src) except gdata.client.RequestError as e: err(e) return
def grab_courses(session, page_url=MY_COURSES_URL, parser=None): dbg('pageURL=%s' % page_url) info('retrieving course data of url %s' % page_url) response = session.get(page_url) d = pq(response.text) pq_course_data = d('tr.TR_BODY[align="center"]') if parser: return parser(pq_course_data) else: return pq_course_data
def insert(self, schedules, title, content, echo=True): for schedule in schedules: event = gdata.calendar.data.CalendarEventEntry() event.title = atom.data.Title(text=title) event.content = atom.data.Content(text=content) event.where.append(gdata.data.Where(value=schedule[WHERE])) event.recurrence = gdata.data.Recurrence( text=CourseRecurrence(schedule).recurrence_text ) if echo: info('inserting %s %s at %s' % (title, content, event.where[0].value)) self.client.InsertEvent(event, self.current_calendar.content.src)
def __init__(self, courses): self.courses = [] info('digesting courses') for course in courses: schedules = [] for schedule in course: if not schedule: continue actual_date = self.get_actual_date(int(schedule[STARTING_WEEK]), schedule[DAY]) d = { ACTUAL_START_TIME: self.get_actual_time(actual_date, schedule[STARTING_CLASS_NUMBER], 'START'), ACTUAL_END_TIME: self.get_actual_time(actual_date, schedule[ENDING_CLASS_NUMBER], 'END'), COUNT: int(schedule[ENDING_WEEK]) -\ int(schedule[STARTING_WEEK]) + 1, FREQUENCY: schedule[FREQUENCY], BYDAY: CourseRecurrence.DAYS_TO_ABBRS[schedule[DAY]], WHERE: u'%s区, %s' % (schedule[DISTRICT], gen_location_by_schedule(schedule)) } schedules.append(d) course = { COURSE_NAME: course.property(COURSE_NAME), TEACHER_NAME: course.property(TEACHER_NAME), SCHEDULE: schedules } self.courses.append(course) info('%s courses digested' % len(self.courses)) self.proxy = GoogleCalendarProxy(THIS_CALENDAR_TIMEZONE, THIS_CALENDAR_LOCATION)
def load_courses(student_id, password): session = requests.Session() captcha = get_captcha(session) with open('.\captcha.jpg', 'wb') as f: f.write(captcha) info('captcha saved at %s' % f.name) # captcha recognition will be implemented in future versions captcha = raw_input('captcha? ') student_name, date, semester = login(session, student_id, password, captcha) if student_name: info('welcome %s %s %s' % (student_name, date, semester)) else: info('strange, nothing was transmitted,' 'perhaps wrong password or captcha?') return student_name, grab_courses(session, parser=my_course_parser)
def logout(session): response = session.post(LOGOUT_URL) info('logging out') if LOGOUT_TRAIT in response.text: info('logout succeeded')