Пример #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
def init_database():
    db.create_all()
    user1 = Weather(**weather_example())
    db.session.add(user1)
    db.session.commit()

    yield db

    db.drop_all()
Пример #4
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
Пример #5
0
    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()
Пример #6
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
Пример #7
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()
Пример #8
0
    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))
Пример #9
0
 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)
Пример #10
0
def new_weather():
    user = Weather(**weather_example())
    return user
Пример #11
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"])