From f5fe2b4537149b2e87d76d77ee35d59ecd329ab5 Mon Sep 17 00:00:00 2001 From: athuler <22741115+athuler@users.noreply.github.com> Date: Fri, 12 Jul 2024 10:07:10 -0400 Subject: [PATCH] Initial Commit --- .gitignore | 3 + passiogo/__init__.py | 212 +++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 2 + setup.py | 22 +++++ tests/__init__.py | 1 + tests/test_main.py | 13 +++ 6 files changed, 253 insertions(+) create mode 100644 .gitignore create mode 100644 passiogo/__init__.py create mode 100644 requirements.txt create mode 100644 setup.py create mode 100644 tests/__init__.py create mode 100644 tests/test_main.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9efe358 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ + +/passiogo/__pycache__ +/tests/__pycache__ diff --git a/passiogo/__init__.py b/passiogo/__init__.py new file mode 100644 index 0000000..fbe6b00 --- /dev/null +++ b/passiogo/__init__.py @@ -0,0 +1,212 @@ +import requests +import websocket +import json + +__version__ = "0.1.0" +BASE_URL = "https://passiogo.com" + + +def sendApiRequest(url, body): + + # Send Request + response = requests.post(url, json = body) + + + # Handle JSON Response + response = response.json() + + + # Handle API Error + if( + "error" in response and + response["error"] != "" + ): + raise Exception(f"Error in Response! Here is the received response: {response}") + + return(response) + + +def getAllRoutes( + systemSelected = 1068, + paramDigit = 1, + amount = 1, + debug = 0 +): + """ + Obtains every route for the selected system. + ========= + systemSelected: system from which to get content + paramDigit: does not affect content of response, only formatting + amount: + 1: Returns all routes for given system + 0: Not Valid, Gives Error + >=2: Returns all routes for given system in addition to unrelated routes. Exact methodology unsure. + """ + + + # Initialize & Send Request + url = BASE_URL+"/mapGetData.php?getRoutes="+str(paramDigit) + body = { + "systemSelected0" : str(systemSelected), + "amount" : amount + } + routes = sendApiRequest(url, body) + + # Handle Request Error + if(routes == None): + return(None) + + # Handle Differing Response Format + if "all" in routes: + routes = routes["all"] + + + return(routes) + + +def getAllStops( + paramDigit = 2, + systemSelected = 1068, + sA = 1, + debug = 0 +): + """ + Obtains all stop for the selected system. + ========= + systemSelected: system from which to get content + paramDigit: No discernable change + sA: + 0: error + 1: Returns all stops for the given system + >=2: Returns unrelated stops as well + """ + + + # Initialize & Send Request + url = BASE_URL+"/mapGetData.php?getStops="+str(paramDigit) + body = { + "s0" : str(systemSelected), + "sA" : sA + } + stops = sendApiRequest(url, body) + + # Handle Request Error + if(stops == None): + return(None) + + return(stops) + + +def getSystemAlerts( + paramDigit = 1, + systemSelected = 1068, + debug = 0 +): + """ + Gets all system alerts for the selected system. + ========= + systemSelected: system from which to get content + paramDigit: + 0: Error + >=1: Valid + """ + + + # Initialize & Send Request + url = BASE_URL+"/goServices.php?getAlertMessages="+str(paramDigit) + body = { + "systemSelected0" : str(systemSelected), + "amount" : 1, + "routesAmount":0 + } + errorMsg = sendApiRequest(url, body) + + + # Handle Request Error + if(errorMsg == None): + return(None) + + + return(errorMsg) + + +def getBuses( + paramDigit = 2, + s0 = 1068, + debug = 0 +): + """ + Gets all currently running buses. + ========= + s0: system from which to get content + paramDigit: + 0: Error + >=1: Valid + """ + + + # Initialize & Send Request + url = BASE_URL+"/mapGetData.php?getBuses="+str(paramDigit) + body = { + "s0" : str(s0), + "sA" : 1 + } + buses = sendApiRequest(url, body) + + # Handle Request Error + if(buses == None): + return(None) + + return(buses) + + + +# Launch WebSocket +def launchWS(): + uri = "wss://passio3.com/" + + + websocket.enableTrace(False) # For Debugging + wsapp = websocket.WebSocketApp( + uri, + on_open = subscribeWS, + #on_message = ..., + on_error = handleWsError, + on_close = handleWsClose + ) + wsapp.run_forever( + ping_interval = 5, + ping_timeout = 3, + ) + + +def handleWsError(wsapp, error): + vars.errors.append(f"->WebSocketError: {error}") + +def handleWsClose(wsapp, close_status_code, close_msg): + wsapp.close() + vars.logs.append("Closing WebSocket") + + +def subscribeWS( + wsapp, + userId = 1068 +): + + subscriptionMsg = { + "subscribe":"location", + "userId":[userId], + "field":[ + "busId", + "latitude", + "longitude", + "course", + "paxLoad", + "more" + ] + } + wsapp.send(json.dumps(subscriptionMsg)) + + + + \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..6bbe197 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +requests>=2.22.0 +websocket-client>=1.2.0 \ No newline at end of file diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..9c80838 --- /dev/null +++ b/setup.py @@ -0,0 +1,22 @@ +from setuptools import setup, find_packages +from passiogo import __version__ + +with open("README.md", 'r') as f: + long_description = f.read() + +with open("requirements.txt", "r") as fh: + requires = [line for line in fh.read().splitlines() if line != ""] + +setup( + name='PassioGo', + version=__version__, + description="Tracking the Performance of UChicago's Shuttle Network", + long_description=long_description, + author='Andrei Thuler', + author_email='info@andreithuler.com', + url="https://github.com/athuler/PassioGo", + packages=find_packages(), + py_modules=find_packages(), + install_requires=requires, + +) \ No newline at end of file diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/tests/__init__.py @@ -0,0 +1 @@ + diff --git a/tests/test_main.py b/tests/test_main.py new file mode 100644 index 0000000..f9dfbdc --- /dev/null +++ b/tests/test_main.py @@ -0,0 +1,13 @@ +import passiogo + +def test_getAllRoutes(): + passiogo.getAllRoutes() + +def test_getAllStops(): + passiogo.getAllStops() + +def test_getSystemAlerts(): + passiogo.getSystemAlerts() + +def test_getBuses(): + passiogo.getBuses() \ No newline at end of file