def get_location(user_input): ''' first check cache. if missing or too old, use autocomplete API ''' last_lookup = Lookup.query.filter_by(user_input=user_input).order_by( Lookup.date.desc()).first() log.info('db result for location: ' + str(last_lookup)) if last_lookup is not None and (datetime.now() - last_lookup.date).seconds < 604800: log.info('got location info from the cache') return last_lookup.location, user_input try: url, location_name, zmw = parse_user_input(user_input) log.info('got location info from autocomplete API') log.info('%s -> %s, %s, %s' % (user_input, url, location_name, zmw)) return Location(zmw, url=url, name=location_name), user_input except IndexError, KeyError: flask.flash( 'seanweather didnt like that, please try another city or zipcode') log.warning('failed to parse: %s. Using 10027' % user_input) last_default = Lookup.query.filter_by(user_input='10027').order_by( Lookup.date.desc()).first() if last_default is not None: location = last_default.location else: url, location_name, zmw = '/q/zmw:10027.1.99999', '10027 - New York, NY', '10027.1.99999' location = Location(zmw, url=url, name=location_name) return location, '10027'
def get_location(user_input): ''' Get the Location corresponding the user input First, we look for the user_input in the Lookup table. If there was a previous lookup, return the corresponding location. If not, use the autocomplete API to get the url. If that url exists in the Location table, return that location. Otherwise, create and return a new Location built from the results of the autocomplete API. ''' last_lookup = (Lookup.query.filter_by(user_input=user_input).order_by( Lookup.date.desc()).first()) log.info('db result for most recent lookup of this location: ' + str(last_lookup)) if (last_lookup is not None and (datetime.now() - last_lookup.date).seconds < 604800): # 7 days log.info('got location info from the cache of a previous lookup') return last_lookup.location, user_input try: url, name = autocomplete_user_input(user_input) log.info('got location info from autocomplete API') log.info('%s -> %s, %s', user_input, url, name) existing_location = Location.query.get(url) if existing_location is None: log.info('no info for that location, creating a new entry') location = Location(url, name=name) else: log.info('already have info for that location, reusing it') location = existing_location return location, user_input except IndexError, KeyError: flask.flash('seanweather didnt like that, please try another city or ' 'zipcode') log.warning('failed to parse: %s. Using %s', user_input, _DEFAULT_USER_INPUT) last_default = (Lookup.query.filter_by( user_input=_DEFAULT_USER_INPUT).order_by( Lookup.date.desc()).first()) if last_default is not None: location = last_default.location else: location = Location('/q/zmw:10027.1.99999', name='10027 -- New York, NY') return location, _DEFAULT_USER_INPUT
def insert_location(self, location): exists = self.session.query(Location).filter_by( name=location['name']).first() if not exists: loc = Location(name=location['name']) self.session.add(loc) logger.write_log("added: " + location['name'] + " to database") self.session.commit()
def home(): log.info('STARTING') prev_zip = session.get('zipcode', '10027') try: zipcode = request.args.get('zipcode', prev_zip) assert(match(r'^\d{5}$', zipcode)) log.info('got %s from the request or session' % zipcode) except: log.info('%s doesnt look like a zipcode, trying to parse' % zipcode) zipcode = get_location(zipcode) if zipcode is None: zipcode = '10027' log.info('couldnt parse location, using %s' % zipcode) else: log.info('%s parsed' % zipcode) try: num_hours = int(request.args.get('num_hours', session.get('num_hours', 12))) except: num_hours = 12 location = Location.query.get(zipcode) if not location: log.info('%s wasnt in the cache, looking up geo information' % zipcode) location = Location(zipcode, geolookup(zipcode)) else: log.info('%s was in the cache, reusing geoinformation' % zipcode) log.info('%s is %s' % (zipcode, location.city)) if (datetime.now()-location.last_updated).seconds > 2700 or len(location.cache) == 0: log.info('looking up the weather for %s' % zipcode) location.cache = dumps(weather_for_zip(zipcode)) location.last_updated = datetime.now() else: log.info('weather for %s was recently cached, reusing' % zipcode) ds = limit(loads(location.cache), num_hours) location = db.session.merge(location) db.session.add(Lookup(location)) db.session.commit() session['zipcode'] = zipcode session['num_hours'] = num_hours log.info('FINISHED with %s' % zipcode) return render_template('weather_form.html', data_string=ds, city=location.city, zipcode=zipcode, num_hours=num_hours)
def __init__(self): self.data_string = '' self.location = Location('/q/zmw:10027.1.99999', name='10027 -- New York, NY') self.user_input = _DEFAULT_USER_INPUT self.num_hours = _DEFAULT_NUM_HOURS self.current_temp = '' self.max_temp = '' self.min_temp = '' self.icon = '' self.units = _DEFAULT_UNITS self.previous = None
def fake(): log.info('STARTING -- fake') sw = SeanWeather() sw.weather_data = [{ 'temp': u'85', 'feel': u'83', 'pop': u'0', 'icon_pos': 100, 'feel_c': u'28', 'temp_c': u'29', 'date': 1498334400000, 'icon': u'http://icons.wxug.com/i/c/k/partlycloudy.gif' }, { 'temp': u'85', 'feel': u'83', 'pop': u'0', 'icon_pos': 100, 'feel_c': u'28', 'temp_c': u'29', 'date': 1498338000000, 'icon': u'http://icons.wxug.com/i/c/k/partlycloudy.gif' }, { 'temp': u'85', 'feel': u'83', 'pop': u'0', 'icon_pos': 100, 'feel_c': u'28', 'temp_c': u'29', 'date': 1498341600000, 'icon': u'http://icons.wxug.com/i/c/k/partlycloudy.gif' }, { 'temp': u'85', 'feel': u'83', 'pop': u'0', 'icon_pos': 100, 'feel_c': u'28', 'temp_c': u'29', 'date': 1498345200000, 'icon': u'http://icons.wxug.com/i/c/k/clear.gif' }, { 'temp': u'83', 'feel': u'82', 'pop': u'0', 'icon_pos': 100, 'feel_c': u'28', 'temp_c': u'28', 'date': 1498348800000, 'icon': u'http://icons.wxug.com/i/c/k/clear.gif' }, { 'temp': u'81', 'feel': u'81', 'pop': u'0', 'icon_pos': 100, 'feel_c': u'27', 'temp_c': u'27', 'date': 1498352400000, 'icon': u'http://icons.wxug.com/i/c/k/nt_clear.gif' }, { 'temp': u'79', 'feel': u'79', 'pop': u'1', 'icon_pos': 100, 'feel_c': u'26', 'temp_c': u'26', 'date': 1498356000000, 'icon': u'http://icons.wxug.com/i/c/k/nt_clear.gif' }, { 'temp': u'78', 'feel': u'78', 'pop': u'2', 'icon_pos': 100, 'feel_c': u'26', 'temp_c': u'26', 'date': 1498359600000, 'icon': u'http://icons.wxug.com/i/c/k/nt_clear.gif' }, { 'temp': u'77', 'feel': u'77', 'pop': u'2', 'icon_pos': 100, 'feel_c': u'25', 'temp_c': u'25', 'date': 1498363200000, 'icon': u'http://icons.wxug.com/i/c/k/nt_clear.gif' }, { 'temp': u'75', 'feel': u'75', 'pop': u'2', 'icon_pos': 100, 'feel_c': u'24', 'temp_c': u'24', 'date': 1498366800000, 'icon': u'http://icons.wxug.com/i/c/k/nt_clear.gif' }, { 'temp': u'74', 'feel': u'74', 'pop': u'3', 'icon_pos': 100, 'feel_c': u'23', 'temp_c': u'23', 'date': 1498370400000, 'icon': u'http://icons.wxug.com/i/c/k/nt_clear.gif' }, { 'temp': u'73', 'feel': u'73', 'pop': u'3', 'icon_pos': 100, 'feel_c': u'23', 'temp_c': u'23', 'date': 1498374000000, 'icon': u'http://icons.wxug.com/i/c/k/nt_clear.gif' }] sw.data_string = jsonify(sw.weather_data) sw.icon = 'http://icons.wxug.com/i/c/k/nt_clear.gif' sw.location = Location('', name='10027 -- New York, NY') sw.user_input = 'chilled' sw.num_hours = 12 sw.current_temp, sw.max_temp, sw.min_temp = 75, 80, 65 log.info('FINISHED with %s -- fake', sw.user_input) return render_template('weather_form.html', sw=sw)
import json import os import sqlalchemy from database import Location from wunderground import weather_for_url, autocomplete_user_input API_KEY = os.environ.get('WUNDERGROUND_KEY', 'development') SQLALCHEMY_DATABASE_URI = 'sqlite:///db.db' def get_db_session(): engine = sqlalchemy.create_engine(SQLALCHEMY_DATABASE_URI, echo=True) return sqlalchemy.orm.sessionmaker(bind=engine)() if __name__ == '__main__': session = get_db_session() zipcodes = {'10003', '12180', '11105', '11215'} for z in zipcodes: url, name = autocomplete_user_input(z) cache = json.dumps(weather_for_url(url, API_KEY)) session.merge(Location(url, name=name, cache=cache)) session.commit()