def home(): log.info('STARTING') user_input = request.args.get('user_input', session.get('user_input', '10027')) location, user_input = get_location(user_input) log.info('%s' % location) try: num_hours = int( request.args.get('num_hours', session.get('num_hours', 12))) except: flask.flash('seanweather didnt like the number of hours, using 12') num_hours = 12 if (datetime.now() - location.last_updated).seconds > 2700 or len( location.cache) == 0: log.info('using weather API for %s' % location.zmw) wd = weather_for_url(location.url) location.cache = dumps(wd) if wd: location.last_updated = datetime.now() else: log.warning("didn't get any results from weather API") else: log.info('weather for %s was recently cached, reusing' % location.zmw) location = db.session.merge(location) db.session.add(Lookup(user_input, location)) db.session.commit() weather_data = loads(location.cache) current_temp, max_temp, min_temp = parse_temps(weather_data) icon = parse_icon(weather_data) ds = limit_hours(weather_data, num_hours) session['user_input'] = user_input session['num_hours'] = num_hours session.permanent = True log.info('FINISHED with %s' % user_input) return render_template('weather_form.html', data_string=ds, location=location.name, user_input=user_input, num_hours=num_hours, current_temp=current_temp, max_temp=max_temp, min_temp=min_temp, icon=icon)
def home(): log.info('STARTING') user_input = request.args.get('user_input', session.get('user_input', '10027')) location, user_input = get_location(user_input) log.info('%s' % location) try: num_hours = int(request.args.get('num_hours', session.get('num_hours', 12))) except: flask.flash('seanweather didnt like the number of hours, using 12') num_hours = 12 if (datetime.now()-location.last_updated).seconds > 2700 or len(location.cache) == 0: log.info('using weather API for %s' % location.zmw) wd = weather_for_url(location.url) location.cache = dumps(wd) if wd: location.last_updated = datetime.now() else: log.warning("didn't get any results from weather API") else: log.info('weather for %s was recently cached, reusing' % location.zmw) location = db.session.merge(location) db.session.add(Lookup(user_input, location)) db.session.commit() weather_data = loads(location.cache) current_temp, max_temp, min_temp = parse_temps(weather_data) icon = parse_icon(weather_data) ds = limit_hours(weather_data, num_hours) session['user_input'] = user_input session['num_hours'] = num_hours session.permanent = True log.info('FINISHED with %s' % user_input) return render_template('weather_form.html', data_string=ds, location=location.name, user_input=user_input, num_hours=num_hours, current_temp=current_temp, max_temp=max_temp, min_temp=min_temp, icon=icon)
#!/usr/bin/python from get_json import weather_for_url 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', '94618', '08904'} for z in zipcodes: zmw = '%s.1.99999' % z url = '/q/zmw:%s' % zmw cache = dumps(weather_for_url(url)) last_updated = datetime.now() c.execute('update location set cache=?, last_updated=? where zmw=?', (cache, last_updated, zmw)) conn.commit() conn.close()