ltasg 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ltasg-0.0.1/.gitignore +162 -0
- ltasg-0.0.1/LICENSE +21 -0
- ltasg-0.0.1/PKG-INFO +15 -0
- ltasg-0.0.1/README.md +1 -0
- ltasg-0.0.1/pyproject.toml +23 -0
- ltasg-0.0.1/requirements.txt +10 -0
- ltasg-0.0.1/src/ltasg/__init__.py +5 -0
- ltasg-0.0.1/src/ltasg/api.py +29 -0
- ltasg-0.0.1/src/ltasg/constants.py +36 -0
- ltasg-0.0.1/src/ltasg/traffic.py +2 -0
- ltasg-0.0.1/src/ltasg/transport/__init__.py +0 -0
- ltasg-0.0.1/src/ltasg/transport/bus.py +30 -0
- ltasg-0.0.1/src/ltasg/transport/taxi.py +16 -0
- ltasg-0.0.1/src/ltasg/transport/train.py +19 -0
- ltasg-0.0.1/src/ltasg/transport/transport.py +9 -0
- ltasg-0.0.1/test/__init__.py +0 -0
- ltasg-0.0.1/test/test.py +79 -0
ltasg-0.0.1/.gitignore
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
# Vscode settings
|
|
2
|
+
.vscode
|
|
3
|
+
# Byte-compiled / optimized / DLL files
|
|
4
|
+
__pycache__/
|
|
5
|
+
*.py[cod]
|
|
6
|
+
*$py.class
|
|
7
|
+
|
|
8
|
+
# C extensions
|
|
9
|
+
*.so
|
|
10
|
+
|
|
11
|
+
# Distribution / packaging
|
|
12
|
+
.Python
|
|
13
|
+
build/
|
|
14
|
+
develop-eggs/
|
|
15
|
+
dist/
|
|
16
|
+
downloads/
|
|
17
|
+
eggs/
|
|
18
|
+
.eggs/
|
|
19
|
+
lib/
|
|
20
|
+
lib64/
|
|
21
|
+
parts/
|
|
22
|
+
sdist/
|
|
23
|
+
var/
|
|
24
|
+
wheels/
|
|
25
|
+
share/python-wheels/
|
|
26
|
+
*.egg-info/
|
|
27
|
+
.installed.cfg
|
|
28
|
+
*.egg
|
|
29
|
+
MANIFEST
|
|
30
|
+
|
|
31
|
+
# PyInstaller
|
|
32
|
+
# Usually these files are written by a python script from a template
|
|
33
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
34
|
+
*.manifest
|
|
35
|
+
*.spec
|
|
36
|
+
|
|
37
|
+
# Installer logs
|
|
38
|
+
pip-log.txt
|
|
39
|
+
pip-delete-this-directory.txt
|
|
40
|
+
|
|
41
|
+
# Unit test / coverage reports
|
|
42
|
+
htmlcov/
|
|
43
|
+
.tox/
|
|
44
|
+
.nox/
|
|
45
|
+
.coverage
|
|
46
|
+
.coverage.*
|
|
47
|
+
.cache
|
|
48
|
+
nosetests.xml
|
|
49
|
+
coverage.xml
|
|
50
|
+
*.cover
|
|
51
|
+
*.py,cover
|
|
52
|
+
.hypothesis/
|
|
53
|
+
.pytest_cache/
|
|
54
|
+
cover/
|
|
55
|
+
|
|
56
|
+
# Translations
|
|
57
|
+
*.mo
|
|
58
|
+
*.pot
|
|
59
|
+
|
|
60
|
+
# Django stuff:
|
|
61
|
+
*.log
|
|
62
|
+
local_settings.py
|
|
63
|
+
db.sqlite3
|
|
64
|
+
db.sqlite3-journal
|
|
65
|
+
|
|
66
|
+
# Flask stuff:
|
|
67
|
+
instance/
|
|
68
|
+
.webassets-cache
|
|
69
|
+
|
|
70
|
+
# Scrapy stuff:
|
|
71
|
+
.scrapy
|
|
72
|
+
|
|
73
|
+
# Sphinx documentation
|
|
74
|
+
docs/_build/
|
|
75
|
+
|
|
76
|
+
# PyBuilder
|
|
77
|
+
.pybuilder/
|
|
78
|
+
target/
|
|
79
|
+
|
|
80
|
+
# Jupyter Notebook
|
|
81
|
+
.ipynb_checkpoints
|
|
82
|
+
|
|
83
|
+
# IPython
|
|
84
|
+
profile_default/
|
|
85
|
+
ipython_config.py
|
|
86
|
+
|
|
87
|
+
# pyenv
|
|
88
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
89
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
90
|
+
# .python-version
|
|
91
|
+
|
|
92
|
+
# pipenv
|
|
93
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
94
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
95
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
96
|
+
# install all needed dependencies.
|
|
97
|
+
#Pipfile.lock
|
|
98
|
+
|
|
99
|
+
# poetry
|
|
100
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
101
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
102
|
+
# commonly ignored for libraries.
|
|
103
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
104
|
+
#poetry.lock
|
|
105
|
+
|
|
106
|
+
# pdm
|
|
107
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
108
|
+
#pdm.lock
|
|
109
|
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
|
110
|
+
# in version control.
|
|
111
|
+
# https://pdm.fming.dev/#use-with-ide
|
|
112
|
+
.pdm.toml
|
|
113
|
+
|
|
114
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
115
|
+
__pypackages__/
|
|
116
|
+
|
|
117
|
+
# Celery stuff
|
|
118
|
+
celerybeat-schedule
|
|
119
|
+
celerybeat.pid
|
|
120
|
+
|
|
121
|
+
# SageMath parsed files
|
|
122
|
+
*.sage.py
|
|
123
|
+
|
|
124
|
+
# Environments
|
|
125
|
+
.env
|
|
126
|
+
.venv
|
|
127
|
+
env/
|
|
128
|
+
venv/
|
|
129
|
+
ENV/
|
|
130
|
+
env.bak/
|
|
131
|
+
venv.bak/
|
|
132
|
+
|
|
133
|
+
# Spyder project settings
|
|
134
|
+
.spyderproject
|
|
135
|
+
.spyproject
|
|
136
|
+
|
|
137
|
+
# Rope project settings
|
|
138
|
+
.ropeproject
|
|
139
|
+
|
|
140
|
+
# mkdocs documentation
|
|
141
|
+
/site
|
|
142
|
+
|
|
143
|
+
# mypy
|
|
144
|
+
.mypy_cache/
|
|
145
|
+
.dmypy.json
|
|
146
|
+
dmypy.json
|
|
147
|
+
|
|
148
|
+
# Pyre type checker
|
|
149
|
+
.pyre/
|
|
150
|
+
|
|
151
|
+
# pytype static type analyzer
|
|
152
|
+
.pytype/
|
|
153
|
+
|
|
154
|
+
# Cython debug symbols
|
|
155
|
+
cython_debug/
|
|
156
|
+
|
|
157
|
+
# PyCharm
|
|
158
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
159
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
160
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
161
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
162
|
+
#.idea/
|
ltasg-0.0.1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Ashe
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
ltasg-0.0.1/PKG-INFO
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: ltasg
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: A LTA Datamall API Wrapper Library for Python
|
|
5
|
+
Project-URL: Homepage, https://github.com/ashe/ltasg
|
|
6
|
+
Project-URL: Issues, https://github.com/ashe/ltasg/issues
|
|
7
|
+
Author-email: Aw Sheng Xiang <awshengxiang@hotmail.com>
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Requires-Python: >=3.8
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
|
|
15
|
+
# ltasg
|
ltasg-0.0.1/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# ltasg
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
[project]
|
|
7
|
+
name = "ltasg"
|
|
8
|
+
version = "0.0.1"
|
|
9
|
+
authors = [
|
|
10
|
+
{ name="Aw Sheng Xiang", email="awshengxiang@hotmail.com" },
|
|
11
|
+
]
|
|
12
|
+
description = "A LTA Datamall API Wrapper Library for Python"
|
|
13
|
+
readme = "README.md"
|
|
14
|
+
requires-python = ">=3.8"
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Programming Language :: Python :: 3",
|
|
17
|
+
"License :: OSI Approved :: MIT License",
|
|
18
|
+
"Operating System :: OS Independent",
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
[project.urls]
|
|
22
|
+
Homepage = "https://github.com/ashe/ltasg"
|
|
23
|
+
Issues = "https://github.com/ashe/ltasg/issues"
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
from aiohttp import ClientSession, ClientConnectionError
|
|
2
|
+
import traceback
|
|
3
|
+
class LTADataMall:
|
|
4
|
+
def __init__(self, api_key) -> None:
|
|
5
|
+
self.BASE_URL = "http://datamall2.mytransport.sg/ltaodataservice/"
|
|
6
|
+
self.API_KEY = api_key
|
|
7
|
+
self.HEADERS = {
|
|
8
|
+
"Accept": "application/json",
|
|
9
|
+
"AccountKey": self.API_KEY
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
async def fetch(self, service_endpoint: str, query_params: dict=None, data:dict=None):
|
|
13
|
+
url = f"{self.BASE_URL}{service_endpoint}"
|
|
14
|
+
if query_params:
|
|
15
|
+
url += "?"
|
|
16
|
+
for param, value in query_params.items():
|
|
17
|
+
url += f"{param}={value}"
|
|
18
|
+
if param != list(query_params.keys())[-1]:
|
|
19
|
+
url += "&"
|
|
20
|
+
async with ClientSession() as session:
|
|
21
|
+
try:
|
|
22
|
+
async with session.get(url, headers=self.HEADERS) as response:
|
|
23
|
+
if response.status == 200:
|
|
24
|
+
return await response.json()
|
|
25
|
+
else:
|
|
26
|
+
raise Exception("Error calling api for {} with status code: {}".format(
|
|
27
|
+
service_endpoint, response.status)) from None
|
|
28
|
+
except ClientConnectionError as e:
|
|
29
|
+
raise e
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
from enum import Enum
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class TransportType(Enum):
|
|
5
|
+
BUS = "bus"
|
|
6
|
+
TRAIN = "mrt"
|
|
7
|
+
TAXI = "taxi"
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
LTA_SERVICES = {
|
|
11
|
+
"PUBLIC_TRANSPORT_SERVICES": {
|
|
12
|
+
TransportType.BUS: {
|
|
13
|
+
"ARRIVAL": "BusArrivalv2",
|
|
14
|
+
"SERVICES": "BusServices",
|
|
15
|
+
"ROUTES": "BusRoutes",
|
|
16
|
+
"STOPS": "BusStops",
|
|
17
|
+
# "PASSENGER_VOLUME": {
|
|
18
|
+
# "BUS_STOPS": "PV/Bus",
|
|
19
|
+
# "ORIGIN_TO_DEST": "PV/ODBus"
|
|
20
|
+
# }
|
|
21
|
+
},
|
|
22
|
+
TransportType.TRAIN: {
|
|
23
|
+
# "PASSENGER_VOLUME": {
|
|
24
|
+
# "ORIGIN_TO_DEST": "PV/ODTrain",
|
|
25
|
+
# },
|
|
26
|
+
"PLATFORM_CROWD_DENSITY": {
|
|
27
|
+
"REALTIME": "PCDRealTime",
|
|
28
|
+
"FORECAST": "PCDForecast"
|
|
29
|
+
},
|
|
30
|
+
"SERVICE_ALERTS": "TrainServiceAlerts"
|
|
31
|
+
},
|
|
32
|
+
TransportType.TAXI: {
|
|
33
|
+
"AVAILABILITY": "Taxi-Availability",
|
|
34
|
+
"STANDS": "TaxiStands",
|
|
35
|
+
}
|
|
36
|
+
}}
|
|
File without changes
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
from api import LTADataMall
|
|
2
|
+
from transport.transport import Transport
|
|
3
|
+
from constants import TransportType
|
|
4
|
+
|
|
5
|
+
class Bus(Transport):
|
|
6
|
+
def __init__(self, api_key):
|
|
7
|
+
super().__init__(api_key)
|
|
8
|
+
self.bus_services = self.transport_services[TransportType.BUS]
|
|
9
|
+
|
|
10
|
+
async def arrival(self, bus_stop_code: int, bus_no: int):
|
|
11
|
+
data = await self.lta_api.fetch(self.bus_services['ARRIVAL'], query_params={
|
|
12
|
+
"BusStopCode": bus_stop_code,
|
|
13
|
+
"ServiceNo": bus_no
|
|
14
|
+
})
|
|
15
|
+
return data["Services"]
|
|
16
|
+
|
|
17
|
+
async def services(self):
|
|
18
|
+
data = await self.lta_api.fetch(self.bus_services['SERVICES'])
|
|
19
|
+
return data["value"]
|
|
20
|
+
|
|
21
|
+
async def routes(self):
|
|
22
|
+
data = await self.lta_api.fetch(self.bus_services['ROUTES'])
|
|
23
|
+
return data["value"]
|
|
24
|
+
|
|
25
|
+
async def stops(self):
|
|
26
|
+
data = await self.lta_api.fetch(self.bus_services['STOPS'])
|
|
27
|
+
return data["value"]
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
# BusArrivalv2?BusStopCode=71111&ServiceNo=137
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
from transport.transport import Transport
|
|
2
|
+
from constants import TransportType
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class Taxi(Transport):
|
|
6
|
+
def __init__(self, api_key) -> None:
|
|
7
|
+
super().__init__(api_key)
|
|
8
|
+
self.taxi_services = self.transport_services[TransportType.TAXI]
|
|
9
|
+
|
|
10
|
+
async def availability(self):
|
|
11
|
+
data = await self.lta_api.fetch(self.taxi_services["AVAILABILITY"])
|
|
12
|
+
return data['value']
|
|
13
|
+
|
|
14
|
+
async def stands(self):
|
|
15
|
+
data = await self.lta_api.fetch(self.taxi_services["STANDS"])
|
|
16
|
+
return data['value']
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
from transport.transport import Transport
|
|
2
|
+
from constants import TransportType
|
|
3
|
+
from typing import Literal
|
|
4
|
+
|
|
5
|
+
class Train(Transport):
|
|
6
|
+
def __init__(self, api_key) -> None:
|
|
7
|
+
super().__init__(api_key)
|
|
8
|
+
self.train_services = self.transport_services[TransportType.TRAIN]
|
|
9
|
+
|
|
10
|
+
async def service_alerts(self):
|
|
11
|
+
data = await self.lta_api.fetch(self.train_services['SERVICE_ALERTS'])
|
|
12
|
+
return data['value']
|
|
13
|
+
|
|
14
|
+
# API currently results in Error 404 not found
|
|
15
|
+
# async def platform_crowd_density(self, type: Literal["realtime", "forecast"] = "realtime"):
|
|
16
|
+
# if type == "realtime":
|
|
17
|
+
# return await self.lta_api.fetch(self.train_services["PLATFORM_CROWD_DENSITY"]['REALTIME'])
|
|
18
|
+
# else:
|
|
19
|
+
# return await self.lta_api.fetch(self.train_services["PLATFORM_CROWD_DENSITY"]["FORECAST"])
|
|
File without changes
|
ltasg-0.0.1/test/test.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import unittest
|
|
2
|
+
from transport.bus import Bus
|
|
3
|
+
from transport.train import Train
|
|
4
|
+
from transport.taxi import Taxi
|
|
5
|
+
import dotenv
|
|
6
|
+
import os
|
|
7
|
+
|
|
8
|
+
class Test(unittest.IsolatedAsyncioTestCase):
|
|
9
|
+
dotenv.load_dotenv()
|
|
10
|
+
api_key = os.getenv("LTA_API_KEY")
|
|
11
|
+
bus = Bus(api_key=api_key)
|
|
12
|
+
train = Train(api_key=api_key)
|
|
13
|
+
taxi = Taxi(api_key=api_key)
|
|
14
|
+
|
|
15
|
+
async def test_bus(self):
|
|
16
|
+
print("Start test for bus \n")
|
|
17
|
+
# bus arrival
|
|
18
|
+
arrival_params = [
|
|
19
|
+
[71111, 63],
|
|
20
|
+
[71111, "63M"],
|
|
21
|
+
[71111, 137],
|
|
22
|
+
[71119, 63],
|
|
23
|
+
[71119, "63M"],
|
|
24
|
+
[71119, 137],
|
|
25
|
+
]
|
|
26
|
+
for param in arrival_params:
|
|
27
|
+
arrival_data = await self.bus.arrival(param[0], param[1])
|
|
28
|
+
print(arrival_data)
|
|
29
|
+
self.assertEqual(str(
|
|
30
|
+
param[1]), arrival_data[0]['ServiceNo'], "Service No is not the same as param")
|
|
31
|
+
# bus services
|
|
32
|
+
services_data = await self.bus.services()
|
|
33
|
+
print(services_data)
|
|
34
|
+
self.assertNotEqual(len(services_data), 0, "Services data is empty")
|
|
35
|
+
|
|
36
|
+
# bus routes
|
|
37
|
+
routes_data = await self.bus.routes()
|
|
38
|
+
print(routes_data)
|
|
39
|
+
self.assertNotEqual(len(routes_data), 0, "Routes data is empty")
|
|
40
|
+
|
|
41
|
+
# bus stops
|
|
42
|
+
stops_data = await self.bus.stops()
|
|
43
|
+
print(stops_data)
|
|
44
|
+
self.assertNotEqual(len(stops_data), 0, "Stops data is empty")
|
|
45
|
+
|
|
46
|
+
print("\Bus test completed\n")
|
|
47
|
+
|
|
48
|
+
async def test_train(self):
|
|
49
|
+
print("Start test for train \n")
|
|
50
|
+
|
|
51
|
+
# service alerts
|
|
52
|
+
service_alerts_data = await self.train.service_alerts()
|
|
53
|
+
print(service_alerts_data)
|
|
54
|
+
self.assertIn(service_alerts_data['Status'], [
|
|
55
|
+
1, 2], "Status returned is not valid")
|
|
56
|
+
# platform crowd density realtime
|
|
57
|
+
|
|
58
|
+
# platform crowd density forecast
|
|
59
|
+
|
|
60
|
+
print("\Train test completed\n")
|
|
61
|
+
|
|
62
|
+
async def test_taxi(self):
|
|
63
|
+
print("Start test for taxi \n")
|
|
64
|
+
|
|
65
|
+
# Availability
|
|
66
|
+
availability_data = await self.taxi.availability()
|
|
67
|
+
print(availability_data)
|
|
68
|
+
self.assertNotEqual(len(availability_data), 0,
|
|
69
|
+
"Availability data is empty")
|
|
70
|
+
# stands
|
|
71
|
+
stands_data = await self.taxi.stands()
|
|
72
|
+
print(stands_data)
|
|
73
|
+
self.assertNotEqual(len(stands_data), 0, "Stands data is empty")
|
|
74
|
+
|
|
75
|
+
print("\Taxi test completed\n")
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
if __name__ == "__main__":
|
|
79
|
+
unittest.main()
|