def get_weather_forecasted(request):    
    country = ''
    postal_code = ''
    address_locality = ''
    
    query = request.args.get('q')
    
    if not query:
      return Response(json.dumps([]), mimetype='application/json')
    
    tokens  = query.split(';')
    for token in tokens:
      items = token.split(':')
      if items[0] == 'postalCode':
        postal_code = items[1]
      elif items[0] == 'country':
        country = items[1]
      elif items[0] == 'addressLocality':
        address_locality = items[1]
        
    if country == 'PT' and address_locality:
      return Response(json.dumps(get_weather_forecasted_pt(address_locality)), mimetype='application/json')
    
    if not country or not postal_code or not postal_code in postal_codes or country <> 'ES':
      return Response(json.dumps([]), mimetype='application/json')
    
    source = aemet_service.format(postal_codes[postal_code])
    req = urllib2.Request(url=source)
    f = urllib2.urlopen(req)
    xml_data = f.read()
    DOMTree = xml.dom.minidom.parseString(xml_data).documentElement
    
    address_locality = DOMTree.getElementsByTagName('nombre')[0].firstChild.nodeValue
    address = { }
    address['addressCountry'] = country
    address['postalCode'] = postal_code
    address['addressLocality'] = address_locality
    
    created =  DOMTree.getElementsByTagName('elaborado')[0].firstChild.nodeValue
    
    forecasts = DOMTree.getElementsByTagName('prediccion')[0].getElementsByTagName('dia')
    
    out = []
    for forecast in forecasts:
      date = forecast.getAttribute('fecha')
      normalizedForecast = parse_aemet_forecast(forecast, date)
      counter = 1
      for f in normalizedForecast:
        f['type'] = 'WeatherForecast'
        f['id'] = generate_id(postal_code, country, date) + '_' + str(counter)
        f['address'] = address
        f['created'] = created
        f['source'] = source
        counter+=1
        out.append(f)
    
    return Response(json.dumps(out), mimetype='application/json')
Exemple #2
0
def get_weather_forecasted(request):
    country = ''
    postal_code = ''
    address_locality = ''

    query = request.args.get('q')

    if not query:
        return Response(json.dumps([]), mimetype='application/json')

    tokens = query.split(';')
    for token in tokens:
        items = token.split(':')
        if items[0] == 'postalCode':
            postal_code = items[1]
        elif items[0] == 'country':
            country = items[1]
        elif items[0] == 'addressLocality':
            address_locality = items[1]

    if country == 'PT' and address_locality:
        return Response(json.dumps(
            get_weather_forecasted_pt(address_locality)),
                        mimetype='application/json')

    if not country or (not postal_code in postal_codes and
                       not address_locality in localities) or country <> 'ES':
        return Response(json.dumps([]), mimetype='application/json')

    param = ''
    if postal_code:
        param = postal_codes[postal_code]
    elif address_locality:
        param = localities[address_locality]

    source = aemet_service.format(param)
    req = urllib2.Request(url=source)
    f = urllib2.urlopen(req)
    xml_data = f.read()
    DOMTree = xml.dom.minidom.parseString(xml_data).documentElement

    address_locality = DOMTree.getElementsByTagName(
        'nombre')[0].firstChild.nodeValue
    address = {}
    address['addressCountry'] = country
    address['postalCode'] = postal_code
    address['addressLocality'] = address_locality

    created = DOMTree.getElementsByTagName('elaborado')[0].firstChild.nodeValue

    forecasts = DOMTree.getElementsByTagName(
        'prediccion')[0].getElementsByTagName('dia')

    out = []
    for forecast in forecasts:
        date = forecast.getAttribute('fecha')
        normalizedForecast = parse_aemet_forecast(forecast, date)
        counter = 1
        for f in normalizedForecast:
            f['type'] = 'WeatherForecast'
            f['id'] = generate_id(postal_code, country,
                                  date) + '_' + str(counter)
            f['address'] = address
            f['dateCreated'] = created
            f['source'] = source
            counter += 1
            out.append(f)

    return Response(json.dumps(out), mimetype='application/json')