Skip to content

Commit

Permalink
Merge pull request #20 from athuler/0.2.1
Browse files Browse the repository at this point in the history
0.2.1
  • Loading branch information
athuler authored Aug 18, 2024
2 parents e0f5e5d + 336fe95 commit 49dc2e7
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 47 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
# Changelog


## 0.2.1 (2024-08-18)

### Added

- Testing for every transportation system
- `Vehicle` object
- `TransportationSystem.getVehicles()` which returns a list of `Vehicle` objects

### Changed

- Fixed `AttributeError: 'list' object has no attribute 'items'` error thrown in `TransportationSystem.getRoutes()`

### Removed


## 0.2.0 (2024-07-31)

### Added
Expand Down
69 changes: 69 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,33 @@ passiogo.getSystemFromID(1068).getSystemAlerts()
<passiogo.SystemAlert at 0x1d62dbe0490>]
```


### `TransportationSystem.getVehicles()`

Gets all alerts for the corresponding transportation system.

**Inputs**:

- **appVersion** (*int*): Version of the application (Default: 1)


**Output**: *List* of [`Vehicle`](#vehicle)

```python
passiogo.getSystemFromID(1068).getVehicles()
```

```
[<passiogo.Vehicle at 0x21b59af8d00>,
<passiogo.Vehicle at 0x21b59af8f70>,
<passiogo.Vehicle at 0x21b59af8d90>,
<passiogo.Vehicle at 0x21b59af8370>,
...
<passiogo.Vehicle at 0x21b597bfd90>,
<passiogo.Vehicle at 0x21b597bfbe0>,
<passiogo.Vehicle at 0x21b597bff10>]
```

## `Route`

### `Route.__init__()`
Expand Down Expand Up @@ -343,6 +370,48 @@ passiogo.getSystemFromID(1068).getRoutes()[0].getStops()
'toOk': '1'}
```

## `Vehicle`

### `Vehicle.__init__()`

All attributes default to **None**.

- **id** (*str*): ID of the vehicle
- **name** (*str*): Name of the vehicle
- **type** (*str*): Type of the vehicle
- **system** (*[`TransportationSystem`](#transportationsystem)*): Type of the vehicle
- **calculatedCourse** (*int*): Unknown
- **routeId** (*str*): ID of the route the vehicle is part of
- **routeName** (*str*): Name of the route the vehicle is part of
- **color** (*str*): Color in which the vehicle/route is displayed
- **created** (*str*): When the vehicle was created
- **latitude** (*float*): Current latitude of the vehicle
- **longitude** (*float*): Current longitude of the vehicle
- **speed** (*float*): Current speed of the vehicle
- **paxLoad** (*float*): Current number of passengers on board
- **outOfService** (*bool*): Whether the vehicle is currently in service
- **more** (*str*): Unknown
- **tripId** (*str*): Unknown


```python
{'id': '7998',
'name': '7998 (CTA)',
'type': 'bus',
'system': <passiogo.TransportationSystem at 0x21b59b13460>,
'calculatedCourse': '0',
'routeId': 'cta4',
'routeName': 'Cottage Grove (CTA) (CTA)',
'color': '#565a5c',
'created': '20240818 11:13',
'longitude': 41.6861457824707,
'speed': None,
'paxLoad': 0,
'outOfService': None,
'more': None,
'tripId': None}
```

## `printAllSystemsMd()`

Prints all system names as a markdown list.
Expand Down
150 changes: 115 additions & 35 deletions passiogo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,13 @@ def getRoutes(
if(routes == None):
return(None)


# Handle Differing Response Format
if "all" in routes:
routes = routes["all"]

allRoutes = []
for digit, route in routes.items():
for route in routes:
possibleKeys = ["id", "groupId", "groupColor", "name", "shortName", "nameOrig", "fullname", "myid", "mapApp", "archive", "goPrefixRouteName", "goShowSchedule", "outdated", "distance", "latitude", "longitude", "timezone", "serviceTime", "serviceTimeShort"]

for possibleKey in possibleKeys:
Expand Down Expand Up @@ -212,12 +213,22 @@ def getStops(
}
stops = sendApiRequest(url, body)

# Return Raw Response
if raw:
return(stops)

# Handle Request Error
if(stops == None):
return(None)

if raw:
return(stops)
# Handle Empty Routes
if stops["routes"] == []:
stops["routes"] = {}

# Handle Empty Stops
if stops["stops"] == []:
stops["stops"] = {}


# Create Route & Stops Dictionary
# {routeid -> [stopid, stopid]}
Expand Down Expand Up @@ -327,6 +338,65 @@ def getSystemAlerts(

return(allAlerts)

def getVehicles(
self,
appVersion = 2
) -> list["Vehicle"]:
"""
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(appVersion)
body = {
"s0" : str(self.id),
"sA" : 1
}
vehicles = sendApiRequest(url, body)

# Handle Request Error
if(vehicles == None):
return(None)

allVehicles = []
for vehicleId, vehicle in vehicles["buses"].items():
if vehicleId == '-1':
continue

vehicle = vehicle[0]

for key in ["busId", "busName", "busType", "calculatedCourse", "routeId", "route", "color", "created", "latitude", "longitude", "speed", "paxLoad100", "outOfService", "more", "tripId"]:
if key not in vehicle:
vehicle[key] = None


allVehicles.append(Vehicle(
id = vehicle["busId"],
name = vehicle["busName"],
type = vehicle["busType"],
system = self,
calculatedCourse = vehicle["calculatedCourse"],
routeId = vehicle["routeId"],
routeName = vehicle["route"],
color = vehicle["color"],
created = vehicle["created"],
latitude = vehicle["latitude"],
longitude = vehicle["longitude"],
speed = vehicle["speed"],
paxLoad = vehicle["paxLoad100"],
outOfService = vehicle["outOfService"],
more = vehicle["more"],
tripId = vehicle["tripId"],
))

return(allVehicles)


def getSystems(
appVersion = 2,
Expand Down Expand Up @@ -483,6 +553,7 @@ def getStops(self):

return(stopsForRoute)


### Stops ###

class Stop:
Expand Down Expand Up @@ -510,7 +581,6 @@ def __init__(
self.radius = radius
self.system = system



### System Alerts ###

Expand Down Expand Up @@ -577,38 +647,49 @@ def __init__(
self.fromF = fromF
self.fromOk = fromOk
self.toOk = toOk




### Vehicles ###

class Vehicle:

def __init__(
self,
id: str = None,
name: str = None,
type: str = None,
system: TransportationSystem = None,
calculatedCourse: int = None,
routeId: str = None,
routeName: str = None,
color: str = None,
created: str = None,
latitude: float = None,
longitude: float = None,
speed: float = None,
paxLoad: float = None,
outOfService: bool = None,
more: str = None,
tripId: str = None,
):
self.id = id
self.name = name
self.type = type
self.system = system
self.calculatedCourse = calculatedCourse
self.routeId = routeId
self.routeName = routeName
self.color = color
self.created = created
self.longitude = latitude
self.speed = speed
self.paxLoad = paxLoad
self.outOfService = outOfService
self.more = more
self.tripId = tripId


def getBuses(
systemSelected,
paramDigit = 2
):
"""
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(systemSelected),
"sA" : 1
}
buses = sendApiRequest(url, body)

# Handle Request Error
if(buses == None):
return(None)

return(buses)


### Live Timings ###
Expand All @@ -634,17 +715,16 @@ def launchWS():


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
userId
):

subscriptionMsg = {
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setup(
name='PassioGo',
version="0.2.0",
version="0.2.1",
description="An unofficial API for Passio Go",
long_description=long_description,
long_description_content_type='text/markdown',
Expand Down
41 changes: 30 additions & 11 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,44 @@
import passiogo
import pytest

def pytest_namespace():
return {'allSystems': None}

def test_getAllSystems():
passiogo.getSystems

global allSystems
allSystems = passiogo.getSystems()
pytest.allSystems = allSystems
assert True

test_getAllSystems()
ids = [f"{element.name} (#{element.id})" for element in pytest.allSystems]

def test_printAllSystems():
passiogo.printAllSystemsMd()
assert True


def test_getSystemFromId():
global testSystem
testSystem = passiogo.getSystemFromID(1068)
assert True


@pytest.mark.parametrize("system", pytest.allSystems, ids=ids)
def test_getAllRoutes(system):
system.getRoutes()


@pytest.mark.parametrize("system", pytest.allSystems, ids=ids)
def test_getAllStops(system):
system.getStops()

def test_getAllRoutes():
testSystem.getRoutes()

def test_getAllStops():
testSystem.getStops()

def test_getSystemAlerts():
testSystem.getSystemAlerts()
@pytest.mark.parametrize("system", pytest.allSystems, ids=ids)
def test_getSystemAlerts(system):
system.getSystemAlerts()

def test_getBuses():
passiogo.getBuses(1068)
@pytest.mark.parametrize("system", pytest.allSystems, ids=ids)
def test_getVehicles(system):
system.getVehicles()

0 comments on commit 49dc2e7

Please sign in to comment.