VyLala commited on
Commit
2b37483
·
verified ·
1 Parent(s): d839145

Update standardize_location.py

Browse files
Files changed (1) hide show
  1. standardize_location.py +75 -73
standardize_location.py CHANGED
@@ -1,74 +1,76 @@
1
- import requests
2
- import re
3
-
4
- # Normalize input
5
- def normalize_key(text):
6
- return re.sub(r"[^a-z0-9]", "", text.strip().lower())
7
-
8
- # Search for city/place (normal flow)
9
- def get_country_from_geonames(city_name, username="vyphung"):
10
- url = "http://api.geonames.org/searchJSON"
11
- params = {
12
- "q": city_name,
13
- "maxRows": 1,
14
- "username": username
15
- }
16
- try:
17
- r = requests.get(url, params=params, timeout=5)
18
- data = r.json()
19
- if data.get("geonames"):
20
- return data["geonames"][0]["countryName"]
21
- except Exception as e:
22
- print("GeoNames searchJSON error:", e)
23
- return None
24
-
25
- # Search for country info using alpha-2/3 codes or name
26
- def get_country_from_countryinfo(input_code, username="vyphung"):
27
- url = "http://api.geonames.org/countryInfoJSON"
28
- params = {
29
- "username": username
30
- }
31
- try:
32
- r = requests.get(url, params=params, timeout=5)
33
- data = r.json()
34
- if data.get("geonames"):
35
- input_code = input_code.strip().upper()
36
- for country in data["geonames"]:
37
- # Match against country name, country code (alpha-2), iso alpha-3
38
- if input_code in [
39
- country.get("countryName", "").upper(),
40
- country.get("countryCode", "").upper(),
41
- country.get("isoAlpha3", "").upper()
42
- ]:
43
- return country["countryName"]
44
- except Exception as e:
45
- print("GeoNames countryInfoJSON error:", e)
46
- return None
47
-
48
- # Combined smart lookup
49
- def smart_country_lookup(user_input, username="vyphung"):
50
- raw_input = user_input.strip()
51
- normalized = re.sub(r"[^a-zA-Z0-9]", "", user_input).upper() # normalize for codes (no strip spaces!)
52
-
53
- # Special case: if user writes "UK: London" split and take main country part
54
- if ":" in raw_input:
55
- raw_input = raw_input.split(":")[0].strip() # only take "UK"
56
- # First try as country code (if 2-3 letters or common abbreviation)
57
- if len(normalized) <= 3:
58
- if normalized.upper() in ["UK","U.K","U.K."]:
59
- country = get_country_from_geonames(normalized.upper(), username=username)
60
- if country:
61
- return country
62
- else:
63
- country = get_country_from_countryinfo(raw_input, username=username)
64
- if country:
65
- return country
66
- country = get_country_from_countryinfo(raw_input, username=username) # try full names
67
- if country:
68
- return country
69
- # Otherwise, treat as city/place
70
- country = get_country_from_geonames(raw_input, username=username)
71
- if country:
72
- return country
73
-
 
 
74
  return "Not found"
 
1
+ import requests
2
+ import re
3
+
4
+ # Normalize input
5
+ def normalize_key(text):
6
+ return re.sub(r"[^a-z0-9]", "", text.strip().lower())
7
+
8
+ # Search for city/place (normal flow)
9
+ def get_country_from_geonames(city_name):
10
+ url = os.environ["URL_SEARCHJSON"]
11
+ username = os.environ["USERNAME_GEO"]
12
+ params = {
13
+ "q": city_name,
14
+ "maxRows": 1,
15
+ "username": username
16
+ }
17
+ try:
18
+ r = requests.get(url, params=params, timeout=5)
19
+ data = r.json()
20
+ if data.get("geonames"):
21
+ return data["geonames"][0]["countryName"]
22
+ except Exception as e:
23
+ print("GeoNames searchJSON error:", e)
24
+ return None
25
+
26
+ # Search for country info using alpha-2/3 codes or name
27
+ def get_country_from_countryinfo(input_code):
28
+ url = os.environ["URL_COUNTRYJSON"]
29
+ username = os.environ["USERNAME_GEO"]
30
+ params = {
31
+ "username": username
32
+ }
33
+ try:
34
+ r = requests.get(url, params=params, timeout=5)
35
+ data = r.json()
36
+ if data.get("geonames"):
37
+ input_code = input_code.strip().upper()
38
+ for country in data["geonames"]:
39
+ # Match against country name, country code (alpha-2), iso alpha-3
40
+ if input_code in [
41
+ country.get("countryName", "").upper(),
42
+ country.get("countryCode", "").upper(),
43
+ country.get("isoAlpha3", "").upper()
44
+ ]:
45
+ return country["countryName"]
46
+ except Exception as e:
47
+ print("GeoNames countryInfoJSON error:", e)
48
+ return None
49
+
50
+ # Combined smart lookup
51
+ def smart_country_lookup(user_input):
52
+ raw_input = user_input.strip()
53
+ normalized = re.sub(r"[^a-zA-Z0-9]", "", user_input).upper() # normalize for codes (no strip spaces!)
54
+
55
+ # Special case: if user writes "UK: London" split and take main country part
56
+ if ":" in raw_input:
57
+ raw_input = raw_input.split(":")[0].strip() # only take "UK"
58
+ # First try as country code (if 2-3 letters or common abbreviation)
59
+ if len(normalized) <= 3:
60
+ if normalized.upper() in ["UK","U.K","U.K."]:
61
+ country = get_country_from_geonames(normalized.upper())
62
+ if country:
63
+ return country
64
+ else:
65
+ country = get_country_from_countryinfo(raw_input)
66
+ if country:
67
+ return country
68
+ country = get_country_from_countryinfo(raw_input) # try full names
69
+ if country:
70
+ return country
71
+ # Otherwise, treat as city/place
72
+ country = get_country_from_geonames(raw_input)
73
+ if country:
74
+ return country
75
+
76
  return "Not found"