def getWeather(resort, db) : url = opensnowWeatherUrl + "&lids=" + resort['opensnow_id'] print "getting OPEN SNOW weather for " + resort['name'] + ": " + url try : response = simplejson.load(urllib2.urlopen(url) ) forecast = response['results']['location' + resort['opensnow_id']]['forecast'] #create the empty row object row = {} row['reported_time'] = forecast['updated_at'] row['resort'] = resort['id'] #first check if entry already exists for that reported time queryString = "select count(*) from " + TABLE_NAME + " where resort='" + resort['id'] + "' and reported_time='" + row['reported_time'] + "'" db.query(queryString) r = db.store_result() results = r.fetch_row(0) count = int(results[0][0]) #print count; #only insert if an entry doesnt already exist for this if (count == 0) : for forecastDate in pDataKeys : row['date'] = forecast[forecastDate]['date'] for timeOfDay in rDataKeys : forecastDayPeriod = forecast[forecastDate][timeOfDay] row['forecast_time'] = timeOfDay row['summary'] = forecastDayPeriod['weather'] row['temp'] = forecastDayPeriod['temp'] row['text_summary'] = forecastDayPeriod['text'] row['snow_forecast'] = parseSnowRange(forecastDayPeriod['snow']) queryString = dbHelper.createInsertStatement(row, TABLE_NAME) #print queryString db.query(queryString) except : print "*****************************************" print "OPENSNOW FORECAST FAILED " + str(datetime.datetime.now()) print url traceback.print_exc(file=sys.stdout) print "*****************************************"
def retrieveNewSnow(filename, db) : resortWeatherInfos = readDataFromFile(filename) for resortWeatherInfo in resortWeatherInfos : snowfallTotal = getNewSnowFallForResort(resortWeatherInfo) #first check if this entry already exists entryAlreadyExistQuery = dbHelper.checkForSnowfallEntryQuery(snowfallTotal, TABLE_NAME) #print entryAlreadyExistQuery db.query(entryAlreadyExistQuery) r = db.store_result() results = r.fetch_row(0) count = int(results[0][0]) #if entry doesn't exist, go ahead and insert if (count == 0) : queryStatement = dbHelper.createInsertStatement(snowfallTotal, TABLE_NAME, False) print queryStatement db.query(queryStatement)
def getWeather(resort, db) : print "getting weather for " + resort['snowforecast_id'] url = sfWeatherUrl + resort['snowforecast_id'] + sfPostUrl soup = xmlHelper.createSoup(url) row = {} #print xmlHelper.getSnowForecastTag("mid", "dayname", soupStr) row['resort'] = resort['id'] row['reported_time'] = xmlHelper.getSnowForecastTag("hash", "issued", soup) #row['resort'] = resort for p in soup.findAll('period') : forecastTimestamp = xmlHelper.getSnowForecastTag("period", "tstampstart", p) forecastDate = datetime.datetime.fromtimestamp(int(forecastTimestamp)) row['date'] = str(forecastDate) row['forecast_time'] = xmlHelper.getSnowForecastTag("period", "ename", p).replace("-<br>","") row['freezing_level'] = xmlHelper.getSnowForecastTag("period", "flevel", p) row['summary'] = xmlHelper.getSnowForecastTag(FORECAST_LOCATION, "pphrase", p) row['low_temp'] = xmlHelper.getSnowForecastTag(FORECAST_LOCATION, "min", p) row['high_temp'] = xmlHelper.getSnowForecastTag(FORECAST_LOCATION, "max", p) row['wind'] = xmlHelper.getSnowForecastTag(FORECAST_LOCATION, "pwind", p) # Parse wind symbol data from something like windSW35metric.gif to SW wsymbol = xmlHelper.getSnowForecastTag(FORECAST_LOCATION, "pwsymbol", p) wsymbol = wsymbol.replace(SNOW_FORECAST_WIND_SYMBOL_PRE, "").replace(SNOW_FORECAST_WIND_SYMBOL_POST, "") wsymbol = wsymbol.replace(row['wind'], "") row['wind_dir'] = wsymbol snow_forecast = xmlHelper.getSnowForecastTag(FORECAST_LOCATION, "psnow", p) if snow_forecast == "-" : snow_forecast = 0 row['snow_forecast'] = snow_forecast queryString = dbHelper.createInsertStatement(row, TABLE_NAME) #print queryString db.query(queryString)
def getWeather(resort, db) : url = nwsWeatherUrl + "&lat=" + resort['latitude'] + "&lon=" + resort['longitude'] print "getting weather for " + resort['name'] + ": " + url try : response = simplejson.load(urllib2.urlopen(url) ) row = {} row['reported_time'] = response['creationDate'] row['resort'] = resort['id'] #first check if entry already exists queryString = "select count(*) from " + TABLE_NAME + " where resort='" + resort['id'] + "' and reported_time='" + row['reported_time'] + "'" db.query(queryString) r = db.store_result() results = r.fetch_row(0) count = int(results[0][0]) #print count; #only insert if an entry doesnt already exist for this if (count == 0) : #determine which days to grab, probably will be all but maybe not the first startIndex = 0 if (response['time']['startPeriodName'][0].find(LATER_TODAY_STRING) >= 0) : startIndex = 1 for i in range(startIndex, len(response['time']['startPeriodName'])) : forecastDate = response['time']['startValidTime'][i] row['date'] = forecastDate.split("T")[0] if (response['time']['startPeriodName'][i].find(NIGHT_STRING) >= 0) : row['forecast_time'] = "night" else : row['forecast_time'] = "day" rData = response['data'] row['summary'] = rData['weather'][i] row['pct_precip'] = rData['pop'][i] if row['forecast_time'] == 'night' : row['low_temp'] = rData['temperature'][i] else : row['high_temp'] = rData['temperature'][i] textSummary = rData['text'][i] row['text_summary'] = textSummary row['snow_forecast'] = 0 if textSummary.find(SNOW_ACCUM_SINGLE_INCH) >= 0: row['snow_forecast'] = 1 else : print textSummary snowAccumIndex = textSummary.find(SNOW_ACCUM_STR) if snowAccumIndex > 0 : tokenized = textSummary[snowAccumIndex + SNOW_ACCUM_STR_LEN:].split(' ') #check special case "around X inches" if (tokenized[0] == 'around') : row['snow_forecast'] = float(tokenized[1]) else : lowSnow = float(tokenized[0]) highSnow = float(tokenized[2]) #Use the midpoint of the forecasted snow row['snow_forecast'] = (lowSnow + highSnow) / 2 #Going to try just going with the highest snow total expected #row['snow_forecast'] = highSnow queryString = dbHelper.createInsertStatement(row, TABLE_NAME) #print queryString db.query(queryString) except simplejson.decoder.JSONDecodeError : print "*****************************************" print "NWS FORECAST FAILED " + str(datetime.datetime.now()) print url print "*****************************************"