1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
|
import asyncio
import os
import sys
from datetime import datetime, timedelta
import aiohttp
from pygti.auth import Auth
from pygti.gti import GTI
GTI_USER = None
GTI_PASS = None
try:
from dotenv import load_dotenv
load_dotenv()
GTI_USER = os.getenv("GTI_USER")
GTI_PASS = os.getenv("GTI_PASS")
except ImportError:
pass
if not (GTI_USER and GTI_PASS):
print("To run the examples, enter your credentials for the GTI API.")
GTI_USER = input("GTI Username: ")
GTI_PASS = input("GTI Password: ")
async def main():
async with aiohttp.ClientSession() as session:
auth = Auth(session, GTI_USER, GTI_PASS)
gti = GTI(auth)
print("Example 1: init()")
ir = await gti.init()
print(ir)
print()
print("Example 2: checkName()")
cn = await gti.checkName({"theName": {"name": "Wartenau"}})
print(cn)
print()
print("Example 3: getRoute()")
payload = {
"language": "de",
"start": {
"name": "Ritterstraße",
"city": "Hamburg",
"combinedName": "Ritterstraße",
"id": "Master:60904",
"type": "STATION",
"coordinate": {"x": 10.046196, "y": 53.567617},
},
"dest": {
"name": "Wartenau",
"city": "Hamburg",
"combinedName": "Wartenau",
"id": "Master:10901",
"type": "STATION",
"coordinate": {"x": 10.035515, "y": 53.56478},
},
"time": {"date": "heute", "time": "jetzt"},
"timeIsDeparture": True,
"realtime": "REALTIME",
}
gr = await gti.getRoute(payload)
print(gr)
print()
print("Example 4.1: departureList()")
dl = await gti.departureList(
{
"station": {
"name": "Wartenau",
"id": "Master:10901",
"type": "STATION",
},
"time": {"date": "heute", "time": "jetzt"},
"maxList": 5,
"maxTimeOffset": 200,
"useRealtime": True,
}
)
print(dl)
print()
print("Example 4.2: departureList(), return filters")
dl = await gti.departureList(
{
"station": {
"name": "Wartenau",
"id": "Master:10901",
"type": "STATION",
},
"time": {"date": "heute", "time": "jetzt"},
"maxList": 5,
"maxTimeOffset": 200,
"useRealtime": True,
"returnFilters": True,
}
)
print(dl)
print()
print("Example 5: getTariff()")
gT = await gti.getTariff(
{
"scheduleElements": [
{
"departureStationId": "Master:10950",
"arrivalStationId": "Master:37979",
"lineId": "DB-EFZ:RE8_DB-EFZ_Z",
}
],
"departure": {"date": "16.02.2020", "time": "8:04"},
"arrival": {"date": "16.02.2020", "time": "8:29"},
}
)
print(gT)
print()
print("Example 6.1: departureCourse() time and direction")
gT = await gti.departureCourse(
{
"lineKey": "VHH:569_VHH",
"station": {
"name": "Rosenhof",
"city": "Ahrensburg",
"combinedName": "Ahrensburg, Rosenhof",
"id": "Master:35009",
"type": "STATION",
"coordinate": {"x": 10.240928, "y": 53.683071},
},
"time": datetime.fromisoformat("2020-10-21T10:19:00.000"),
"direction": "Ahrensburg, Schulzentrum Am Heimgarten",
}
)
# print(gT)
print(
"departureCourse() output is too long, please uncomment only if neccessary"
)
# print()
# print("Example 6.2: departureCourse() time and serviceId")
# gT = await gti.departureCourse(
# {
# "lineKey": "HHA-U:U1_HHA-U",
# "station": {
# "name": "Wartenau",
# "id": "Master:10901",
# "type": "STATION",
# },
# "time": datetime.now(),
# "serviceId": 1626150555,
# }
# )
# print(gT)
print(
"departureCourse() output is too long, please uncomment only if neccessary"
)
print("Example 7: listStations()")
# used a older dataReleaseID to show changes since then in the response
ls = await gti.listStations({"dataReleaseID": "32.17.02"})
print(ls)
print()
print("Example 8: listLines()")
ll = await gti.listLines({"dataReleaseID": "32.17.02"})
print(ll)
print("Example 9: AnnouncementRequest")
ar = await gti.getAnnouncements(
{
"names": ["S3"],
"timeRange": {
"begin": datetime.now() - timedelta(days=2),
"end": datetime.now() + timedelta(days=10),
},
}
)
print(ar)
print()
print("Example 10: getIndividualRoute()")
payload = {
"starts": [
{"type": "ADDRESS", "coordinate": {"x": 9.92496, "y": 53.563494}}
],
"dests": [
{"type": "ADDRESS", "coordinate": {"x": 9.924269, "y": 53.562925}}
],
"maxLength": 10000,
"serviceType": "BICYCLE",
"profile": "BICYCLE_NORMAL",
"speed": "NORMAL",
}
indRoute = await gti.getIndividualRoute(payload)
print(indRoute)
print()
print("Example 11a: getVehicleMap()")
payload = {
"boundingBox": {
"lowerLeft": {"x": 9.985707, "y": 53.573138, "type": "EPSG_4326"},
"upperRight": {"x": 9.992702, "y": 53.576916, "type": "EPSG_4326"},
},
"periodBegin": int(datetime.timestamp(datetime.now())),
"periodEnd": int(datetime.timestamp(datetime.now() + timedelta(minutes=1))),
"withoutCoords": True,
"coordinateType": "EPSG_31467",
"vehicleTypes": ["U_BAHN"],
"realtime": False,
}
vm = await gti.getVehicleMap(payload)
print(vm)
print()
print("Example 11b: getTrackCoordinates()")
payload = {
"coordinateType": "EPSG_4326",
"stopPointKeys": ["HHA-U:909010:1", "HHA-U:119000:1"],
}
tc = await gti.getTrackCoordinates(payload)
print(tc)
print()
print("Example 12: checkPostalCode()")
pc = await gti.checkPostalCode({"postalCode": 20355})
print(pc)
print()
print("Example 13: stationInformation()")
si = await gti.stationInformation(
{"station": {"name": "Wartenau", "id": "Master:10901", "type": "STATION"}}
)
print(si)
print()
print("Example 14: tariffZoneNeighbours()")
tzn = await gti.tariffZoneNeighbours({})
print(tzn)
print()
print("Example 15.1: tariffMetaData() in german")
tmd = await gti.tariffMetaData({})
print(tmd)
print()
print("Example 15.2: tariffMetaData() in english")
tmd = await gti.tariffMetaData({"language": "en"})
print(tmd)
print()
print("Example 16.1: singleTicketOptimizer()")
sto = await gti.singleTicketOptimizer(
{
"withReturnJourney": True,
"numberOfAdults": 2,
"numberOfChildren": 0,
"tickets": [],
"route": {
"trip": [
{
"start": {"id": "Master:43063", "name": "Außenmühle"},
"destination": {
"id": "Master:49001",
"name": "Bf. Harburg",
},
"line": {"id": "HHA-B:145_HHA-B", "name": "145"},
"vehicleType": "Bus",
},
{
"start": {"id": "Master:49950", "name": "Harburg"},
"destination": {
"id": "Master:80950",
"name": "Landungsbrücken",
},
"line": {"id": "SBH:S3_SBH_SBAHNS", "name": "S3"},
"vehicleType": "S",
},
{
"start": {
"id": "Master:80984",
"name": "Landungsbrücken Brücke 1",
},
"destination": {
"id": "Master:80932",
"name": "Elbphilharmonie",
},
"line": {"id": "ZVU-DB:72_ZVU-DB_HADAGZ", "name": "72"},
"vehicleType": "Schiff",
},
],
"departure": datetime.fromisoformat("2020-04-17T18:16:00.000"),
"arrival": datetime.fromisoformat("2020-04-17T19:14:00.000"),
"tariffRegions": {
"zones": [{"regions": ["308", "208", "108", "000"]}],
"rings": [{"regions": ["B", "A"]}],
"counties": [{"regions": ["HH1", "HH2"]}],
},
"singleTicketTariffLevelId": 14,
"extraFareType": "NO",
},
}
)
print(sto)
print()
print("Example 17: ticketList()")
tl = await gti.ticketList({"stationKey": "Master:92903"})
print(tl)
# To avoid 'Event loop is closed' RuntimeError due to compatibility issue with aiohttp
if sys.platform.startswith("win") and sys.version_info >= (3, 8):
try:
from asyncio import WindowsSelectorEventLoopPolicy
except ImportError:
pass
else:
if not isinstance(
asyncio.get_event_loop_policy(), WindowsSelectorEventLoopPolicy
):
asyncio.set_event_loop_policy(WindowsSelectorEventLoopPolicy())
asyncio.run(main())
|