Skip to content

Commit

Permalink
Improve search | Search based on city names
Browse files Browse the repository at this point in the history
  • Loading branch information
yankeexe committed Jan 26, 2022
1 parent e3c2235 commit 5bce633
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 23 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,14 @@ To run this CLI using Docker, check [Run using Docker :whale:](#run-using-docker
### Search for local date time

You can use short country code like 'AE', 'RU', 'US' and so on.
You can search via city like: 'Paris', 'London', 'Moscow', 'Chicago' and so on.

```bash
$ tz search "us"

$ tz search "Nepal"

$ tz search "Paris"
```

<details><summary><strong>Demo</strong></summary>
Expand Down
5 changes: 2 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"pytz",
"simple-term-menu",
"tzlocal",
"thefuzz[speedup]",
]

# Development Requirements
Expand All @@ -29,9 +30,7 @@
url="https://github.com/yankeexe/timezones-cli",
description="Get local datetime from multiple timezones!",
license="MIT",
packages=setuptools.find_packages(
exclude=["dist", "build", "*.egg-info", "tests"]
),
packages=setuptools.find_packages(exclude=["dist", "build", "*.egg-info", "tests"]),
long_description=long_description,
long_description_content_type="text/markdown",
install_requires=requirements,
Expand Down
25 changes: 7 additions & 18 deletions timezones_cli/commands/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

import click
import pycountry
import pytz
from thefuzz import process

from timezones_cli.utils import (
console,
extract_fuzzy_country_data,
get_local_time,
get_timezones,
handle_interaction,
)
from timezones_cli.utils import (console, extract_fuzzy_country_data,
get_local_time, get_timezones,
handle_interaction, query_handler)


@click.command()
Expand All @@ -31,20 +29,11 @@ def search(query: str, toggle: bool):
$ tz search Africa
"""
try:
# Search with user query.
# @TODO: Handle list with multiple data.
data: t.List = pycountry.countries.search_fuzzy(query)

# extract alpha2 value
_, _, alpha_2, _ = extract_fuzzy_country_data(data)

# Get a list of timezone names.
result = get_timezones(alpha_2)

result = query_handler(query)
payload: t.List = []

# If length is greater than one, show terminal menu.
if len(result) > 1:
if isinstance(result, t.List) and len(result) > 1:
entry = handle_interaction(result)

payload.append(entry)
Expand Down
30 changes: 28 additions & 2 deletions timezones_cli/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
from datetime import datetime
from datetime import time as time_obj
from datetime import timezone
from typing import List, NamedTuple, Optional, Tuple, Union
from typing import Any, List, NamedTuple, Optional, Tuple, Union

import click
import pycountry
import pytz
from rich.console import Console
from simple_term_menu import TerminalMenu
from tabulate import tabulate
from thefuzz import process
from tzlocal import get_localzone

from timezones_cli.utils import variables
Expand Down Expand Up @@ -122,7 +123,7 @@ def extract_fuzzy_country_data(
return name, official_name, alpha_2, alpha_3


def get_local_time(timezones: List, query: Optional[str] = None, toggle=False):
def get_local_time(timezones: List[Any], query: Optional[str] = None, toggle=False):
"""
Get localtime based on passed timezones.
"""
Expand Down Expand Up @@ -285,3 +286,28 @@ def get_local_utc_time():
tabulate([(timezone, time, utc_time)], headers, tablefmt="fancy_grid")
)
sys.exit()


def match_fuzzy(query):
timezones = []
all_timezones = list(pytz.all_timezones)
matches = process.extractBests(query, all_timezones)

for match in matches:
if match[1] >= 75:
timezones.append(match[0])

return timezones


def query_handler(query: str) -> List:
timezones = match_fuzzy(query)

if len(query) in (2, 3) or not timezones:
data: List = pycountry.countries.search_fuzzy(query)
# extract alpha2 value
_, _, alpha_2, _ = extract_fuzzy_country_data(data)
# Get a list of timezone names.
timezones = get_timezones(alpha_2)

return timezones

0 comments on commit 5bce633

Please sign in to comment.