forked from mileserickson/stadfinder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stadfinder.py
52 lines (39 loc) · 1.38 KB
/
stadfinder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""Stadium Finder."""
import requests
from bs4 import BeautifulSoup
def get_stadium_coordinates(stadium_name):
"""Return the coordinates of a stadium.
Parameters
----------
name : string
Name of stadium (must match Wikipedia)
Returns
-------
latitude, longitude : float, float
Latitude and longitude in decimal degrees.
Examples
--------
>>> get_stadium_coordinates("CenturyLink Field")
(47.5952, -122.3316)
>>> get_stadium_coordinates("Trumpeter Swan Arena 679") == None
True
"""
WIKIPEDIA_URL = "https://en.wikipedia.org/wiki/"
stadium_name = stadium_name.replace(" ", "_")
r = requests.get(WIKIPEDIA_URL + stadium_name)
stadium_soup = BeautifulSoup(r.content, "lxml")
#TODO(Miles): Update for geo-dms
location_html = stadium_soup.select_one("span.geo-dec")
if not location_html:
return None # Stadium not found
location = location_html.get_text().encode('ascii', 'ignore')
latitude, longitude = location.split()
print latitude, longitude
# North and east are positive, south and west are negative
sign_dict = {'N': 1, 'E': 1, 'S': -1, 'W': -1}
def decimalize(l):
"""Convert text lat or long to decimal."""
return float(l[:-1]) * sign_dict[l[-1]]
latitude = decimalize(latitude)
longitude = decimalize(longitude)
return (latitude, longitude)