Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DB optimizations #7

Open
wants to merge 3 commits into
base: geo
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions noaa.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ def scrape_noaa(geolocator, database, db_query):
print("[NOAA] Fetching list of stations")
station_page = requests.get(STATION_LIST_URL)
stations = []
all_db = database.all()
for match in re.finditer(STATION_LISTING_PATTERN, station_page.text):
stat_id = match[1]
stat_name = html.unescape(match[2])
search = database.get(db_query.station.id_ == stat_id)
if search is None:
search = list(filter(lambda station: station['station']['id_'] == stat_id, all_db))
if not search:
station_info = requests.get(STATION_INFO_URL_FORMAT.format(stat_id))

# Get station latitude
Expand All @@ -75,7 +76,7 @@ def scrape_noaa(geolocator, database, db_query):
stations.append(station_object)
database.insert({'station': station_object.to_dict()})
else:
stations.append(Station.from_dict(search['station']))
stations.append(Station.from_dict(search[0]['station']))
return StationGlobe(stations, geolocator)

def closest_station_coords(self, latitude, longitude):
Expand Down Expand Up @@ -166,7 +167,6 @@ class NOAA(BotModule):

def __init__(self):
super().__init__()
print("Initializing NOAA bot module")
self.station_globe = StationGlobe.scrape_noaa(geocoders.Nominatim(user_agent='scubot'),
self.module_db,
Query())
Expand Down