예제 #1
0
def weather_by_location(request, latitude, longitude):
    if request.method == 'GET':
        print(latitude, longitude)
        try:
            weather = YahooWeather(APP_ID=AppId,
                                   api_key=ClientId,
                                   api_secret=ClientSecret)
            weather.get_yahoo_weather_by_location(latitude, longitude)
        except:
            return Response(status=status.HTTP_404_NOT_FOUND)
        data = Weather(
            city=weather.location.city,
            temperature=weather.current_observation.condition.temperature,
            country=weather.location.country,
            wind_speed=weather.current_observation.wind.speed,
            humidity=weather.current_observation.atmosphere.humidity,
            pressure=weather.current_observation.atmosphere.pressure,
            condition=weather.current_observation.condition.text,
            pubDate=weather.current_observation.pubDate)
        w_serializer = WeatherSerializer(data)
        forcasts = []

        for forecast in weather.forecasts:
            newforecast = Forecast(day=forecast.day,
                                   weather=data,
                                   lowest_temp=forecast.low,
                                   highest_temp=forecast.high,
                                   condition=forecast.text)
            f_serializer = ForecastSerializer(newforecast)
            forcasts.append(f_serializer.data)
        return Response([w_serializer.data, forcasts])
예제 #2
0
def get_city_weather(city=None, lat=None, lon=None):

    data = YahooWeather(APP_ID=app_ID,
                        api_key=consumer_key,
                        api_secret=consumer_secret)
    if city:
        weather = data.get_yahoo_weather_by_city(city, Unit.celsius)
    else:
        weather = data.get_yahoo_weather_by_location(lat, lon, Unit.celsius)

    if weather:
        sunny = [32, 36]
        cloudy = [19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
        snowy = [5, 6, 7, 8, 9, 10, 14, 15, 16, 41, 42, 43, 46]
        stormy = [
            0, 1, 2, 3, 4, 11, 12, 13, 17, 18, 35, 37, 38, 39, 40, 45, 47
        ]
        supermoon = [31, 33, 34, 44]

        # Get the location
        location = data.get_location().__dict__
        # print(condition)

        # Get the condition
        condition = data.get_condition().__dict__
        # print(condition)

        # Get the forecasts
        forecasts = []
        for day in data.get_forecasts():
            forecasts.append(day.__dict__)
        # print(forecasts)

        wind = data.get_wind()
        # print(wind.__dict__)

        humidity = data.get_atmosphere().__dict__['humidity']

        astronomy = data.get_astronomy().__dict__

        return {
            'location': location,
            'condition': condition,
            'forecasts': forecasts,
            'wind': wind,
            'humidity': humidity,
            'astronomy': astronomy,
            # icones
            'sunny': sunny,
            'cloudy': cloudy,
            'snowy': snowy,
            'stormy': stormy,
            'supermoon': supermoon,
            'today': datetime.datetime.now().strftime("%A"),
        }

    else:
        return {'error': True, 'city': city}
예제 #3
0
class YahooWeatherAPI(WeatherAPIBase):
    DEFAULT_APP_ID = os.getenv('YAHOO_APP_ID', 'sJXMrh4i')
    DEFAULT_API_KEY = os.getenv('YAHOO_API_KEY', 'dj0yJmk9dkhYUlZnMkJjcWM2JmQ9WVdrOWMwcFlUWEpvTkdrbWNHbzlNQS0tJnM9Y29uc3VtZXJzZWNyZXQmc3Y9MCZ4PWE5')
    DEFAULT_API_SECRET = os.getenv('YAHOO_API_SECRET', 'd9bb6c4495d902332e235e498595046cb2c33c02')

    def __init__(self, app_id=None, api_key=None, api_secret=None):
        super(YahooWeatherAPI, self).__init__()

        self.app_id = app_id if app_id else self.DEFAULT_APP_ID
        self.api_key = api_key if api_key else self.DEFAULT_API_KEY
        self.api_secret = api_secret if api_secret else self.DEFAULT_API_SECRET

        self.data = YahooWeather(
            APP_ID=self.app_id,
            api_key=self.api_key,
            api_secret=self.api_secret
        )

    def get_weather_by_city(self, city, *args, **kwargs):
        self.data.get_yahoo_weather_by_city(city, Unit.celsius)
        return self._forecasts_to_condition(self.data.forecasts)

    @staticmethod
    def _forecasts_to_condition(forecasts):
        forecast_data = {
            i.date.date(): Condition(i.date.date(), i.text, i.low, i.high)
            for i in forecasts}
        return forecast_data

    def get_weather_by_location(self, lat, long, *args, **kwargs):
        self.data.get_yahoo_weather_by_location(lat, long)
        return self._forecasts_to_condition(self.data.forecasts)

    def forecast_to_text(self, condition: Condition):
        msg_tpl = "Weather of {date}:condition {condition};tempeture range {temp_low}-{temp_high} degree"
        msg = msg_tpl.format(
            date=condition.date,
            condition=condition.condition,
            temp_low=condition.low_temperature,
            temp_high=condition.high_temperature
        )
        return msg
예제 #4
0
class YahooWeatherAPI(WeatherAPIBase):
    DEFAULT_APP_ID = os.getenv('YAHOO_APP_ID', '')
    DEFAULT_API_KEY = os.getenv('YAHOO_API_KEY', '')
    DEFAULT_API_SECRET = os.getenv('YAHOO_API_SECRET', '')

    def __init__(self, app_id=None, api_key=None, api_secret=None):
        super(YahooWeatherAPI, self).__init__()

        self.app_id = app_id if app_id else self.DEFAULT_APP_ID
        self.api_key = api_key if api_key else self.DEFAULT_API_KEY
        self.api_secret = api_secret if api_secret else self.DEFAULT_API_SECRET

        self.data = YahooWeather(
            APP_ID=self.app_id,
            api_key=self.api_key,
            api_secret=self.api_secret
        )

    def get_weather_by_city(self, city, *args, **kwargs):
        self.data.get_yahoo_weather_by_city("tehran", Unit.celsius)
        return self._forecasts_to_condition(self.data.forecasts)

    @staticmethod
    def _forecasts_to_condition(forecasts):
        forecast_data = {
            i.date.date(): Condition(i.date.date(), i.text, i.low, i.high)
            for i in forecasts}
        return forecast_data

    def get_weather_by_location(self, lat, long, *args, **kwargs):
        self.data.get_yahoo_weather_by_location(lat, long)
        return self._forecasts_to_condition(self.data.forecasts)

    def forecast_to_text(self, condition: Condition):
        msg_tpl = "Weather of {date}:condition {condition};tempeture range {temp_low}-{temp_high} degree"
        msg = msg_tpl.format(
            date=condition.date,
            condition=condition.condition,
            temp_low=condition.low_temperature,
            temp_high=condition.high_temperature
        )
        return msg
예제 #5
0
from yahoo_weather.weather import YahooWeather
from yahoo_weather.config.units import Unit
weather = YahooWeather(APP_ID="", api_key="", api_secret="")

weather.get_yahoo_weather_by_city("Vellore", Unit.celsius)
print(weather.condition.text)
print(weather.condition.temperature)
print(weather.condition.code)
'''
weather.get_yahoo_weather_by_location(35.67194, 51.424438)
print(weather.condition.text)
print(weather.condition.temperature)
'''
weather_code = [0, 0, 0, 0, 0, 0, 0]
temperature = [0, 0, 0, 0, 0, 0, 0]
new_weather = weather.get_forecasts()
for i in range(0, 7):
    weather_code[i] = new_weather[i].code
    temperature[i] = new_weather[i].high
    #print(new_weather[i].text,new_weather[i].code,new_weather[i].high)

for i in range(0, 7):
    print(weather_code[i], temperature[i])
예제 #6
0
from yahoo_weather.weather import YahooWeather
from yahoo_weather.config.units import Unit

weather = YahooWeather(APP_ID="Your App ID",
                     api_key="Your API KEY",
                     api_secret="Your API secret")

weather.get_yahoo_weather_by_city("tehran", Unit.celsius)
print(weather.condition.text)
print(weather.condition.temperature)

weather.get_yahoo_weather_by_location(35.67194, 51.424438)
print(weather.condition.text)
print(weather.condition.temperature)