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)
#!/usr/bin/python from get_json import weather_for_zip from datetime import datetime from json import dumps from sqlite3 import connect from os.path import dirname, abspath if __name__ == '__main__': directory = dirname(abspath(__file__)) conn = connect(directory + '/db.db') c = conn.cursor() zipcodes = {'10027', '94110', '11205', '08901'} for z in zipcodes: cache = dumps(weather_for_zip(z)) last_updated = datetime.now() c.execute('update location set cache=?,last_updated=? where zipcode=?', (cache, last_updated, z)) conn.commit() conn.close()