Ejemplo n.º 1
0
def init_lifts(resort_id):
    parsed_json = parse(resort_id)
    lifts = parsed_json['Lifts']
    lift_names = list(map(lambda l: l['Name'], lifts))
    #save lift names in db
    for lift in lift_names:
        sql = 'INSERT INTO lifts (resort_id, name) VALUES (%s, %s)'
        val = (resort_id, lift)
        db.execute(sql, val)
    db.commit()
    db.close_db_connection()
Ejemplo n.º 2
0
def init_lifts(resort_id):
    lifts_dom = parse(resort_id)
    lifts = lifts_dom.find_all('div','lift')

    lift_names = list(map(lambda l: l.find_all('div','cell')[0].text, lifts))
    #save lift names in db
    for lift in lift_names:
        sql = 'INSERT INTO lifts (resort_id, name) VALUES (%s, %s)'
        val = (resort_id, lift)
        db.execute(sql, val)
    db.commit()
    db.close_db_connection()
Ejemplo n.º 3
0
def get_weather_for_resort(resort_id):
    apikey = config['appid']
    res = db.query('SELECT geo_lat, geo_lon FROM resorts WHERE id='+str(resort_id))
    latitude = res[0]['geo_lat']
    longitude = res[0]['geo_lon']
    url = 'http://api.openweathermap.org/data/2.5/weather?lat={}&lon={}&APPID={}'.format(latitude, longitude, apikey)

    report = requests.get(url)
    weather_data = json.loads(report.content)

    data_map = {
        'source': 'openweathermap',
        'updated_at': time.strftime('%Y-%m-%d %H:%M:%S'),
        'resort_id': resort_id
    }
    if len(weather_data) > 0:
        if len(weather_data['weather']) > 0:
            weather = weather_data['weather'][0]
            if 'id' in weather: data_map['label_id'] = weather['id']
            if 'main' in weather: data_map['label'] = weather['main']
            if 'description' in weather: data_map['description'] = weather['description']
        if 'main' in weather_data:
            main = weather_data['main']
            if 'temp' in main: data_map['temperature'] = (main['temp'] - 273.15) #kelvin->centigrade
            if 'pressure' in main: data_map['pressure'] = main['pressure']
            if 'humidity' in main: data_map['humidity'] = main['humidity']
            if 'temp_min' in main: data_map['temperature_min'] = (main['temp_min'] - 273.15) #kelvin->centigrade
            if 'temp_max' in main: data_map['temperature_max'] = (main['temp_max'] - 273.15) #kelvin->centigrade
        if 'visibility' in weather_data: data_map['visibility'] = weather_data['visibility']
        if 'wind' in weather_data:
            wind = weather_data['wind']
            if 'speed' in wind: data_map['wind_speed'] = wind['speed']
            if 'deg' in wind: data_map['wind_dir'] = wind['deg']
        if 'clouds' in weather_data and 'all' in weather_data['clouds']:
            data_map['cloudiness'] = weather_data['clouds']['all']
        if 'rain' in weather_data:
            rain = weather_data['rain']
            if '1h' in rain: data_map['rain_last_1h'] = rain['1h']
            if '3h' in rain: data_map['rain_last_3h'] = rain['3h']
        if 'snow' in weather_data:
            snow = weather_data['snow']
            if '1h' in snow: data_map['snow_last_1h'] = snow['1h']
            if '3h' in snow: data_map['snow_last_3h'] = snow['3h']
        if 'dt' in weather_data: data_map['data_calculated_at'] = datetime.utcfromtimestamp(weather_data['dt']).strftime('%Y-%m-%d %H:%M:%S')
    cols = ','.join(data_map.keys())
    vals = data_map.values()
    vals = tuple(vals)
    tmp = ','.join(['%s'] * len(vals))
    sql = 'INSERT INTO weather_reports ({}) VALUES ({})'.format(cols,tmp)
    db.execute(sql, vals)
    db.commit()
    db.close_db_connection()
Ejemplo n.º 4
0
def get_lift_status(resort_id):
    global lift_map
    lift_map_sql = db.query('SELECT id, name FROM lifts WHERE resort_id='+str(resort_id))
    for sl in lift_map_sql:
        lift_map[sl['name']] = int(sl['id'])

    lifts_dom = parse(resort_id)
    lifts = lifts_dom.find_all('div','lift')
    lift_status = list(map(get_lift_status_obj, lifts))
    #save lift statuses in db
    for status in lift_status:
        sql = 'INSERT INTO lift_status (lift_id, status, updated_at) VALUES (%s, %s, %s)'
        val = (status['lift_id'], status['lift_status'], time.strftime('%Y-%m-%d %H:%M:%S'))
        db.execute(sql, val)
    db.commit()
    db.close_db_connection()
Ejemplo n.º 5
0
def get_lift_status(resort_id):
    #get mapping of lift name to lift id
    lift_map_sql = db.query('SELECT id, name FROM lifts WHERE resort_id='+str(resort_id))
    lift_map = {}
    for lift in lift_map_sql:
        lift_map[lift['name']] = int(lift['id'])
    parsed_json = parse(resort_id)
    lifts = parsed_json['Lifts']
    lift_status = list(map(lambda l: {'lift_id': lift_map[l['Name']], 'lift_status': l['Status']}, lifts))
    #save lift statuses in db
    for status in lift_status:
        sql = 'INSERT INTO lift_status (lift_id, status, updated_at) VALUES (%s, %s, %s)'
        val = (status['lift_id'], status['lift_status'], time.strftime('%Y-%m-%d %H:%M:%S'))
        db.execute(sql, val)
    db.commit()
    db.close_db_connection()
Ejemplo n.º 6
0
def get_weather_for_resort(resort_id):
    res = db.query('SELECT weather_gridpoint FROM resorts WHERE id=' +
                   str(resort_id))
    gridpoint = res[0]['weather_gridpoint']
    url1 = 'https://api.weather.gov/gridpoints/{}'.format(gridpoint)
    url2 = 'https://api.weather.gov/gridpoints/{}/forecast/hourly'.format(
        gridpoint)

    report1 = requests.get(url1)
    report2 = requests.get(url2)
    weather_data = json.loads(report1.content)
    # print weather_data
    weather_data2 = json.loads(report2.content)

    data_map = {
        'source': 'nws',
        'updated_at': time.strftime('%Y-%m-%d %H:%M:%S'),
        'resort_id': resort_id
    }
    if 'properties' in weather_data:
        keys = mapping.keys()
        for key in keys:
            if key in weather_data['properties'] and 'values' in weather_data[
                    'properties'][key]:
                vals = weather_data['properties'][key]['values']
                if len(vals) > 0:
                    data_map[mapping[key]] = get_current_value(vals)
    if 'properties' in weather_data2:
        if 'periods' in weather_data2['properties'] and len(
                weather_data2['properties']['periods']) > 0:
            period = get_current_period(weather_data2['properties']['periods'])
            if 'shortForecast' in period:
                data_map['label'] = period['shortForecast']
            if 'detailedForecast' in period:
                data_map['description'] = period['detailedForecast']

    # print data_map
    cols = ','.join(data_map.keys())
    vals = data_map.values()
    vals = tuple(vals)
    tmp = ','.join(['%s'] * len(vals))
    sql = 'INSERT INTO weather_reports ({}) VALUES ({})'.format(cols, tmp)
    db.execute(sql, vals)
    db.commit()
    db.close_db_connection()
Ejemplo n.º 7
0
from db import db
db.query('select * from resorts')
db.close_db_connection()

from parsers import epic
epic.get_lift_status(1)

from weather.parsers import openweathermap
openweathermap.get_weather_for_resort(1)

from weather.parsers import nws
nws.get_weather_for_resort(3)