.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
time_zone.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2# pylint: disable=missing-module-docstring
3
4from __future__ import annotations
5import typing as t
6
7import datetime
8
9from flask_babel import gettext # type: ignore
10from searx.result_types import EngineResults
11from searx.weather import DateTime, GeoLocation
12
13from . import Plugin, PluginInfo
14
15if t.TYPE_CHECKING:
16 from searx.search import SearchWithPlugins
17 from searx.extended_types import SXNG_Request
18 from searx.plugins import PluginCfg
19
20
21@t.final
23 """Plugin to display the current time at different timezones (usually the
24 query city)."""
25
26 id: str = "time_zone"
27 keywords: list[str] = ["time", "timezone", "now", "clock", "timezones"]
28
29 def __init__(self, plg_cfg: "PluginCfg"):
30 super().__init__(plg_cfg)
31
33 id=self.id,
34 name=gettext("Timezones plugin"),
35 description=gettext("Display the current time on different time zones."),
36 preference_section="query",
37 examples=["time Berlin", "clock Los Angeles"],
38 )
39
40 def post_search(self, request: "SXNG_Request", search: "SearchWithPlugins") -> EngineResults:
41 """The plugin uses the :py:obj:`searx.weather.GeoLocation` class, which
42 is already implemented in the context of weather forecasts, to determine
43 the time zone. The :py:obj:`searx.weather.DateTime` class is used for
44 the localized display of date and time."""
45
46 results = EngineResults()
47 if search.search_query.pageno > 1:
48 return results
49
50 # remove keywords from the query
51 query = search.search_query.query
52 query_parts = filter(lambda part: part.lower() not in self.keywords, query.split(" "))
53 search_term = " ".join(query_parts).strip()
54
55 if not search_term:
56 date_time = DateTime(time=datetime.datetime.now())
57 results.add(results.types.Answer(answer=date_time.l10n()))
58 return results
59
60 geo = GeoLocation.by_query(search_term=search_term)
61 if geo:
62 date_time = DateTime(time=datetime.datetime.now(tz=geo.zoneinfo))
63 tz_name = geo.timezone.replace('_', ' ')
64 results.add(
65 results.types.Answer(
66 answer=(f"{tz_name}:" f" {date_time.l10n()} ({date_time.datetime.strftime('%Z')})")
67 )
68 )
69
70 return results
EngineResults post_search(self, "SXNG_Request" request, "SearchWithPlugins" search)
Definition time_zone.py:40
__init__(self, "PluginCfg" plg_cfg)
Definition time_zone.py:29