Skip to content

Commit

Permalink
Merge pull request #1 from suclearnub/master
Browse files Browse the repository at this point in the history
Adding basic station caching
  • Loading branch information
hxtk committed Aug 3, 2018
2 parents 34a27fc + e2a3df1 commit a77cd8e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
21 changes: 15 additions & 6 deletions noaa.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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')

12 changes: 12 additions & 0 deletions noaa_scraping.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit a77cd8e

Please sign in to comment.