def get_login_data(page): """Extract the path to the serverside script and the input form data from the Dualis login page""" form = page.xpath('//form[@id = "cn_loginForm"]')[0] inputs = form.xpath('.//input') header = {i.name: i.value for i in inputs if i.name is not None} header.update({ 'usrname': get_config_val('username'), 'pass': get_config_val('password') }) return form.action, header
async def do_output_io(session, semesters): """Print the data and update the data file""" to_display = get_config_val('semester') if to_display is not None: display_sems = [sem for sem in semesters if sem.number == to_display] else: display_sems = semesters # Load all websites 'concurrently' await asyncio.gather(*[sem.load_results() for sem in display_sems]) if get_config_val('new'): output_sems_format(get_new_res(display_sems)) else: output_sems_format(display_sems) update_data_file(display_sems)
async def get_semesters(session): start = await PageInfo.init(session, get_config_val('url')) login_page = await follow_mrefresh(await follow_mrefresh(start)) # Two redirects main_page = await follow_mrefresh(await login(login_page)) semester = Semester.from_pageinfo(await go_to_semester_page(main_page)) semesters = parse_dropdown_menu(semester) return semesters
def update_data_file(display_sems): sems_d = sems_to_dict(display_sems) old_sems_d = get_old_sems_dict() old_sems_d.update(sems_d) try: with open(get_config_val('data'), 'w') as file: json.dump(list(old_sems_d.values()), file, indent=4) except IOError: pass
def output_sems_format(semesters): """Output semesters in the desired output format""" if get_config_val('json'): # when "--json" is used print(sems_to_json(semesters)) print("if line") #pushbullet integration #if (pushbullet_on ==True): else: # when "--new" or nothing is used sems_pretty_print(semesters)
def get_old_sems_dict(): """Read the data file from disk and cache the result""" self = get_old_sems_dict if getattr(self, 'cache', None) is None: try: with open(get_config_val('data')) as file: data = json.load(file) self.cache = { sem['number']: sem for sem in data } except IOError: self.cache = {} return copy.deepcopy(self.cache)
def output_sems_format(semesters): """Output semesters in the desired output format""" if get_config_val('json'): print(sems_to_json(semesters)) else: sems_pretty_print(semesters)
def pretty_print(self): max_col_width = 35 # Chosen arbitrarily # Necessary headers # Using a dict for set operations because dicts preserve insertion order headers = list({ column: None for result in self.results for column in result.keys() }.keys()) # Get column width termwidth, _ = get_terminal_size() termwidth -= len(headers) - 1 # Subtract spaces between columns col_width = min(termwidth // len(headers), max_col_width) def table_row(columns): return ('\n'.join( map( lambda row: ' '.join([col.ljust(col_width) for col in row]), zip_longest(*[ textwrap.wrap(col, width=col_width) for col in columns ], fillvalue='')))) title_str = Fore.LIGHTBLUE_EX + self.title + Style.RESET_ALL title_str_clean = self.title headers_str = Fore.LIGHTGREEN_EX + table_row(headers) + Style.RESET_ALL headers_str_clean = table_row(headers) res_strings = [] res_strings_clean = [] for result in self.results: res_strings.append( table_row([result.get(column, '') for column in headers])) results_str = '\n'.join(res_strings) for result in self.results: res_strings_clean.append( table_row([result.get(column, '') for column in headers])) results_str_clean = '\n'.join(res_strings_clean) final_res_str = Fore.LIGHTYELLOW_EX + table_row( ['Gesamt:', '', self.final_results]) + Style.RESET_ALL final_res_str_clean = table_row(['Gesamt:', '', self.final_results]) list1 = [] print(title_str) print(headers_str) print(results_str) print(final_res_str) #list1 = [title_str, headers_str, results_str, final_res_str] list1 = [ title_str_clean, headers_str_clean, results_str_clean, final_res_str_clean ] #print("------From List-----") #print('s=%s' % str(list1)) pb = Pushbullet(get_config_val('pushbullet_api_key')) clean_text = title_str_clean + headers_str_clean + results_str_clean + final_res_str_clean anon_text = "Im Fach: " + title_str_clean push = pb.push_note("Neue Noten sind da!", clean_text) # If you manually created a pushbullet channel for your course if usePushbulletChannel == True: my_channel = pb.channels[ channelnum] # by default uses your first channel, for a manual channel use tag from "print(pb.channels)" push = my_channel.push_note("Neue Noten sind da!", anon_text)