def blink(message): print(message, end='\r') sleep(0.3) print(' ' * 120, end='\r') sleep(0.2) print(message, end='\r') sleep(0.1) print(' ' * 120, end='\r') sleep(0.1) print(message)
def printProgressBar(iteration, total, prefix='', suffix='', decimals=1, length=100, fill='█'): percent = ("{0:." + str(decimals) + "f}").format( 100 * (iteration / float(total))) filledLength = int(length * iteration // total) bar = fill * filledLength + '-' * (length - filledLength) print('\r%s |%s| %s%% %s' % (prefix, bar, percent, suffix), end='\r') if iteration == total: print('\r Pages collected ', center=0)
def scrambler(animation_text): chars = '#*@!?$%+&' animation_text = animation_text.split() while True: text = list() for word in animation_text: word = list(word) word[randrange(len(word))] = choice(chars) text.append(''.join(word)) if running: print(' '.join(text), end='\r') sleep(0.05) else: return
def check_updates(): global current_version current_version = tuple(map(int, current_version.split('.'))) animate('Checking updates') releases_url = 'https://github.com/ankit1w/OCD/releases/latest' latest_version = session.head(releases_url, allow_redirects=True, timeout=10).url.split('/tag/v')[1] latest_version = tuple(map(int, latest_version.split('.'))) animate(end=1) if latest_version != current_version: print('Update available! Get the latest version from bit.ly/ocd-update') if latest_version[0] > current_version[0] or latest_version[1] > current_version[1]: print('Press any key to launch site.') getch() system('start https://github.com/ankit1w/OCD/releases') raise SystemExit(1) else: print("Press 'U' to launch update page, any other key to continue.", '\n') update_now = getch() in (b'U', b'u') if update_now: system('start https://github.com/ankit1w/OCD/releases') raise SystemExit(1)
def course_scraper(): global step try: check_updates() step = 1 start_phantomjs() step = 2 login() step = 3 lecture_name = load_handbook() step = 4 lecture_links, new_type = get_lecture_res() return lecture_name, lecture_links, new_type except (NoSuchElementException, requests.exceptions.RequestException, WebDriverException): if driver: driver.quit() animate(end=1) print(f'An error occurred while {error_pos[step]} :(') print('Please check your internet connection speed and stability.') raise SystemExit
def get_lecture_res(): animate('Counting lectures') lecture_links = list() module_navigators = driver.find_element_by_id('div_navigate_session').find_elements_by_tag_name('input') if not module_navigators: raise NoSuchElementException total_lectures = int(module_navigators[-1].get_attribute('value')) lecture_page = driver.find_element_by_id('IFRAME_ID_1').get_attribute('src').split('#')[0] driver.quit() if session.head(lecture_page, timeout=10).status_code != 200: animate(end=1) print('Lecture could not be found on server.') raise SystemExit new_type = "src='Imagepath/" in session.get(lecture_page, timeout=10).text if total_lectures == 1: animate('Lecture loaded!', end=1) lecture_links.append(lecture_page) else: module = 1 animate(f'{total_lectures} lectures have been found.', end=1) print() for i in range(1, total_lectures + 1): printProgressBar(i, total_lectures, prefix=' Discovering pages', suffix='Complete', length=50) lecture_page = lecture_page.replace(f'_L{i - 1}', f'_L{i}') if session.head(lecture_page, timeout=10).status_code == 404: lecture_page = lecture_page.replace(f'_M{module}', f'_M{module + 1}') module += 1 lecture_links.append(lecture_page) return lecture_links, new_type
def print_to_pdf(lecture_links, lecture_name, new_type): try: print('\n', 'Printing gathered pages to PDF...') print() common_args = '-L 0mm -R 0mm -T 0mm -B 0mm --image-quality 100 -n --load-media-error-handling abort ' if new_type: cli_args = common_args + ' '.join(lecture_links) print_error = system( fr'{work_dir}\wkhtmltopdf.exe {cli_args} "{temp_pdf}"') else: cli_args = common_args + '--page-width 350mm --page-height 10000mm -d 600 --zoom 2 ' + ' '.join( lecture_links) print_error = system( fr'{work_dir}\wkhtmltopdf.exe {cli_args} "{temp_pdf}_uncropped"' ) print() if path.exists(f'{temp_pdf}_uncropped'): animate('Adjusting margins') from pdfCropMargins_mod.main_pdfCropMargins import main_crop main_crop() remove(f'{temp_pdf}_uncropped') animate('Margins adjusted!', end=1) else: raise ConnectionError if path.exists(f'{temp_pdf}'): move_fail = system( f'move "{temp_pdf}" "{lecture_name}.pdf">nul 2>&1') if move_fail: raise MoveFailed else: raise ConnectionError return print_error except ConnectionError: animate(end=1) print('Connection Error :(') raise SystemExit except MoveFailed: print( '\n', 'Could not attain permissions to create PDF in the current location.' ) print( "Make sure an already existing PDF isn't open in a running program," " or administrative rights are not required.", '\n') raise SystemExit
def load_handbook(): animate('Gathering subjects') driver.execute_script("LoadPage('frmSubjectList.aspx',0)") timeout = 0 while True: subject_list = driver.find_element_by_id('ul_subject_menu').find_elements_by_tag_name('input') if subject_list[0].get_attribute('onclick'): break if timeout == 10: raise NoSuchElementException system('timeout 1 > nul') timeout += 1 js_functions = tuple(map(lambda x: x.get_attribute('onclick').split(';')[0], subject_list)) if not len(js_functions): raise NoSuchElementException animate('Subjects loaded!', end=1) print() for index, command in enumerate(js_functions): sub_name = command.split(',')[1][1:-2].split(' - ', 1) sub_name = (sub_name[0] + ' ').ljust(20, '─') + ' ' + sub_name[1] print(f'{str(index + 1).rjust(15)}. {sub_name}', center=0, color=0) while True: print() try: cmd_no = int(input(f'Enter subject number [1-{len(js_functions)}] : '.rjust(60))) - 1 if cmd_no not in range(0, len(js_functions)): raise ValueError break except ValueError: print('Wrong input! Enter again.' + ' ' * 10, '\n') driver.execute_script(js_functions[cmd_no]) lecture_name = js_functions[cmd_no].split(',', 1)[1][1:-2] system('cls') system(f'title Online Courseware Downloader : Downloading ↓ {lecture_name}'.replace('&', '^&')) print('Online Courseware Downloader', 'github.com/ankit1w/OCD', '─' * 125, sep='\n', color=0) blink(f"Loading...{lecture_name}") print() return lecture_name
from cleanup import cleanup from course_scraper import course_scraper from path_vars import work_dir, phantomjs_path from print_pdf import print_to_pdf try: t = Thread(target=system, args=(fr'{work_dir}\disable_quick_edit.bat 2 >nul',)) t.start() system('cls') dummy = ''.join(choices(ascii_letters + digits, k=10)) dummy_error = system(f'2>nul ( >{dummy} type nul)') if dummy_error: print('Could not attain permissions to create PDF in the current location.', 'Make sure the folder is writable without administrative access.', 'If not, run the program as administrator.', sep='\n') raise SystemExit system(f'del {dummy}') system('mode con cols=125 lines=30') system('powershell -command "&{$H=get-host;$W=$H.ui.rawui;$B=$W.buffersize;' '$B.width=125;$B.height=450;$W.buffersize=$B;}">nul') if phantomjs_path != '.': try: copyfile(fr'{work_dir}\phantomjs.exe', fr'{gettempdir()}\phantomjs.exe') except PermissionError: pass