MCPcopy Create free account
hub / github.com/OpenBMB/BMTools / cName2IATA

Function cName2IATA

bmtools/tools/travel/api.py:65–90  ·  view source on GitHub ↗

This function accepts a city name and returns a IATA pilot code of the local airport. :param (str) city_name: city name a s a string like 'Beijing' :return: 3-letter IATA code like 'PEK'

(city_name: str)

Source from the content-addressed store, hash-verified

63
64
65 def cName2IATA(city_name: str):
66 """
67 This function accepts a city name and returns a IATA pilot code of the local airport.
68 :param (str) city_name: city name a s a string like 'Beijing'
69 :return: 3-letter IATA code like 'PEK'
70
71 """
72 try:
73 url = f"https://www.iata.org/en/publications/directories/code-search/?airport.search={city_name}"
74 response = requests.get(url)
75 html_content = response.content
76
77 soup = str(BeautifulSoup(html_content, "html.parser"))
78 head = soup.find(f"<td>{city_name}</td>")
79
80 string = soup[head:]
81 pattern = r"<td>(.*?)</td>"
82 matches = re.findall(pattern, string)
83
84 # Extract the desired value
85 desired_value = matches[2] # Index 2 corresponds to the third match (WUH)
86
87 return desired_value # Output: WUH
88
89 except:
90 raise ValueError("The input city may not have an IATA registered air-port.")
91
92
93 @tool.get("/lodgingProducts")

Callers 1

flightProductsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected