def get_data(sem_code, course_code, section_str, start_time, end_time): file_name = course_code + '.json' file_path = os.path.join(index_path, sem_code, 'courses', file_name) if not os.path.exists(file_path): return get_error_json('Course does not exist.') try: with open(file_path, 'r') as file: fcntl.flock(file.fileno(), fcntl.LOCK_SH) data = json.load(file) data_all = data[section_str] time_list = [x['timestamp'] for x in data_all] if start_time == -1: left = 0 else: left = bisect.bisect_left(time_list, int(start_time), 0, len(time_list)) if end_time == -1: righ = len(data_all) else: righ = bisect.bisect_right(time_list, int(end_time), 0, len(time_list)) ret = data_all[left:righ] if int(end_time) > ret[len(ret) - 1]['timestamp']: x = copy.copy(ret[len(ret) - 1]) x['timestamp'] = int(end_time) ret.append(x) return get_correct_json(ret) except: return get_error_json('File error.')
def semester(request): if request.GET: if 'semCode' in request.GET: return retriever.get_sem_info(request.GET['semCode']) else: return get_error_json('Parameter(s) missing.') else: return get_error_json('Please use GET method to get info.')
def course(request): if request.GET: if 'semCode' in request.GET and \ 'courseCode' in request.GET: return retriever.get_course_section(request.GET['semCode'], request.GET['courseCode']) else: return get_error_json('Parameter(s) missing.') else: return get_error_json('Please use GET method to get info.')
def get_sem_info(sem_code): file_path = os.path.join(index_path, str(sem_code), 'info.json') if not os.path.exists(file_path): return get_error_json('Semester does not exist.') try: with open(file_path, 'r') as file: fcntl.flock(file.fileno(), fcntl.LOCK_SH) ret = json.load(file) return get_correct_json(ret) except: return get_error_json('File error.')
def get_course_section(sem_code, course_code): file_name = course_code + '.json' file_path = os.path.join(index_path, sem_code, 'courses', file_name) if not os.path.exists(file_path): return get_error_json('Course does not exist.') try: with open(file_path, 'r') as file: fcntl.flock(file.fileno(), fcntl.LOCK_SH) data = json.load(file) ret = list(data.keys()) return get_correct_json(ret) except: return get_error_json('File error.')
def section(request): if request.GET: if 'semCode' in request.GET and \ 'courseCode' in request.GET and \ 'section' in request.GET: args = [ request.GET['semCode'], request.GET['courseCode'], request.GET['section'], request.GET['startTime'] if 'startTime' in request.GET else -1, request.GET['endTime'] if 'endTime' in request.GET else -1, ] return retriever.get_data(*args) else: return get_error_json('Parameter(s) missing.') else: return get_error_json('Please use GET method to get info.')