Exemple #1
0
def forecast_update(site_key):
    site = Site.get_by_key_name(site_key)
    if site is None:
        return Response(status = 404)

    forecast_url = "http://www.metoffice.gov.uk/public/data/PWSCache/BestForecast/Forecast/%s?format=application/json" % site_key

    result = urlfetch.fetch(forecast_url)
    if result.status_code == 200:
        forecast = parse_forecast(result.content)
        issued_date = parse_date(forecast["@dataDate"])
        for date, day in days(forecast):
            forecast_day = ForecastDay.get_by_key_name(make_key_name(site,date))
            if forecast_day is None:
                forecast_day = ForecastDay(key_name=make_key_name(site,date), forecast_date = date, site = site)
            forecast_day.site = site
            for timestep, data in day_timesteps(day):
                w = Forecast()
                w.issued = issued_date
                for k,v in data.items():
                    prop_name = snake_case(k)
                    if hasattr(w, prop_name):
                        if v == "missing":
                            v = None
                        setattr(w, prop_name, v)

                forecast_day.forecasts.add(timestep,w)

            forecast_day.save()
        site.save()
    return Response(status = 204)
Exemple #2
0
def forecast_update(site_key):
    site = Site.get_by_key_name(site_key)
    if site is None:
        return Response(status=404)

    forecast_url = "http://www.metoffice.gov.uk/public/data/PWSCache/BestForecast/Forecast/%s?format=application/json" % site_key

    result = urlfetch.fetch(forecast_url)
    if result.status_code == 200:
        forecast = parse_forecast(result.content)
        issued_date = parse_date(forecast["@dataDate"])
        for date, day in days(forecast):
            forecast_day = ForecastDay.get_by_key_name(
                make_key_name(site, date))
            if forecast_day is None:
                forecast_day = ForecastDay(key_name=make_key_name(site, date),
                                           forecast_date=date,
                                           site=site)
            forecast_day.site = site
            for timestep, data in day_timesteps(day):
                w = Forecast()
                w.issued = issued_date
                for k, v in data.items():
                    prop_name = snake_case(k)
                    if hasattr(w, prop_name):
                        if v == "missing":
                            v = None
                        setattr(w, prop_name, v)

                forecast_day.forecasts.add(timestep, w)

            forecast_day.save()
        site.save()
    return Response(status=204)
Exemple #3
0
def site_forecasts(site_id):
    site = Site.get_by_key_name(site_id)
    if site is None:
        return Response(status = 404)

    if request.args.has_key('day'):
        p = request.args.get('day')
        day = parse_yyyy_mm_dd_date(p)
    else:
        return Response("day query parameter is required",status = 400)

    forecast_day = ForecastDay.get_by_key_name(make_key_name(site,day))

    if forecast_day is None:
        return Response(status = 404)

    return json_response(forecast_day)
Exemple #4
0
def forecast_import(site_id):
    site = Site.get_by_key_name(site_id)
    if site is None:
        return Response(status = 404)
    today = date.today()
    url = "http://metofficewatch.appspot.com/sites/%s/forecasts?day=%s" % (site_id, today.isoformat())

    result = urlfetch.fetch(url)

    if result.status_code == 200:
        forecasts = json.loads(result.content)
        forecast_day = ForecastDay.get_by(site, today, not_found_return_new = True)
        forecast_day.forecasts = Forecasts.from_json(forecasts['forecasts'])
        forecast_day.lastdata_datetime = parse_date(forecasts['lastdata_datetime']) if forecasts['lastdata_datetime'] is not None else None

        forecast_day.save()

    return Response(status = 204)