Example #1
0
def index(request):

    if request.method == "POST":
        form = SearchForm(request.POST)
        if form.is_valid():
            url=weather_url.format(request.POST['ciudad'])
            ciudad=request.POST['ciudad']
            toDel = Weather.objects.filter(zipcode=ciudad)
            toDel.delete()
            response = requests.get(url)

            json_response = json.loads(response.text)

            w= Weather(temperature=k_to_c(json_response["main"]["temp"]),zipcode=ciudad, description=json_response["weather"][0]["description"], sunrise= datetime.utcfromtimestamp(json_response["sys"]["sunrise"]), sunset= datetime.utcfromtimestamp(json_response["sys"]["sunset"]),wind= json_response["wind"]["speed"])
            w.save()
            #html="<html><body>%s y %s</body></html>"% (w.temperature, w.zipcode)
            context_dict = {}
            context_dict['temperature'] = k_to_c(json_response["main"]["temp"])
            context_dict['zipcode'] = ciudad
            context_dict['description'] = json_response["weather"][0]["description"]
            context_dict['sunrise']= datetime.utcfromtimestamp(json_response["sys"]["sunrise"])
            context_dict['sunset']= datetime.utcfromtimestamp(json_response["sys"]["sunset"])
            context_dict['wind']= json_response["wind"]["speed"]

            return render(request, 'weather/weather.html', context_dict)
    else:
        w= Weather.objects.all()
        context_dict = {'city': w,'form':SearchForm()}

        return render(request, 'weather/index.html', context_dict)
Example #2
0
 def weather_run():
     count=0
     for location in Location.objects.all():
         weather_base = 'http://api.wunderground.com/api/e1fde55153190b2a/geolookup/conditions/q/'
         url = weather_base + urllib2.quote(location.state_name) + '/' + urllib2.quote(location.city_name) + '.json'
         count += 1
         print count
         f = urllib2.urlopen(url)
         json_string = f.read()
         parsed_json = json.loads(json_string)
         if 'current_observation' in parsed_json:
             wind = parsed_json['current_observation']['precip_today_in']
             feels_like = parsed_json['current_observation']['feelslike_f']
             weather = parsed_json['current_observation']['weather']
             try:
                 float(parsed_json['current_observation']['precip_today_in'])
                 precipitation = float(parsed_json['current_observation']['precip_today_in'])
             except ValueError:
                 print 'Not string value'
             temperature = parsed_json['current_observation']['temp_f']
             try:
                 int(parsed_json['current_observation']['observation_epoch'])
                 datetime = time.strftime('%Y-%m-%d %H:%M:%S',time.gmtime(int(parsed_json['current_observation']['observation_epoch'])))
             except ValueError:
                 print 'Not Integer value'
             print wind, precipitation, temperature, weather, datetime
             weather = Weather(wind=wind, precipitation=precipitation, temperature=temperature, weather=weather,
                         feels_like=feels_like, datetime=datetime)
             weather.location_id = location.id
             weather.save()
Example #3
0
def create_weather(data):
    weather = Weather(
        city=data["city"],
        pm2_5=data["pm2_5"],
        temperature=data["temperature"],
        condition=unicode(data["condition"]),
        code=data["code"]
    )
    weather.save()
 def weather_run():
     count = 0
     for location in Location.objects.all():
         weather_base = 'http://api.wunderground.com/api/e1fde55153190b2a/geolookup/conditions/q/'
         url = weather_base + urllib2.quote(
             location.state_name) + '/' + urllib2.quote(
                 location.city_name) + '.json'
         count += 1
         print count
         f = urllib2.urlopen(url)
         json_string = f.read()
         parsed_json = json.loads(json_string)
         if 'current_observation' in parsed_json:
             wind = parsed_json['current_observation']['precip_today_in']
             feels_like = parsed_json['current_observation']['feelslike_f']
             weather = parsed_json['current_observation']['weather']
             try:
                 float(
                     parsed_json['current_observation']['precip_today_in'])
                 precipitation = float(
                     parsed_json['current_observation']['precip_today_in'])
             except ValueError:
                 print 'Not string value'
             temperature = parsed_json['current_observation']['temp_f']
             try:
                 int(parsed_json['current_observation']
                     ['observation_epoch'])
                 datetime = time.strftime(
                     '%Y-%m-%d %H:%M:%S',
                     time.gmtime(
                         int(parsed_json['current_observation']
                             ['observation_epoch'])))
             except ValueError:
                 print 'Not Integer value'
             print wind, precipitation, temperature, weather, datetime
             weather = Weather(wind=wind,
                               precipitation=precipitation,
                               temperature=temperature,
                               weather=weather,
                               feels_like=feels_like,
                               datetime=datetime)
             weather.location_id = location.id
             weather.save()
Example #5
0
def zipcode_post(request):
    #return HttpResponse("Hello Weather")
    if (request.method == "POST"):
        form = SearchForm(request.POST)
        if form.is_valid():
            url=weather_url.format(request.POST['ciudad'])
            response = requests.get(url)

            json_response = json.loads(response.text)

            w= Weather(temperature=k_to_c(json_response["main"]["temp"]),zipcode=zipcode, description=json_response["weather"][0]["description"], sunrise= json_response["sys"]["sunrise"], sunset= json_response["sys"]["sunset"],wind= json_response["wind"]["speed"])
            w.save()
            #html="<html><body>%s y %s</body></html>"% (w.temperature, w.zipcode)
            context_dict = {}
            context_dict['temperature'] = k_to_c(json_response["main"]["temp"])
            context_dict['zipcode'] = zipcode
            context_dict['description'] = json_response["weather"][0]["description"]
            context_dict['sunrise']= datetime.utcfromtimestamp(json_response["sys"]["sunrise"])
            context_dict['sunset']= datetime.utcfromtimestamp(json_response["sys"]["sunset"])
            context_dict['wind']= json_response["wind"]["speed"]

            return render(request, 'weather/weather.html', context_dict)