def weather_in_city(request, cityname): if request.method == 'GET': city_name = "tehran" try: weather = YahooWeather(APP_ID=AppId, api_key=ClientId, api_secret=ClientSecret) weather.get_yahoo_weather_by_city(cityname) 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])
def weather_setup(): weather_access = YahooWeather(APP_ID="YourID", api_key="YourKey", api_secret="YourSecret") weather_access.get_yahoo_weather_by_city("Erp", Unit.celsius) weather = weather_access.condition.text weather_temp = weather_access.condition.temperature return weather, weather_temp
def __init__(self, city): self.city = city weather = YahooWeather(APP_ID="Your App ID", api_key="Your API KEY", api_secret="Your API secret") weather.get_yahoo_weather_by_city(self.city) self.temperature = weather.condition.temperature self.weather_conditions = weather.condition.text self.wind_speed = weather.wind.speed
class WeatherService: def __init__(self): self.weather_service = YahooWeather(APP_ID=config.YAHOO_CLIENT_ID, api_key=config.YAHOO_CLIENT_KEY, api_secret=config.YAHOO_CLIENT_SECRET) def getWeatherByCity(self, city): self.weather_service.get_yahoo_weather_by_city(city, Unit.celsius) cond = config.WEATHER_CONSTANTS[str(self.weather_service.condition.code)] temp = self.weather_service.condition.temperature return cond, temp def getForecastsByCity(self, city): self.weather_service.get_yahoo_weather_by_city(city, Unit.celsius) return self.weather_service.forecasts
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}
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
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
#!/usr/bin/env python3 from yahoo_weather.weather import YahooWeather from yahoo_weather.config.units import Unit def read_private_data(filename: str): with open(f"private/{filename}", "r") as f: return f.readline().strip() app_id = read_private_data("app_id") api_key = read_private_data("client_id") api_secret = read_private_data("client_secret") data = YahooWeather(APP_ID=app_id, api_key=api_key, api_secret=api_secret) data.get_yahoo_weather_by_city("Stockholm", Unit.celsius) print(data.condition.text) print(data.condition.temperature)
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])
# This the example of using external module in Python # Yahoo Weather module is invoked # pip install yahoo-weather # Yahoo APP_ID is required (can be ordered here https://developer.yahoo.com/weather/?guccounter=1) from yahoo_weather.weather import YahooWeather from yahoo_weather.config.units import Unit data = YahooWeather(APP_ID="ElssVX7a", api_key="dj0yJmk9UWFZWjQ2RGNhY3BIJmQ9WVdrOVJXeHpjMVpZTjJFbWNHbzlNQS0tJnM9Y29uc3VtZXJzZWNyZXQmc3Y9MCZ4PWE0", api_secret="6e29d9f7fa9dd3c56e7a0319ecd0e51918e2f450") print('Welcome to Yahoo Weather!') print('*************************') print('Input city name (e.g. Chisinau):') city = input() data.get_yahoo_weather_by_city(city, Unit.celsius) print('Condition:',data.condition.text) print('Temperature, Celcius:',data.condition.temperature) print('Atmopshere pressure:',data.atmosphere.pressure) print('Atmosphere humidity:',data.atmosphere.humidity)
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)
import os from yahoo_weather.weather import YahooWeather from yahoo_weather.config.units import Unit from dotenv import load_dotenv load_dotenv() print("Testing Weather...") my_id = os.getenv('YAHOO_APP_ID') my_key = os.getenv('YAHOO_API_KEY') my_secret = os.getenv('YAHOO_API_SECRET') city_name = "Glasgow" weather = YahooWeather(APP_ID=my_id, api_key=my_key, api_secret=my_secret) weather.get_yahoo_weather_by_city(city_name, Unit.celsius) print(city_name) print(weather.condition.text) print(weather.condition.temperature) print("End Weather Test.")
""".format(API_TOKEN='dff', CHAT_ID='-352331363', TEXT='Kukusiki')) urllib.request.urlopen(""" https://api.telegram.org/bot{API_TOKEN}/sendMessage?chat_id={CHAT_ID}&text={TEXT} """.format(API_TOKEN='token', CHAT_ID='-352331363', TEXT='Pora delat` rabochiy vid, parni')) dateHome = datetime.now().strftime('%H:%M:%S') home = '18:00:00' format = '%H:%M:%S' time = datetime.strptime(home, format) - datetime.strptime( dateHome, format) urllib.request.urlopen(""" https://api.telegram.org/bot{API_TOKEN}/sendMessage?chat_id={CHAT_ID}&text={TEXT} """.format(API_TOKEN='token', CHAT_ID='-352331363', TEXT="Do kontsa rabochego dnia ostalos` {}".format( str(time)))) data.get_yahoo_weather_by_city("Kyiv", Unit.celsius) urllib.request.urlopen(""" https://api.telegram.org/bot{API_TOKEN}/sendMessage?chat_id={CHAT_ID}&text={TEXT} """.format(API_TOKEN='token', CHAT_ID='-352331363', TEXT="Temperatura za bortom: {}".format( str(data.condition.temperature)))) break connection.close()
class GraphicsTest(SampleBase): def __init__(self, *args, **kwargs): super(GraphicsTest, self).__init__(*args, **kwargs) self.refresh_weather() def refresh_weather(self): self.philly_weather = YahooWeather(APP_ID="RVSaie5a", api_key="my_key", api_secret="my_secret") self.philly_weather.get_yahoo_weather_by_city("philadelphia", Unit.fahrenheit) print("Updated weather") def run(self): rotation = 0 offscreen_canvas = self.matrix.CreateFrameCanvas() #canvas = self.matrix font = graphics.Font() font.LoadFont("../../../fonts/5x8.bdf") weather_font = graphics.Font() weather_font.LoadFont("../../../fonts/4x6.bdf") green = graphics.Color(0, 255, 0) #graphics.DrawCircle(canvas, 15, 15, 10, green) blue = graphics.Color(0, 255, 213) yellow = graphics.Color(255, 255, 0) client = LiveSportsClient(api_key='my_key') scoreboard_index = 0 client.set_todays_nba_games_list() length_of_games_today = len(client.games_today) home_score = '' away_score = '' final = '' while True: home_score, away_score, final = client.get_scoreboard_for_todays_game( client.games_today[scoreboard_index]).get_scoreboard() #home_score = "PHI 32" #away_score = "MIN 22" #final = "FINAL" if rotation % 2 == 0: graphics.DrawText(offscreen_canvas, font, 2, 9, blue, home_score) graphics.DrawText(offscreen_canvas, font, 2, 20, blue, away_score) graphics.DrawText(offscreen_canvas, font, 2, 30, blue, final) else: #home_score = "HHI 32" #away_score = "NNN 22" #final = "INALF" graphics.DrawText(offscreen_canvas, font, 2, 9, green, home_score) graphics.DrawText(offscreen_canvas, font, 2, 20, green, away_score) graphics.DrawText(offscreen_canvas, font, 2, 30, green, final) graphics.DrawText(offscreen_canvas, weather_font, 40, 6, yellow, self.philly_weather.condition.text) graphics.DrawText(offscreen_canvas, weather_font, 55, 13, yellow, str(self.philly_weather.condition.temperature)) scoreboard_index = scoreboard_index + 1 if scoreboard_index == length_of_games_today: scoreboard_index = 0 rotation = rotation + 1 if rotation == 90000: rotation = 0 offscreen_canvas = self.matrix.SwapOnVSync(offscreen_canvas) offscreen_canvas = self.matrix.CreateFrameCanvas() time.sleep(5) home_score = '' away_score = '' final = ''
from yahoo_weather.classes.astronomy import Astronomy from yahoo_weather.classes.condition import Condition from yahoo_weather.classes.current_observation import Current_Observation from yahoo_weather.classes.current_weather import Current_Weather from yahoo_weather.classes.forecasts import Forecasts from yahoo_weather.classes.location import Location from yahoo_weather.classes.wind import Wind from yahoo_weather.config.units import Unit data = YahooWeather( APP_ID="7QMMCcOS", api_key= "dj0yJmk9MlFpQTFrQnNYamx4JmQ9WVdrOU4xRk5UVU5qVDFNbWNHbzlNQT09JnM9Y29uc3VtZXJzZWNyZXQmc3Y9MCZ4PTU5", api_secret="28ba0fc7c529f17ae61f9b6cd83985ec200a4db2") data.get_yahoo_weather_by_city("vinh", Unit.celsius) # data.get_yahoo_weather_by_location(35.67194, 51.424438) #location print(data.location.city) print(data.location.region) print(data.location.country) print(data.location.lat) print(data.location.long) print(data.location.timezone_id) print("------------------------") print(data.wind.chill) print(data.wind.direction) print(data.wind.speed) print("------------------------") print(data.atmosphere.humidity) print(data.atmosphere.visibility)
def get_weather(): data = YahooWeather( APP_ID="YHtM6G4m", api_key="dj0yJmk9akZCYUNvMktDYVBKJmQ9WVdrOVdVaDBUVFpITkcwbWN" "HbzlNQS0tJnM9Y29uc3VtZXJzZWNyZXQmc3Y9MCZ4PWYz", api_secret="4ba0d4a13d3f3c8da7fa13a96de81c027feded92") data.get_yahoo_weather_by_city("beijing", Unit.celsius) weather_cn = translator.translate(data.condition.text, dest='zh-CN').text if weather_cn == '明确': weather_cn = '晴朗' weather_beijing = "北京: " + str( data.condition.temperature) + "度 " + str(weather_cn) data.get_yahoo_weather_by_city("sheffield", Unit.celsius) weather_cn = translator.translate(data.condition.text, dest='zh-CN').text if weather_cn == '明确': weather_cn = '晴朗' weather_sheffield = "谢菲尔德: " + str( data.condition.temperature) + "度 " + str(weather_cn) data.get_yahoo_weather_by_city("Nottingham", Unit.celsius) weather_cn = translator.translate(data.condition.text, dest='zh-CN').text if weather_cn == '明确': weather_cn = '晴朗' weather_Nottingham = "诺丁汉: " + str( data.condition.temperature) + "度 " + str(weather_cn) data.get_yahoo_weather_by_city("tokyo", Unit.celsius) weather_cn = translator.translate(data.condition.text, dest='zh-CN').text if weather_cn == '明确': weather_cn = '晴朗' weather_tokyo = "东京: " + str( data.condition.temperature) + "度 " + str(weather_cn) data.get_yahoo_weather_by_city("boston", Unit.celsius) weather_cn = translator.translate(data.condition.text, dest='zh-CN').text if weather_cn == '明确': weather_cn = '晴朗' weather_boston = "波士顿: " + str( data.condition.temperature) + "度 " + str(weather_cn) data.get_yahoo_weather_by_city("anshun", Unit.celsius) weather_cn = translator.translate(data.condition.text, dest='zh-CN').text if weather_cn == '明确': weather_cn = '晴朗' weather_anshun = "安顺: " + str( data.condition.temperature) + "度 " + str(weather_cn) data.get_yahoo_weather_by_city("chengdu", Unit.celsius) weather_cn = translator.translate(data.condition.text, dest='zh-CN').text if weather_cn == '明确': weather_cn = '晴朗' weather_chengdu = "成都: " + str( data.condition.temperature) + "度 " + str(weather_cn) data.get_yahoo_weather_by_city("jinan", Unit.celsius) weather_cn = translator.translate(data.condition.text, dest='zh-CN').text if weather_cn == '明确': weather_cn = '晴朗' weather_jinan = "济南: " + str( data.condition.temperature) + "度 " + str(weather_cn) data.get_yahoo_weather_by_city("shanghai", Unit.celsius) weather_cn = translator.translate(data.condition.text, dest='zh-CN').text if weather_cn == '明确': weather_cn = '晴朗' weather_shanghai = "上海: " + str( data.condition.temperature) + "度 " + str(weather_cn) data.get_yahoo_weather_by_city("guangzhou", Unit.celsius) weather_cn = translator.translate(data.condition.text, dest='zh-CN').text if weather_cn == '明确': weather_cn = '晴朗' weather_guangzhou = "广州: " + str( data.condition.temperature) + "度 " + str(weather_cn) data.get_yahoo_weather_by_city("quanzhou", Unit.celsius) weather_cn = translator.translate(data.condition.text, dest='zh-CN').text if weather_cn == '明确': weather_cn = '晴朗' weather_quanzhou = "泉州: " + str( data.condition.temperature) + "度 " + str(weather_cn) weather = weather_beijing + '\n' + weather_sheffield + '\n' + weather_Nottingham + '\n'+ weather_tokyo + '\n'\ + weather_boston + '\n' + weather_anshun + '\n' + weather_chengdu + '\n' + weather_jinan + '\n'\ + weather_shanghai + '\n' + weather_guangzhou + '\n' + weather_quanzhou return weather print(weather)