def get_user_usos_id_and_name(user: User) -> dict: """Get user usos_id, first_name and last_name :param user: User that has an active session :returns: Dict that contains usos_id, first_name and last_name """ fields = ['id', 'first_name', 'last_name'] r = user.api_post(BASE_URL + USER_URL, data={'fields': '|'.join(fields)}) return r.json()
def get_timetable_for_tommorow(user: User): """Get timetable for tommorow for specified user :param user: User that has an active session :returns: User's timetable for tommorow """ # TODO: Make timetable class and return it r = user.api_post(BASE_URL + TIMETABLE_URL, data={'days': 4}) return r.json()
def get_user_points(user: User) -> set: """Get all points that user has scored in all courses :param user: User that has an active session :returns: Set of all points scored by user """ r = user.api_get(BASE_URL + TEST_PARTICIPANT_URL) user_points = set() for term in get_active_terms_ids(user): if term not in r.json()['tests']: continue for root_id, root_content in r.json()['tests'][term].items(): fields = [ 'node_id', 'root_id', 'parent_id', 'name', 'type', 'subnodes' ] r = user.api_post(BASE_URL + NODE_URL, data={ 'node_id': root_id, 'recursive': 'true', 'fields': '|'.join(fields) }) root_node = Node.from_json(r.json(), None) pkt_node_ids = [ str(i) for i in Node.search_tree( root_node, lambda x: x.type == 'pkt', lambda x: x.node_id) ] r = user.api_post(BASE_URL + USER_POINTS_URL, data={'node_ids': '|'.join(pkt_node_ids)}) course_id = root_content['course_edition']['course_id'] for point in r.json(): # We need to add 'name' and 'course_id' attributes to point because API doesn't return them point['name'] = Node.get_node_by_id(root_node, point['node_id']).name point['course_id'] = course_id user_points.add(Points.from_json(point)) return user_points
def get_user_programs(user: User) -> set: """Get all user programs :param user: User that has an active session :returns: Set of unique Program objects representing user programs """ r = user.api_get(BASE_URL + USER_PROGRAMS_URL) programs = set() for program in r.json(): programs.add(Program.from_json(program['programme'])) return programs
def get_active_terms_ids(user: User) -> list: """Gets active term ID :param user: User that has an active session :returns: A list with active term IDs """ r = user.api_post(BASE_URL + USER_COURSES_URL, data={ 'fields': 'terms', 'active_terms_only': 'true' }) return [term['id'] for term in r.json()['terms']]
def get_user_courses(user: User) -> set: """Get all user courses :param user: User that has an active session :returns: Set of unique Course objects representing user courses """ fields = ['course_id', 'course_name', 'term_id'] r = user.api_post(BASE_URL + USER_COURSES_PARTICIPANT_URL, data={ 'fields': '|'.join(fields), 'active_terms': 'true' }) courses = set() for term in get_active_terms_ids(user): for course in r.json()['groups'][term]: courses.add(Course.from_json(course)) return courses
def get_usos_users(user_ids: list = None) -> list: """Get all users from DB and convert to User objects :param user_ids: List of user_ids to get from DB. Omit this parameter if you want to get all users :returns: List of User objects """ connection = DbConnector.get_connection() # Column names columns = [ 'id', 'fb_first_name', 'fb_last_name', 'nickname', 'gender', 'subscriptions', 'usos_first_name', 'usos_last_name', 'usos_id', 'usos_courses', 'usos_token', 'usos_token_secret', 'locale', 'is_registered' ] users = [] cursor = connection.cursor(dictionary=True) if user_ids is None: query = 'select {} from users;'.format(', '.join(columns)) cursor.execute(query) else: query = 'select {} from users where id in ({});'.format( ', '.join(columns), ', '.join(['%s' for i in range(len(user_ids))]) ) cursor.execute(query, user_ids) for user_data in cursor: if user_data['is_registered']: user_data['id_'] = user_data['id'] del user_data['id'] users.append(User(**user_data)) cursor.close() return users
import requests from dotenv import load_dotenv from usos.usos_api_calls import * from usos.objects.user import User from usos.usos_mysql.update_tables import update_usos_courses, update_usos_programs, update_new_usos_points from usos.usos_mysql.user_ops import get_usos_users if __name__ == '__main__': load_dotenv() User.get_usos_api_key() users = get_usos_users() courses = set() programs = set() points = set() for u in users: try: programs.update(get_user_programs(u)) courses.update(get_user_courses(u)) points.update(get_user_points(u)) except requests.exceptions.ConnectionError as err: print(err) print('User programs:') for i in sorted(programs, key=lambda x: x.program_name_pl): i: Program print(i.program_id, i.program_name_pl, sep=' - ') print('\nUser courses:') for i in sorted(courses, key=lambda x: x.course_name_pl):