diff --git a/noaa.py b/noaa.py index 2b608a4..2b0d7d4 100644 --- a/noaa.py +++ b/noaa.py @@ -23,17 +23,26 @@ def __init__(self, latitude, longitude, name, id_): class StationGlobe(object): + def __init__(self, stations, geolocator): self.stations = stations # iterable collection of `Station` objects self.geolocator = geolocator # geopy geolocator @staticmethod def scrape_noaa(geolocator): + db_query = Query() noaa = requests.get(STATION_LIST_URL) stations = [] for match in re.finditer(STATION_LISTING_PATTERN, noaa.text): - geo = geolocator.geocode(match[1]) - stations.append(Station(geo.latitude, geo.longitude, match[1], match[0])) + search = NOAA.module_db.search(db_query.station.id_ == match[0]) + if search is None: + geo = geolocator.geocode(match[1]) + station_object = Station(geo.latitude, geo.longitude, match[1], match[0]) + stations.append(station_object) + NOAA.module_db.insert({'station': station_object}) + else: + stations.append(search) + return StationGlobe(stations, geolocator) def closest_station_coords(self, latitude, longitude): @@ -164,10 +173,10 @@ async def parse_command(self, message, client): if re.match('^[\d]+$', msg[2]): station_id = msg[2] elif coords_match: - station_id = station_globe.closest_station_coords(float(coords_match.group(1)), - float(coords_match.group(3))).id_ + station_id = self.station_globe.closest_station_coords(float(coords_match.group(1)), + float(coords_match.group(3))).id_ else: - station_id = station_globe.closest_station_name(msg[2]).id_ + station_id = self.station_globe.closest_station_name(msg[2]).id_ m_ret = await client.send_message(message.channel, embed=await self.fetching_placeholder()) self.scroll.title = "Tidal information for station #" + station_id @@ -210,4 +219,4 @@ async def on_reaction_add(self, reaction, client, user): embed = self.scroll.previous(current_pos=pos) await client.edit_message(reaction.message, embed=embed) await self.update_pos(reaction.message, 'prev') - + diff --git a/noaa_scraping.py b/noaa_scraping.py new file mode 100644 index 0000000..07a2798 --- /dev/null +++ b/noaa_scraping.py @@ -0,0 +1,12 @@ +# Made by hxtk + +import noaa +import re +import requests +from tinydb import TinyDB, Query +from geopy import geocoders +from geopy.extra.rate_limiter import RateLimiter + +geolocator = geocoders.Nominatim(user_agent='scubot', timeout=5) +geolocator.geocode = RateLimiter(geolocator.geocode, min_delay_seconds=1) +station_globe = noaa.StationGlobe.scrape_noaa(geolocator) \ No newline at end of file