From 5f7c6ba6d05497ef6a76fb807bacf3b0cfbe765c Mon Sep 17 00:00:00 2001 From: JustAnOkapi Date: Wed, 15 Jun 2022 15:31:36 -0500 Subject: [PATCH] Prettify code --- .vscode/launch.json | 16 +++ driver.py | 252 ++++++++++++++++++++++++++++++-------------- 2 files changed, 189 insertions(+), 79 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..306f58e --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,16 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Python: Current File", + "type": "python", + "request": "launch", + "program": "${file}", + "console": "integratedTerminal", + "justMyCode": true + } + ] +} \ No newline at end of file diff --git a/driver.py b/driver.py index 7a6bfef..2bab1aa 100644 --- a/driver.py +++ b/driver.py @@ -1,10 +1,11 @@ -import requests # make the api request -import json # parse the data -import pandas as pd # graph the data -import time # delay -import subprocess # open local file -from datetime import datetime # compare how long its been +import json # parse the data +import time # delay +from datetime import datetime # compare how long its been + import dearpygui.dearpygui as dpg # create OS gui window +import pandas as pd # graph the data +import requests # make the api request +from playsound import playsound # play sound duhh def request(): @@ -23,10 +24,12 @@ def request(): var_typeid = 21 locations = requests.post( url='https://publicapi.txdpsscheduler.com/api/AvailableLocation', - data=json.dumps({'TypeId': var_typeid, - 'ZipCode': f'{var_zipcode}', - 'CityName': '', - 'PreferredDay': 0}), + data=json.dumps({ + 'TypeId': var_typeid, + 'ZipCode': f'{var_zipcode}', + 'CityName': '', + 'PreferredDay': 0 + }), headers={'Origin': 'https://public.txdpsscheduler.com'}) return json.loads(locations.content.decode('utf-8')) @@ -40,16 +43,17 @@ def parse(locations: list): var_tdratio = dpg.get_value('var_tdratio') name, distance, date, days, score, = [], [], [], [], [] for location in locations: - name. append(location['Name']) + name.append(location['Name']) distance.append(location['Distance']) - date. append(location['NextAvailableDate']) - delta = datetime(int(location['NextAvailableDateYear']), - int(location['NextAvailableDateMonth']), - int(location['NextAvailableDateDay']) - ) - datetime.now() - days. append(delta.days) - score. append(int(100 - ( - delta.days+var_tdratio*int(location['Distance']))/2)) + date.append(location['NextAvailableDate']) + delta = datetime( + int(location['NextAvailableDateYear']), + int(location['NextAvailableDateMonth']), + int(location['NextAvailableDateDay'])) - datetime.now() + days.append(delta.days) + score.append( + int(100 - + (delta.days + var_tdratio * int(location['Distance'])) / 2)) return name, distance, date, days, score @@ -58,8 +62,13 @@ def graph(name: list, distance: list, date: list, days: list, score: list): Takes in the name, distance, date, and score. Returns the sorted pandas dataframe. """ - data = {'Name': name, 'Distance': distance, 'Date': date, - 'Days': days, 'Score': score} + data = { + 'Name': name, + 'Distance': distance, + 'Date': date, + 'Days': days, + 'Score': score + } for i in range(len(days)): dpg.set_item_label(f'location{i}', name[i]) graph_data = dpg.get_value(f'location{i}') @@ -67,7 +76,8 @@ def graph(name: list, distance: list, date: list, days: list, score: list): graph_data[1].append(days[i]) dpg.set_value(f'location{i}', graph_data) dpg.set_axis_limits('y_axis', -10.0, 200.0) - dpg.set_axis_limits('x_axis', graph_data[0][0]-1, graph_data[0][-1]+10) + dpg.set_axis_limits('x_axis', graph_data[0][0] - 1, + graph_data[0][-1] + 10) dataframe = pd.DataFrame(data) dataframe = dataframe.sort_values(by=['Score'], ascending=False) return dataframe @@ -81,27 +91,43 @@ def update(counter, current_score): """ name, distance, date, days, score = parse(request()) if current_score == score: - print_console(False, f'#{str(counter).zfill(6)} | SAME | Best Score: \ -{max(score)} \r') + print_console( + False, + f'#{str(counter).zfill(6)} | SAME | Best Score: {max(score)} \r') counter += 1 return counter, score else: - print_console(True, f'\ -{str(graph(name, distance, date, days, score))}\n\n') - subprocess.Popen(['\ -C:/Program Files (x86)/K-Lite Codec Pack/MPC-HC64/mpc-hc64.exe', - 'sms-alert-4-daniel_simon.mp3', - '/play', '/close', '/minimized']) + print_console(True, + f'{str(graph(name, distance, date, days, score))}\n\n') + play_sound() return counter, score -def open_gui(): +def play_sound(): + """Plays the alert sound. + Loops if the option is on. + """ + if dpg.get_value('var_sound'): + dpg.configure_item('update', show=True) + while not dpg.get_value('var_acknowledged'): + playsound('sms-alert-4-daniel_simon.mp3') + dpg.configure_item('update', show=False) + dpg.set_value('var_acknowledged', False) + else: + playsound('sms-alert-4-daniel_simon.mp3') + + +def setup_gui(): + """Creates, styles, and sets up the gui. + Font, theme, items, and values are created. + """ dpg.create_context() dpg.create_viewport(title='DMVscanner', width=1050, height=900) dpg.setup_dearpygui() - with dpg.font_registry(): - SourceSans = dpg.add_font('SourceSansPro-Regular.ttf', 20) - RobotoMono = dpg.add_font('RobotoMono-VariableFont_wght.ttf', 20) + dpg.add_font_registry(tag='font reg') + SourceSans = dpg.add_font('SourceSansPro-Regular.ttf', + 20, + parent='font reg') with dpg.theme() as global_theme: with dpg.theme_component(dpg.mvAll): dpg.add_theme_style(dpg.mvStyleVar_FrameRounding, 12) @@ -109,69 +135,125 @@ def open_gui(): dpg.add_theme_style(dpg.mvStyleVar_CellPadding, 12, 12) dpg.bind_font(SourceSans) dpg.bind_theme(global_theme) - with dpg.item_handler_registry(tag='widget handler') as handler: + with dpg.item_handler_registry(tag='widget handler'): dpg.add_item_visible_handler(callback=lambda: print_console()) with dpg.value_registry(): - dpg.add_string_value(tag='var_typeid', - default_value='\ -Apply for first time Texas DL/Permit') - dpg.add_bool_value(tag='var_paused', - default_value=True) - dpg.add_int_value(tag='var_zipcode', - default_value=76036) - dpg.add_float_value(tag='var_tdratio', - default_value=1.5) - dpg.add_string_value(tag='var_console', - default_value='') - dpg.add_int_value(tag='var_sleep', - default_value=1) - - with dpg.window(tag='Settings', label='Settings', min_size=(400, 200), - pos=(600, 25)): - dpg.add_checkbox(label='Paused', source='var_paused', + dpg.add_string_value( + tag='var_typeid', + default_value='Apply for first time Texas DL/Permit') + dpg.add_bool_value(tag='var_paused', default_value=True) + dpg.add_int_value(tag='var_zipcode', default_value=76036) + dpg.add_float_value(tag='var_tdratio', default_value=1.5) + dpg.add_string_value(tag='var_console', default_value='') + dpg.add_int_value(tag='var_sleep', default_value=1) + dpg.add_bool_value(tag='var_sound', default_value=False) + dpg.add_bool_value(tag='var_acknowledged', default_value=False) + with dpg.window(label='Update!', + pos=(200, 200), + show=False, + tag='update', + width=200, + height=50): + dpg.add_checkbox(label='Dismiss', source='var_acknowledged') + + +def create_settings_gui(): + """Create window: Settings. + Can pause and change settings + of the update_loop() before its called. + """ + with dpg.window(tag='Settings', + label='Settings', + min_size=(400, 200), + pos=(600, 15)): + dpg.add_checkbox(label='Paused', + source='var_paused', callback=update_loop) dpg.add_text(default_value='What appointment type:') - dpg.add_radio_button(items=['Apply for first time Texas DL/Permit', - 'Change, replace or renew Texas DL/Permit', - 'Class C Road Skills Test'], - indent=20, source='var_typeid') - dpg.add_input_int(label='ZIP Code', source='var_zipcode', step=0, - width=100, tag='zipcode') + dpg.add_radio_button(items=[ + 'Apply for first time Texas DL/Permit', + 'Change, replace or renew Texas DL/Permit', + 'Class C Road Skills Test' + ], + indent=20, + source='var_typeid') + dpg.add_input_int(label='ZIP Code', + source='var_zipcode', + step=0, + width=100, + tag='zipcode') with dpg.tooltip('zipcode'): dpg.add_text('Texas 5 digit ZIP code') dpg.add_slider_float(label='Time/Distance ratio', - min_value=0.0, max_value=3.0, default_value=1.5, - width=200, tag='tdratio', source='var_tdratio') + min_value=0.0, + max_value=3.0, + default_value=1.5, + width=200, + tag='tdratio', + source='var_tdratio') with dpg.tooltip('tdratio'): dpg.add_text('days per mile') - dpg.add_input_int(label='Sleep Time', source='var_sleep', - width=85, tag='sleep') + dpg.add_input_int(label='Sleep Time', + source='var_sleep', + width=85, + tag='sleep') with dpg.tooltip('sleep'): dpg.add_text('How many seconds between each scan') + dpg.add_checkbox(label='Loop Sound', source='var_sound', tag='sound') + with dpg.tooltip('sound'): + dpg.add_text('Loop the sound until acknowledged') + +def create_console_gui(): + """Adds window: Console. + The console gets updated by + print_console(). + Has two boxes to highlight. + """ with dpg.window(tag='Console'): dpg.add_text(source='var_console', tag='console') with dpg.draw_layer(tag='box', show=False): - dpg.draw_rectangle(pmin=(-5, 701), pmax=(550, 726), - fill=(0, 102, 255, 50), rounding=8) - dpg.draw_rectangle(pmin=(490, 683), pmax=(545, 805), - fill=(0, 102, 255, 50), rounding=8) + dpg.draw_rectangle(pmin=(-5, 701), + pmax=(550, 726), + fill=(0, 102, 255, 50), + rounding=8) + dpg.draw_rectangle(pmin=(490, 683), + pmax=(545, 805), + fill=(0, 102, 255, 50), + rounding=8) + RobotoMono = dpg.add_font('RobotoMono-VariableFont_wght.ttf', + 20, + parent='font reg') dpg.bind_item_font('console', RobotoMono) dpg.bind_item_handler_registry('console', 'widget handler') - with dpg.window(tag='Graph', label='Graph', min_size=(400, 200), - pos=(600, 300)): + +def create_graph_gui(): + """Add a window: Graph. + The graph has 5 lines. + """ + with dpg.window(tag='Graph', + label='Graph', + min_size=(400, 200), + pos=(600, 315)): with dpg.plot(height=500, width=-1): dpg.add_plot_legend() - dpg.add_plot_axis(dpg.mvXAxis, tag='x_axis', - label='Runtime', no_gridlines=True) - with dpg.plot_axis(dpg.mvYAxis, tag='y_axis', - label='Days', no_gridlines=False): - dpg.add_stair_series([], [], tag='location0') - dpg.add_stair_series([], [], tag='location1') - dpg.add_stair_series([], [], tag='location2') - dpg.add_stair_series([], [], tag='location3') - dpg.add_stair_series([], [], tag='location4') + dpg.add_plot_axis(dpg.mvXAxis, + tag='x_axis', + label='Runtime', + no_gridlines=True) + with dpg.plot_axis(dpg.mvYAxis, + tag='y_axis', + label='Days', + no_gridlines=False): + for i in range(5): + dpg.add_stair_series([], [], tag=f'location{i}') + + +def open_gui(): + """Runs and opens the gui. + Makes console the primary. + """ dpg.show_viewport() dpg.set_primary_window('Console', True) dpg.start_dearpygui() @@ -179,6 +261,11 @@ def open_gui(): def print_console(change=False, addon=''): + """Adds text to the GUI console. + Takes in change and addon. + If there is no data change it + overwrites the previous line. + """ if change: if dpg.get_value('var_console').count('\n') > 40: var_console = dpg.get_value('var_console').split('\n', 8)[8] @@ -190,12 +277,15 @@ def print_console(change=False, addon=''): elif not change: var_console = dpg.get_value('var_console') cut = var_console[:var_console.rfind('\n')] - dpg.set_value('var_console', - f"""{cut}\n{str(addon)}""") + dpg.set_value('var_console', f"""{cut}\n{str(addon)}""") return addon def update_loop(): + """Loops the update function. + Skips if var_paused is true. + Sleeps for var_sleep seconds + """ counter = 0 current_score = [] while not dpg.get_value('var_paused'): @@ -205,4 +295,8 @@ def update_loop(): if __name__ == '__main__': start_time = datetime.now() + setup_gui() + create_settings_gui() + create_console_gui() + create_graph_gui() open_gui()