コード例 #1
0
    async def __get_weather_data(self):
        """
        Make weather data API request
        """
        async with aiohttp.ClientSession() as session:
            params = self.params
            city = params["city"]
            appId = params["appId"]
            units = params["units"]
            lang = params["lang"]

            try:
                async with session.get(
                    "https://api.openweathermap.org/data/2.5/weather?q={}&appid={}&units={}&lang={}".format(
                        city, appId, units, lang
                    )
                ) as response:
                    weatherResponse = await response.json()
                    if weatherResponse.get("cod") != 200:
                        raise WeatherDataException(weatherResponse.get("message"))

                    weather = Weather(weatherResponse)
                    weatherData = weather.get_data()

                    return weatherData
            except Exception as exc:
                # TODO: Log exceptions in api call
                raise WeatherDataException(
                    "Error fetching weather information: {}".format(str(exc))
                )
コード例 #2
0
    def check_sinoptik(self):
        """Check and process weather data from sinoptik.ua."""
        date = datetime.strftime(datetime.now(), "%Y-%m-%d")
        last_update = UpdateDay.objects.all().filter(id=1).first()
        if (last_update is None) or (last_update.day != date):
            weather = Sinoptik()
            if weather.load():
                weather.get_weather()

                weather_db = Weather.objects.all().filter(id=1).first()
                if weather_db is None:
                    weather_db = Weather(
                        today=weather.today_str(),
                        days=weather.days_str(),
                        water=weather.water_str(),
                        infoDaylight=weather.infoDaylight_str(),
                        warnings=weather.warnings,
                        description=weather.description)
                else:
                    weather_db.today = weather.today_str()
                    weather_db.days = weather.days_str()
                    weather_db.water = weather.water_str()
                    weather_db.infoDaylight = weather.infoDaylight_str()
                    weather_db.warnings = weather.warnings
                    weather_db.description = weather.description
                weather_db.save()

                if last_update is None:
                    last_update = UpdateDay(day=date)
                else:
                    last_update.day = date
                last_update.save()
コード例 #3
0
ファイル: views.py プロジェクト: falconchen/FetionWether
def update(request):
    action = 'update'
    #title = '更新'
    try :                
        phone_num = request.session['update_phone_num']
        u = User.objects.get(phone_num=phone_num)
        msg = {'phone_num':u.phone_num, 'fid':u.fid}    
        msg['area'] = u.wid
        msg['sub_type'] = User.TYPE_CHOICES[0][1] if u.sub_type == User.TYPE_CHOICES[0][0] else User.TYPE_CHOICES[1][1]
        t = loader.get_template('user_info.html')
        msg = t.render(Context({'msg':msg}))
        if request.method == 'POST':
            #post_args = request.POST
            #post_args['phone_num'] = phone_num
            form = UpdateSubForm(request.POST)
            if form.is_valid():
                cd = form.cleaned_data
                #执行更新逻辑
                                
                try:
                    weather = Weather.objects.get(cid=cd['city'], hour=cd['hour'])
                except Weather.DoesNotExist:
                    city = City.objects.get(cid=cd['city'])
                    weather = Weather(cid=city, hour=cd['hour'])
                    weather.save()
                
                #更改订阅类型
                u.wid = weather
                u.sub_type = cd['sub_type']
                u.save()
                
                request.session['announce'] = u'更改成功'
                Log(level=2,event=u'%s:%s' % (u.phone_num,request.session['announce'])).save()
                request.session['user'] = u
                #if not settings.DEBUG:   
                del request.session['update_phone_num']        
                return HttpResponseRedirect('/weather/announce/')
                                                
            #表单字段无效时
            else:
                pass
        else:
            default_hour = int(strftime('%H')) + 1
            form = UpdateSubForm(initial={'hour':default_hour, 'sub_type':'E'}) #定义字段的option默认值
        return render_to_response('index.html', {'form':form, 'msg':msg, 'action':action,'form_id':'subscribe'}, context_instance=RequestContext(request))
    except Exception, e:
        #print Exception
        #raise e
        raise Http404('非法请求')
コード例 #4
0
ファイル: conftest.py プロジェクト: gleivas/weather_project
def init_database():
    db.create_all()
    user1 = Weather(**weather_example())
    db.session.add(user1)
    db.session.commit()

    yield db

    db.drop_all()
コード例 #5
0
ファイル: views.py プロジェクト: falconchen/FetionWether
def index(request):
    msg = '请先填写手机号码和订阅信息, 天气短信将发送到你的手机。'
    if request.method == 'POST':
        form = SubscribeForm(request.POST)            
        if form.is_valid():
            fid = form.fid
            cd = form.cleaned_data
            
            
            #向天气订阅表增加记录或找出指定城市的订阅实例
            try:
                weather = Weather.objects.get(cid=cd['city'], hour=cd['hour'])
            except Weather.DoesNotExist:
                city = City.objects.get(cid=cd['city'])
                weather = Weather(cid=city, hour=cd['hour'])
                weather.save()
            
            #发送加为好友的短信
            ft = MyFetion(PHONE,PSW)
            add_res = ft.addfriend(cd['phone_num'], NICK, '2')
            if add_res:
                Log(level=2,event='success to send adding friend sms to %s' % cd['phone_num']).save()
            else:
                Log(level=1,event='failed to send adding friend sms to %s ,maybe he/she has been your friend.' % cd['phone_num']).save()

            #用户与天气订阅表关联,仅使用新增            
            user = User(fid=fid, phone_num=cd['phone_num'], wid=weather, sub_type=cd['sub_type'], active=True)
            user.save()            
            
            if add_res:
                msg = '你将收到一条"%s"的好友请求短信,请于24小时内确认,逾期无效' %  NICK
            else:
                msg = '订制成功'
            form = None
    
    else:    
        default_hour = int(strftime('%H')) + 1
        form = SubscribeForm(initial={'hour':default_hour, 'sub_type':'E'}) #定义字段的option默认值        
    return render_to_response('index.html', {'form':form, 'msg':msg, 'form_id':'subscribe'}, context_instance=RequestContext(request))
コード例 #6
0
ファイル: fetch_weather.py プロジェクト: namproctin/usec
    def handle(self, *args, **options):

        channel_layer = channels.layers.get_channel_layer()
        for location in options["location"]:
            res = requests.get(
                "http://wttr.in/%s?0" % location,
                headers={
                    "User-Agent":
                    "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36"
                },
            )
            if res.status_code == 200:
                try:
                    weather = Weather.objects.get(location=location)
                except Weather.DoesNotExist:
                    weather = Weather(location=location)
                data_start_index = res.text.find("<pre>")
                data_end_index = res.text.find("</pre>", data_start_index)
                if data_start_index < 0 or data_end_index < 0:
                    self.stdout.write(
                        self.style.ERROR(
                            'Failed fetch weather of location "%s"' %
                            location))
                    continue
                weather.data = res.text[data_start_index +
                                        len("<pre>"):data_end_index]
                weather.save()
                async_to_sync(channel_layer.group_send)("weather", {
                    "type": "weather_message",
                    "message": "update"
                })
            else:
                self.stdout.write(
                    self.style.ERROR('Failed fetch weather of location "%s"' %
                                     location))

            self.stdout.write(
                self.style.SUCCESS('Success fetch weather of location "%s"' %
                                   location))
コード例 #7
0
def save_request_data(data, city_code, from_date, to_date):
    all_data = json.loads(data.decode('utf-8')).get('data')
    get_data_information = False
    max_temp = None
    min_temp = None
    rain_forecast = None
    day_max_temp = None
    day_min_temp = None
    day_rain_forecast = None

    # A partir do momento que encontro a data de inicio começo a pegar as informacoes, na data final saio do loop
    for data in all_data:
        if data['date'] == from_date.strftime('%Y-%m-%d'):
            get_data_information = True

        if get_data_information:
            if not max_temp or int(data['temperature'].get('max')) > max_temp:
                max_temp = int(data['temperature'].get('max'))
                day_max_temp = data['date']
            if not min_temp or int(data['temperature'].get('min')) < min_temp:
                min_temp = int(data['temperature'].get('min'))
                day_min_temp = data['date']
            if not rain_forecast or int(
                    data['rain'].get('precipitation')) > rain_forecast:
                rain_forecast = int(data['rain'].get('precipitation'))
                day_rain_forecast = data['date']

        if data['date'] == to_date.strftime('%Y-%m-%d'):
            break

    # Converto as datas para datetime
    day_max_temp = datetime.strptime(day_max_temp, '%Y-%m-%d').date()
    day_min_temp = datetime.strptime(day_min_temp, '%Y-%m-%d').date()
    day_rain_forecast = datetime.strptime(day_rain_forecast, '%Y-%m-%d').date()

    # Salvo as informacoes no banco de dados
    weather = Weather(city_code=city_code,
                      from_date=from_date,
                      to_date=to_date,
                      day_max_temp=day_max_temp,
                      max_temp=max_temp,
                      day_min_temp=day_min_temp,
                      min_temp=min_temp,
                      day_rain_forecast=day_rain_forecast,
                      rain_forecast=rain_forecast)
    db.session.add(weather)
    db.session.commit()

    return weather
コード例 #8
0
def get_weather(city):
    response = requests.get(
        'https://api.openweathermap.org/data/2.5/weather?q=' + city +
        '&appid=c3afb7e24dedb09e3ca373a8cb64747d')
    json = response.json()
    weather = Weather()
    location = Location()
    weather.city_name = city
    location.longitude = json['coord']['lon']
    location.latitude = json['coord']['lat']
    location.timezone = json['timezone']
    weather.sunset = json['sys']['sunset']
    weather.sunrise = json['sys']['sunrise']
    weather.max_temp = json['main']['temp_max']
    weather.min_temp = json['main']['temp_min']
    weather.location = location
    return weather
コード例 #9
0
ファイル: mixins.py プロジェクト: fj-fj-fj/weather-api
    def _save_data_to_db(self) -> None:
        # due to a circular import
        from weather.models import Service, Weather

        recreate(db)
        existing_service = Service.query.filter(
            Service.url == self.model.get('url')).first()
        if not existing_service:
            db.session.add(Service(**self.model))
            db.session.commit()
        # REFACTOR: ~ db.session.bulk_insert_mappings(Weather, mappings)
        for i, weather in enumerate((v for v in self._parsed_data.values())):
            db.session.add(Weather(**weather))
            if i and i % 1000 == 0:
                db.session.commit()
        db.session.commit()
コード例 #10
0
ファイル: import.py プロジェクト: ekatosha/Weather_app
 def handle(self, *args, **options):
     cur_date = date.today()
     date_aggr = Weather.objects.order_by('-date').first()
     if cur_date > date_aggr.date:
         start_date = date_aggr.date + timedelta(1)
         end_date = cur_date
         for city in Cities.objects.all():
             r = requests.get(city.url.format(
                 start_date=start_date.strftime('%d.%m.%Y'),
                 end_date=end_date.strftime('%d.%m.%Y')),
                              allow_redirects=True)
             if r.status_code != 200:
                 continue
             open('test.gz', 'wb').write(r.content)
             with gzip.open('test.gz', 'rb') as f:
                 file_content = f.read()
             file = file_content.decode().split('\n')
             file[6] = file[6][:-1] + ';"space"\r'
             to_csv = ''.join(file[6:])
             with open('test.csv', 'w', newline='', encoding='utf-8') as f:
                 f.write(to_csv)
             update = pd.read_csv('test.csv', sep=';')
             del update['space']
             update['date'] = update.iloc[:, [0]].squeeze().str.split(
                 ' ').str.get(0)
             update['time'] = update.iloc[:, [0]].squeeze().str.split(
                 ' ').str.get(1)
             variable = []
             for index, row in update.iterrows():
                 row = row.dropna()
                 if 'RRR' not in row.keys(
                 ) or row['RRR'] == 'Осадков нет' or row[
                         'RRR'] == 'Следы осадков':
                     precipitation_mm = None
                 else:
                     precipitation_mm = row['RRR']
                 if 'W1' not in row.keys(
                 ) or not Precipitation.objects.filter(
                         precipitation=row['W1']).exists():
                     precipitation = None
                 else:
                     precipitation = Precipitation.objects.filter(
                         precipitation=row['W1']).first()
                 if 'DD' not in row.keys():
                     wind_direction = None
                 else:
                     wind_direction = Wind.objects.filter(
                         wind_full_name=row['DD']).first()
                 if 'Ff' not in row.keys():
                     wind_speed = None
                 else:
                     wind_speed = row['Ff']
                 if 'T' not in row.keys():
                     temperature = None
                 else:
                     temperature = row['T']
                 dat = datetime.strptime(row['date'], '%d.%m.%Y').date()
                 variable.append(
                     Weather(
                         **{
                             'date': dat,
                             'time': row['time'],
                             'city': city,
                             'temperature': temperature,
                             'wind_direction': wind_direction,
                             'wind_speed': wind_speed,
                             'precipitation_mm': precipitation_mm,
                             'precipitation': precipitation
                         }))
             Weather.objects.bulk_create(variable)
コード例 #11
0
def add_new_weather(currentCity,pm25,data,Picture,the_weather,temperature,today_or_future=1):
    """
    currentCity:城市名
    pm25:PM25值
    data:日期
    Picture:图片
    the_weather:天气
    temperature:温度
    today_or_future=1:今天还是未来(今天为:1,未来为:2),默认为今天
    :return:
    """
    weather = Weather()  # 实例化新的天气
    weather.currentCity = currentCity
    weather.pm25 = pm25
    weather.data = data
    weather.Picture = Picture
    weather.the_weather = the_weather
    weather.temperature = temperature
    weather.today_or_future = today_or_future
    weather.save()
コード例 #12
0
ファイル: conftest.py プロジェクト: gleivas/weather_project
def new_weather():
    user = Weather(**weather_example())
    return user
コード例 #13
0
for i in range(0,40):
    a.append(data["list"][i]["dt_txt"])
    b.append(data["list"][i]["weather"][0]["description"])
    c.append(k2c(data["list"][i]["main"]["temp_min"]))
    d.append(k2c(data["list"][i]["main"]["temp_max"]))
    e.append(data["list"][i]["main"]["humidity"])
    f.append(data["list"][i]["main"]["pressure"])
    g.append(data["list"][i]["wind"]["deg"])
    h.append(data["list"][i]["wind"]["speed"])

for item in zip(a, b, c, d, e, f, g, h):
    Weather(
        date=item[0],
        desc=item[1],
        temp_min=item[2],
        temp_max=item[3],
        humidity=item[4],
        pressure=item[5],
        deg=item[6],
        speed=item[7]
    ).save()



for i in range(0,40):
    print("+ 도시 =", data["city"]["name"])
    print("| 날짜 =", data["list"][i]["dt_txt"])
    print("| 날씨 =", data["list"][i]["weather"][0]["description"])
    print("| 최저 기온 =", k2c(data["list"][i]["main"]["temp_min"]))
    print("| 최고 기온 =", k2c(data["list"][i]["main"]["temp_max"]))
    print("| 습도 =", data["list"][i]["main"]["humidity"])
    print("| 기압 =", data["list"][i]["main"]["pressure"])