added weather api caller

This commit is contained in:
Neythen
2021-06-02 21:16:15 +01:00
parent aeda550210
commit a1dd720093
10 changed files with 204 additions and 54 deletions

View File

@@ -176,9 +176,6 @@ def updateStockPricesIEX(symbols):
CSV.write(symbol + ',' + str(current_prices[i]) + ',' + str(opening_prices[i]) + '\n')
CSV.close()
def updateCrypto(coins, coin_info, unique_bases):
response = coingecko_client.get_price(ids=','.join(coins), vs_currencies = unique_bases, include_24hr_change=True)
@@ -222,7 +219,49 @@ def updateNews():
CSV.close()
def updateWeather(location, api_key):
url = "https://api.openweathermap.org/data/2.5/weather?q={}&units=metric&appid={}".format(location, api_key)
r = requests.get(url)
weather = r.json()
current_weather = {}
coords = weather['coord']
lat = coords['lat']
lon = coords['lon']
url = 'https://api.openweathermap.org/data/2.5/onecall?lat={}&units=metric&lon={}&appid={}'.format(lat, lon, api_key)
r = requests.get(url)
current_weather['main_weather'] = weather['weather'][0]['main']
current_weather['description'] = weather['weather'][0]['description']
current_weather['temp'] = weather['main']['temp']
current_weather['min_temp'] = weather['main']['temp_min']
current_weather['max_temp'] = weather['main']['temp_max']
current_weather['feels_like'] = weather['main']['feels_like']
current_weather['humidity'] = weather['main']['humidity']
current_weather['clouds'] = weather['clouds']['all']
current_weather['wind_speed'] = weather['wind']['speed']
current_weather['wind_direction'] = weather['wind']['deg']
current_weather['visibility'] = weather['visibility']
current_weather['uv'] = r.json()['current']['uvi']
json.dump( current_weather, open( "csv/current_weather.json", 'w+' ))
daily_weather = []
daily = r.json()['daily']
for day in daily:
dct = {}
dct['main_weather'] = day['weather'][0]['main']
dct['description'] = day['weather'][0]['description']
dct['min_temp'] = day['temp']['min']
dct['min_temp'] = day['temp']['max']
daily_weather.append(dct)
json.dump( daily_weather, open( "csv/daily_weather.json", 'w+' ))
if __name__ == '__main__':
newsapi = NewsApiClient(api_key='cf08652bd17647b89aaf469a1a8198a9')
@@ -230,7 +269,11 @@ if __name__ == '__main__':
finnhubAPIkey = "c24qddqad3ickpckgg80" #Finnhub
finnhubsandboxAPIkey = "sandbox_c24qddqad3ickpckgg8g" #Finnhub
sleeptime = 2 #minutes
stock_time = 2 #minutes
news_time = 30 #minutes
weather_time = 10 #minutes
# TODO: different update times for stocks, weather and news
finnhubClient = finnhub.Client(api_key=finnhubAPIkey)
max_stocks = 200
@@ -238,12 +281,9 @@ if __name__ == '__main__':
iexSandboxAPIkey = 'Tpk_0078dff413ef4f979137f7111452dc4b'
#updateStockPricesIEX(symbols)
finnhubClient = finnhub.Client(api_key=finnhubAPIkey)
coingecko_client = CoinGeckoAPI()
NY_zone = pytz.timezone('America/New_York')
@@ -258,6 +298,10 @@ if __name__ == '__main__':
updateStockPrices(symbols)
updateUpdate(NY_time)
coins, coin_info, unique_bases = readCryptoCSV('csv/crypto.csv', max_stocks)
weather_location, weather_key = 'London', 'bd5d5096a5ba30bbcfb57ead42ab3fee'
updateWeather(weather_location, weather_key)
updateCrypto(coins, coin_info, unique_bases)
@@ -271,7 +315,7 @@ if __name__ == '__main__':
while True:
msg = getInput()
if msg == 'R' or time.time() - t > sleeptime*60:
if msg == 'R' or time.time() - t > stock_time*60:
@@ -281,6 +325,7 @@ if __name__ == '__main__':
updateCrypto(coins, coin_info, unique_bases)
updateNews()
updateWeather(weather_location, weather_key)
NY_time = datetime.now(NY_zone)
symbols, stock_info = readCSV('csv/tickers.csv', max_stocks)