Created
April 26, 2022 01:13
-
-
Save KenoLeon/7e58829814415ff28b46ebc26cb34266 to your computer and use it in GitHub Desktop.
WIFI Scanner Integration V1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import PySimpleGUI as sg | |
| import subprocess | |
| import re | |
| _VARS = {'Networks': [], | |
| 'NetworkStrength': []} | |
| EDGE_OFFSET = 8 # offset from the left edge for first bar | |
| GRAPH_SIZE = DATA_SIZE = (600, 600) # size in pixels | |
| GLOBAL_FONT = ('Helvetica', 14) | |
| # SCANNER DATA : | |
| def getNetworks(): | |
| process = subprocess.run(["airport", "en0", "--scan"], | |
| check=True, | |
| stdout=subprocess.PIPE, | |
| universal_newlines=True) | |
| processRows = process.stdout.split("\n") | |
| # clear global network Lists | |
| _VARS['Networks'].clear() | |
| _VARS['NetworkStrength'].clear() | |
| # Parse by 2 or more spaces and build dicts: | |
| for item in processRows: | |
| splitData = re.split(r'\s{2,}', item) | |
| if len(splitData) > 1: | |
| _VARS['Networks'].append(splitData[1]) | |
| _VARS['NetworkStrength'].append(splitData[2]) | |
| # Remove first item, which is the header | |
| _VARS['Networks'].pop(0) | |
| _VARS['NetworkStrength'].pop(0) | |
| # GUI | |
| sg.theme('DarkAmber') | |
| layout = [[sg.Text('WIFI SCANNER', font=GLOBAL_FONT)], | |
| [sg.Graph(GRAPH_SIZE, (0, 0), DATA_SIZE, k='-GRAPH-')], | |
| [sg.Button('SCAN FOR WIFI NETWORKS', font=GLOBAL_FONT), | |
| sg.Exit(font=GLOBAL_FONT)]] | |
| window = sg.Window('Bar Graph', layout, finalize=True) | |
| graph = window['-GRAPH-'] # type: sg.Graph | |
| # GUI | |
| while True: | |
| graph.erase() | |
| getNetworks() | |
| print('Getting Networks') | |
| numNetworks = len(_VARS['Networks']) | |
| bar_width = (GRAPH_SIZE[0]/numNetworks)-EDGE_OFFSET | |
| bar_spacing = (GRAPH_SIZE[0]/numNetworks) | |
| for i in range(numNetworks): | |
| graph_value = -int(_VARS['NetworkStrength'][i]) | |
| networkName = _VARS['Networks'][i] | |
| graph.draw_rectangle(top_left=(i * bar_spacing + EDGE_OFFSET, | |
| graph_value), | |
| bottom_right=(i * bar_spacing + | |
| EDGE_OFFSET + bar_width, 0), | |
| fill_color='lightgreen') | |
| graph.draw_text(text=networkName, location=( | |
| i*bar_spacing+EDGE_OFFSET+100, graph_value+10), | |
| font=GLOBAL_FONT, color='white') | |
| # Normally at the top of the loop, | |
| # but because we're drawing the graph first, making it at the bottom | |
| event, values = window.read() | |
| if event in (sg.WIN_CLOSED, 'Exit'): | |
| break | |
| window.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment