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

Adding basic station caching #1

Merged
merged 7 commits into from
Aug 3, 2018
Merged
Show file tree
Hide file tree
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
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)