mirror of
https://github.com/searxng/searxng.git
synced 2025-09-01 19:13:50 +00:00
[feat] plugins: add new time/timezone search plugin
The plugin uses the ``GeoLocation`` class, which is already implemented in the context of weather forecasts, to determine the time zone. The ``DateTime`` class is used for the localized display of date and time. Co-authored-by: Markus Heiser <markus.heiser@darmarit.de>
This commit is contained in:
parent
90e602b349
commit
5ca08c1813
5 changed files with 87 additions and 0 deletions
|
@ -13,3 +13,4 @@ Built-in Plugins
|
||||||
self_info
|
self_info
|
||||||
tor_check
|
tor_check
|
||||||
unit_converter
|
unit_converter
|
||||||
|
time_zone
|
||||||
|
|
8
docs/dev/plugins/time_zone.rst
Normal file
8
docs/dev/plugins/time_zone.rst
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
.. _time zone plugin:
|
||||||
|
|
||||||
|
=========
|
||||||
|
Time Zone
|
||||||
|
=========
|
||||||
|
|
||||||
|
.. automodule:: searx.plugins.time_zone
|
||||||
|
:members:
|
70
searx/plugins/time_zone.py
Normal file
70
searx/plugins/time_zone.py
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
# pylint: disable=missing-module-docstring
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
import typing as t
|
||||||
|
|
||||||
|
import datetime
|
||||||
|
|
||||||
|
from flask_babel import gettext # type: ignore
|
||||||
|
from searx.result_types import EngineResults
|
||||||
|
from searx.weather import DateTime, GeoLocation
|
||||||
|
|
||||||
|
from . import Plugin, PluginInfo
|
||||||
|
|
||||||
|
if t.TYPE_CHECKING:
|
||||||
|
from searx.search import SearchWithPlugins
|
||||||
|
from searx.extended_types import SXNG_Request
|
||||||
|
from searx.plugins import PluginCfg
|
||||||
|
|
||||||
|
|
||||||
|
@t.final
|
||||||
|
class SXNGPlugin(Plugin):
|
||||||
|
"""Plugin to display the current time at different timezones (usually the
|
||||||
|
query city)."""
|
||||||
|
|
||||||
|
id: str = "time_zone"
|
||||||
|
keywords: list[str] = ["time", "timezone", "now", "clock", "timezones"]
|
||||||
|
|
||||||
|
def __init__(self, plg_cfg: "PluginCfg"):
|
||||||
|
super().__init__(plg_cfg)
|
||||||
|
|
||||||
|
self.info = PluginInfo(
|
||||||
|
id=self.id,
|
||||||
|
name=gettext("Timezones plugin"),
|
||||||
|
description=gettext("Display the current time on different time zones."),
|
||||||
|
preference_section="query",
|
||||||
|
examples=["time Berlin", "clock Los Angeles"],
|
||||||
|
)
|
||||||
|
|
||||||
|
def post_search(self, request: "SXNG_Request", search: "SearchWithPlugins") -> EngineResults:
|
||||||
|
"""The plugin uses the :py:obj:`searx.weather.GeoLocation` class, which
|
||||||
|
is already implemented in the context of weather forecasts, to determine
|
||||||
|
the time zone. The :py:obj:`searx.weather.DateTime` class is used for
|
||||||
|
the localized display of date and time."""
|
||||||
|
|
||||||
|
results = EngineResults()
|
||||||
|
if search.search_query.pageno > 1:
|
||||||
|
return results
|
||||||
|
|
||||||
|
# remove keywords from the query
|
||||||
|
query = search.search_query.query
|
||||||
|
query_parts = filter(lambda part: part.lower() not in self.keywords, query.split(" "))
|
||||||
|
search_term = " ".join(query_parts).strip()
|
||||||
|
|
||||||
|
if not search_term:
|
||||||
|
date_time = DateTime(time=datetime.datetime.now())
|
||||||
|
results.add(results.types.Answer(answer=date_time.l10n()))
|
||||||
|
return results
|
||||||
|
|
||||||
|
geo = GeoLocation.by_query(search_term=search_term)
|
||||||
|
if geo:
|
||||||
|
date_time = DateTime(time=datetime.datetime.now(tz=geo.zoneinfo))
|
||||||
|
tz_name = geo.timezone.replace('_', ' ')
|
||||||
|
results.add(
|
||||||
|
results.types.Answer(
|
||||||
|
answer=(f"{tz_name}:" f" {date_time.l10n()} ({date_time.datetime.strftime('%Z')})")
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
return results
|
|
@ -238,6 +238,9 @@ plugins:
|
||||||
searx.plugins.hostnames.SXNGPlugin:
|
searx.plugins.hostnames.SXNGPlugin:
|
||||||
active: true
|
active: true
|
||||||
|
|
||||||
|
searx.plugins.time_zone.SXNGPlugin:
|
||||||
|
active: true
|
||||||
|
|
||||||
searx.plugins.oa_doi_rewrite.SXNGPlugin:
|
searx.plugins.oa_doi_rewrite.SXNGPlugin:
|
||||||
active: false
|
active: false
|
||||||
|
|
||||||
|
|
|
@ -20,6 +20,7 @@ import typing
|
||||||
import base64
|
import base64
|
||||||
import datetime
|
import datetime
|
||||||
import dataclasses
|
import dataclasses
|
||||||
|
import zoneinfo
|
||||||
|
|
||||||
from urllib.parse import quote_plus
|
from urllib.parse import quote_plus
|
||||||
|
|
||||||
|
@ -137,6 +138,10 @@ class GeoLocation:
|
||||||
country_code: str # 2-Character ISO-3166-1 alpha2 country code. E.g. DE for Germany
|
country_code: str # 2-Character ISO-3166-1 alpha2 country code. E.g. DE for Germany
|
||||||
timezone: str # Time zone using time zone database definitions
|
timezone: str # Time zone using time zone database definitions
|
||||||
|
|
||||||
|
@property
|
||||||
|
def zoneinfo(self) -> zoneinfo.ZoneInfo:
|
||||||
|
return zoneinfo.ZoneInfo(self.timezone)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue