Skip to content

Commit 9128102

Browse files
authored
Merge pull request #15 from nextcloud/feat/public-transport-routing
Feat: public transport routing
2 parents 0fc2cdd + ee07051 commit 9128102

File tree

5 files changed

+84
-1
lines changed

5 files changed

+84
-1
lines changed

.l10nignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
2+
# SPDX-License-Identifier: AGPL-3.0-or-later
3+
js/
4+
vendor/
5+
.venv/
6+
venv/

.tx/config

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
2+
# SPDX-License-Identifier: AGPL-3.0-or-later
3+
[main]
4+
host = https://www.transifex.com
5+
lang_map = th_TH: th, ja_JP: ja, bg_BG: bg, cs_CZ: cs, fi_FI: fi, hu_HU: hu, nb_NO: nb, sk_SK: sk
6+
7+
[o:nextcloud:p:nextcloud:r:context_agent]
8+
file_filter = translationfiles/<lang>/context_agent.po
9+
source_file = translationfiles/templates/context_agent.pot
10+
source_lang = en
11+
type = PO

.tx/l10n/.gitkeep

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
2+
# SPDX-License-Identifier: AGPL-3.0-or-later

ex_app/lib/all_tools/external.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,33 @@ def get_current_weather_for_coordinates(lat: str, lon: str) -> dict[str, typing.
5151
raise Exception('Could not retrieve weather for coordinates')
5252
return json['properties']['timeseries'][0]['data']['instant']['details']
5353

54+
55+
56+
@tool
57+
@safe_tool
58+
def get_public_transport_route_for_coordinates(origin_lat: str, origin_lon: str, destination_lat: str, destination_lon: str, routes: int) -> dict[str, typing.Any]:
59+
"""
60+
Retrieve a public transport route between two coordinates
61+
When using get_public_transport_route_for_coordinates, always let the user know that the routing service here.com was used.
62+
:param origin_lat: Latitude of the starting point
63+
:param origin_lon: Longitude of the starting point
64+
:param destination_lat: Latitude of the destination
65+
:param destination_lon: Longitude of the destination
66+
:param routes: the number of routes returned
67+
:return:
68+
"""
69+
70+
api_key = nc.appconfig_ex.get_value('here_api')
71+
res = httpx.get('https://transit.hereapi.com/v8/routes?transportMode=car&origin='
72+
+ origin_lat + ',' + origin_lon + '&destination=' + destination_lat + ',' + destination_lon
73+
+ '&alternatives=' + str(routes-1) + '&apikey=' + api_key)
74+
json = res.json()
75+
return json
76+
77+
78+
5479
return [
5580
get_coordinates_for_address,
5681
get_current_weather_for_coordinates,
82+
get_public_transport_route_for_coordinates
5783
]

ex_app/lib/main.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
22
# SPDX-License-Identifier: AGPL-3.0-or-later
3+
import os
34
import traceback
45
from contextlib import asynccontextmanager
56
from json import JSONDecodeError
@@ -9,15 +10,32 @@
910
import httpx
1011
from fastapi import FastAPI
1112
from nc_py_api import NextcloudApp, NextcloudException
12-
from nc_py_api.ex_app import AppAPIAuthMiddleware, LogLvl, run_app, set_handlers
13+
from nc_py_api.ex_app import (
14+
AppAPIAuthMiddleware,
15+
LogLvl,
16+
run_app,
17+
set_handlers,
18+
SettingsForm,
19+
SettingsField,
20+
SettingsFieldType)
1321

1422
from ex_app.lib.agent import react
1523
from ex_app.lib.logger import log
1624
from ex_app.lib.provider import provider
1725

26+
from contextvars import ContextVar
27+
from gettext import translation
28+
1829

1930
app_enabled = Event()
2031

32+
LOCALE_DIR = os.path.join(os.path.dirname(os.path.dirname(__file__)), "locale")
33+
current_translator = ContextVar("current_translator")
34+
current_translator.set(translation(os.getenv("APP_ID"), LOCALE_DIR, languages=["en"], fallback=True))
35+
36+
def _(text):
37+
return current_translator.get().gettext(text)
38+
2139
@asynccontextmanager
2240
async def lifespan(app: FastAPI):
2341
set_handlers(app, enabled_handler)
@@ -31,6 +49,24 @@ async def lifespan(app: FastAPI):
3149
APP = FastAPI(lifespan=lifespan)
3250
APP.add_middleware(AppAPIAuthMiddleware) # set global AppAPI authentication middleware
3351

52+
SETTINGS = SettingsForm(
53+
id="settings_context_agent",
54+
section_type="admin",
55+
section_id="ai",
56+
title=_("Context Agent"),
57+
description=_("Find more details on how to set up Context Agent in the Administration documentation."),
58+
fields=[
59+
SettingsField(
60+
id="here_api",
61+
title=_("API Key HERE"),
62+
description=_("Set the API key for the HERE public transport routing"),
63+
type=SettingsFieldType.PASSWORD,
64+
default="",
65+
placeholder=_("API key"),
66+
),
67+
],
68+
)
69+
3470

3571
def enabled_handler(enabled: bool, nc: NextcloudApp) -> str:
3672
# This will be called each time application is `enabled` or `disabled`
@@ -40,6 +76,8 @@ def enabled_handler(enabled: bool, nc: NextcloudApp) -> str:
4076
nc.providers.task_processing.register(provider)
4177
app_enabled.set()
4278
log(nc, LogLvl.WARNING, f"App enabled: {nc.app_cfg.app_name}")
79+
80+
nc.ui.settings.register_form(SETTINGS)
4381
else:
4482
nc.providers.task_processing.unregister(provider.id)
4583
app_enabled.clear()

0 commit comments

Comments
 (0)