This commit is contained in:
Daniel Tsvetkov 2019-04-20 23:23:30 +02:00
parent fae628b534
commit 17ca8f8b33
2 changed files with 17 additions and 16 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
venv
data/.cache.csv
__pycache__
.idea

32
tww.py
View File

@ -3,10 +3,9 @@
Find time now, in the past or future in any timezone or location.
"""
import os
import argparse
import logging
import sys
import os
import dateparser
from pytz.exceptions import UnknownTimeZoneError
@ -20,6 +19,7 @@ basepath = os.path.dirname(os.path.abspath(__file__))
logging.basicConfig()
logger = logging.getLogger()
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('query', nargs='*', default="now", help="<datetime-like> to <timezone-like or location string>")
@ -36,19 +36,19 @@ def setup_logging_level(debug=False):
logger.debug("Debugging enabled")
class Location:
"""
Represents a location with name, latitude and longitude
"""
def __init__(self, name:str, latitude: float, longitude: float):
def __init__(self, name: str, latitude: float, longitude: float):
self.name = name
self.latitude = latitude
self.longitude = longitude
def __lt__(self, other):
return self.name < other.name
def __str__(self):
return "{} {} {}".format(self.name, self.latitude, self.longitude)
@ -59,9 +59,9 @@ def normalize_words_to_number(query):
Assumes one-word numbers used
"""
from word2number import w2n
normal_list = []
for word in query.split():
try:
normal_list.append(str(w2n.word_to_num(word)))
@ -79,7 +79,7 @@ def timezone_to_normal(query):
america/new_york -> America/New_York
"""
import re
# The magic in the regex is that it splits by either / OR _ OR -
# where the | are OR; and then the parens ( ) keep the splitting
# entries in the list so that we can join later
@ -98,9 +98,9 @@ def create_if_not_exists(fname):
def write_to_cache(query, location):
import csv
logger.debug("Writing location to cache")
with open(os.path.join(basepath, "data",".cache.csv"), 'a+') as wf:
with open(os.path.join(basepath, "data", ".cache.csv"), 'a+') as wf:
cachewriter = csv.writer(wf)
cachewriter.writerow([query,
location.latitude,
@ -124,11 +124,11 @@ def resolve_location_local(query):
from fuzzywuzzy import fuzz
query = query.lower()
create_if_not_exists(os.path.join(basepath, "data",".cache.csv"))
create_if_not_exists(os.path.join(basepath, "data", ".cache.csv"))
# location hypothesis heap
heap = []
for fname in [".cache", "countries", "cities"]:
with open(os.path.join(basepath, "data", "{}.csv".format(fname))) as f:
cfile = csv.reader(f)
@ -196,9 +196,9 @@ def parse_query(query):
logger.debug("raw human_dt: {}".format(human_dt))
logger.debug("raw human_tz_loc: {}".format(human_tz_loc))
human_dt = normalize_words_to_number(human_dt)
return human_dt, human_tz_loc