Example #1
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()
Example #2
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))