added weather api caller
This commit is contained in:
parent
aeda550210
commit
a1dd720093
@ -177,9 +177,6 @@ def updateStockPricesIEX(symbols):
|
|||||||
CSV.close()
|
CSV.close()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def updateCrypto(coins, coin_info, unique_bases):
|
def updateCrypto(coins, coin_info, unique_bases):
|
||||||
response = coingecko_client.get_price(ids=','.join(coins), vs_currencies = unique_bases, include_24hr_change=True)
|
response = coingecko_client.get_price(ids=','.join(coins), vs_currencies = unique_bases, include_24hr_change=True)
|
||||||
|
|
||||||
@ -222,7 +219,49 @@ def updateNews():
|
|||||||
|
|
||||||
CSV.close()
|
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__':
|
if __name__ == '__main__':
|
||||||
newsapi = NewsApiClient(api_key='cf08652bd17647b89aaf469a1a8198a9')
|
newsapi = NewsApiClient(api_key='cf08652bd17647b89aaf469a1a8198a9')
|
||||||
@ -230,7 +269,11 @@ if __name__ == '__main__':
|
|||||||
|
|
||||||
finnhubAPIkey = "c24qddqad3ickpckgg80" #Finnhub
|
finnhubAPIkey = "c24qddqad3ickpckgg80" #Finnhub
|
||||||
finnhubsandboxAPIkey = "sandbox_c24qddqad3ickpckgg8g" #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)
|
finnhubClient = finnhub.Client(api_key=finnhubAPIkey)
|
||||||
max_stocks = 200
|
max_stocks = 200
|
||||||
|
|
||||||
@ -239,11 +282,8 @@ if __name__ == '__main__':
|
|||||||
|
|
||||||
#updateStockPricesIEX(symbols)
|
#updateStockPricesIEX(symbols)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
finnhubClient = finnhub.Client(api_key=finnhubAPIkey)
|
finnhubClient = finnhub.Client(api_key=finnhubAPIkey)
|
||||||
|
|
||||||
|
|
||||||
coingecko_client = CoinGeckoAPI()
|
coingecko_client = CoinGeckoAPI()
|
||||||
|
|
||||||
NY_zone = pytz.timezone('America/New_York')
|
NY_zone = pytz.timezone('America/New_York')
|
||||||
@ -259,6 +299,10 @@ if __name__ == '__main__':
|
|||||||
updateUpdate(NY_time)
|
updateUpdate(NY_time)
|
||||||
coins, coin_info, unique_bases = readCryptoCSV('csv/crypto.csv', max_stocks)
|
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)
|
updateCrypto(coins, coin_info, unique_bases)
|
||||||
|
|
||||||
updateNews()
|
updateNews()
|
||||||
@ -271,7 +315,7 @@ if __name__ == '__main__':
|
|||||||
while True:
|
while True:
|
||||||
|
|
||||||
msg = getInput()
|
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)
|
updateCrypto(coins, coin_info, unique_bases)
|
||||||
|
|
||||||
updateNews()
|
updateNews()
|
||||||
|
updateWeather(weather_location, weather_key)
|
||||||
|
|
||||||
NY_time = datetime.now(NY_zone)
|
NY_time = datetime.now(NY_zone)
|
||||||
symbols, stock_info = readCSV('csv/tickers.csv', max_stocks)
|
symbols, stock_info = readCSV('csv/tickers.csv', max_stocks)
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
symbol,name,base,current,24hr change
|
symbol,name,base,current,24hr change
|
||||||
BTC,bitcoin,usd,38140,5.220972287309966
|
BTC,bitcoin,usd,37943,5.398882470458167
|
||||||
ETH,ethereum,gbp,1966.44,8.603614342494295
|
ETH,ethereum,gbp,1950.25,8.111712194442813
|
||||||
|
|
1
csv/current_weather.json
Normal file
1
csv/current_weather.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
{"main_weather": "Clouds", "description": "overcast clouds", "temp": 19.95, "min_temp": 17.32, "max_temp": 21.2, "feels_like": 19.54, "humidity": 59, "clouds": 96, "wind_speed": 1.31, "wind_direction": 113, "visibility": 10000, "uv": 0}
|
1
csv/daily_weather.json
Normal file
1
csv/daily_weather.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
[{"main_weather": "Rain", "description": "light rain", "min_temp": 27.04}, {"main_weather": "Clouds", "description": "scattered clouds", "min_temp": 24.89}, {"main_weather": "Rain", "description": "moderate rain", "min_temp": 16.33}, {"main_weather": "Clear", "description": "clear sky", "min_temp": 22.03}, {"main_weather": "Rain", "description": "light rain", "min_temp": 21.89}, {"main_weather": "Rain", "description": "light rain", "min_temp": 24.13}, {"main_weather": "Rain", "description": "light rain", "min_temp": 25.2}, {"main_weather": "Rain", "description": "light rain", "min_temp": 25.55}]
|
@ -1 +1 @@
|
|||||||
02/06/2021 11:35:50
|
02/06/2021 16:08:56
|
||||||
|
|
40
csv/news.csv
40
csv/news.csv
@ -1,21 +1,21 @@
|
|||||||
headline,source,date,time
|
headline,source,date,time
|
||||||
First human case of H10N3 bird flu in China: These are the symptoms - MARCA.com,Marca,2021-06-02,14:25:39Z
|
LKL finalo serijos starte – užtikrinta „Žalgirio“ pergalė - Lrytas.lt,Lrytas.lt,2021-06-02,18:41:12Z
|
||||||
Canadarm2 damaged by ‘lucky strike’ from space junk in orbit - Global News,Global News,2021-06-02,14:21:44Z
|
Israeli Opposition Races Against Deadline to Oust Netanyahu: Live Updates - The New York Times,New York Times,2021-06-02,18:36:55Z
|
||||||
Coronavirus latest news: Matt Hancock warns vaccine fake news is a ‘deadly threat’ - Telegraph.co.uk,Telegraph.co.uk,2021-06-02,14:20:59Z
|
Help with roads^ rivers and rubble a priority as farmers take stock of flood devastation - Stuff.co.nz,Stuff.co.nz,2021-06-02,18:23:00Z
|
||||||
Space Debris Damages Robotic Arm Attached to the International Space Station - PCMag AU,PCMag.com,2021-06-02,14:09:11Z
|
ICFO: Spain overtakes China in one of the biggest quantum communication problems | Science - Sunday Vision,Sundayvision.co.ug,2021-06-02,18:15:04Z
|
||||||
Big TSX companies tripled number of Black board members in past year — but there are still only 26 - CBC.ca,CBC News,2021-06-02,14:05:55Z
|
Joel Embiid (knee) will not play in Game 5 - NBA.com,Heat.com,2021-06-02,18:00:00Z
|
||||||
Huawei officially launches Android alternative HarmonyOS for smartphones - Yahoo Finance Australia,Yahoo Entertainment,2021-06-02,14:05:17Z
|
Elephant herd razes 500-kilometer path of destruction after escape from China nature reserve - CNN ,CNN,2021-06-02,17:58:00Z
|
||||||
Space junk slams into International Space Station^ leaving hole in robotic arm - CBS News,CBS News,2021-06-02,14:03:59Z
|
President Biden News: Live Updates - The New York Times,New York Times,2021-06-02,17:57:48Z
|
||||||
COVID-19: Three-quarters of adults in UK have now had first vaccine dose - Sky News,Sky.com,2021-06-02,14:03:45Z
|
Lack of papal apology for Canada indigenous schools abuses 'shameful' - minister - GMA News,GMA News,2021-06-02,17:45:42Z
|
||||||
Dalian Atkinson: PC's fear 'through the roof' during arrest - BBC News,BBC News,2021-06-02,14:02:38Z
|
No Time To Die Singer Billie Eilish Rocks Blonde Locks In Lost Cause Video - CinemaBlend,CinemaBlend,2021-06-02,17:45:39Z
|
||||||
Dow Jones Today Targets 5th Advance As Stocks Inch Up; DXC Upgraded; BorgWarner Nails Hyundai EV Deal - Investor's Business Daily,Investor's Business Daily,2021-06-02,14:00:00Z
|
Jaguar F-Type available only with V8 engine for 2022 - Driving,Driving,2021-06-02,17:42:02Z
|
||||||
Ford^ Lecce expected to make announcement about Ontario school reopening plan - CBC.ca,CBC News,2021-06-02,13:52:35Z
|
Premier Doug Ford announces no in-class learning until September - Toronto Star,Toronto Star,2021-06-02,17:37:30Z
|
||||||
Huawei MatePad Pro 12.6 in for review - GSMArena.com news - GSMArena.com,GSMArena.com,2021-06-02,13:52:01Z
|
Cricket: Devon Conway shines on debut as Black Caps dominate day one of first test against England at Lord's - New Zealand Herald,New Zealand Herald,2021-06-02,17:35:00Z
|
||||||
Everton set to begin talks with Nuno Espirito Santo over vacant manager role - Sky Sports,Sky Sports,2021-06-02,13:42:25Z
|
Atmospheric metal layers appear with surprising regularity - Science Daily,Science Daily,2021-06-02,17:31:46Z
|
||||||
2021's first solar eclipse will show off a 'ring of fire' in the sky - CNET,CNET,2021-06-02,13:36:57Z
|
Kevin Lunney attackers broke his leg and scored chest with blade^ court hears - The Irish Times,The Irish Times,2021-06-02,17:30:53Z
|
||||||
Asus ROG Strix G17^ ROG Strix G15 Advantage Edition Gaming Laptops With AMD Radeon RX 6800M GPU Announced - Gadgets 360,NDTV News,2021-06-02,13:21:30Z
|
US wields $2bn tariff threat against 6 nations over digital taxes - Financial Times,Financial Times,2021-06-02,17:25:24Z
|
||||||
Venus Williams has perfect response to Naomi Osaka French Open drama - New York Post ,New York Post,2021-06-02,13:18:00Z
|
Duke Blue Devils basketball coach Mike Krzyzewski plans to retire after season^ sources say - ESPN,ESPN,2021-06-02,17:24:40Z
|
||||||
Australia's Victoria extends Melbourne COVID-19 lockdown for 2nd week - Reuters,Reuters,2021-06-02,13:18:00Z
|
Do not politick with land^ Malema warns Ramaphosa as he cranks up pressure on ANC over amendment - News24,News24,2021-06-02,17:24:10Z
|
||||||
Nets make quick work of short-handed Celtics in first round of playoffs^ but much tougher test awaits in Bucks - CBS Sports,CBS Sports,2021-06-02,13:09:00Z
|
AMC frenzy triggers trading halts as stock surges 100 percent - NBC News,NBC News,2021-06-02,17:23:00Z
|
||||||
First Solar Eclipse of the year 2021: will be visible only from Arunachal Pradesh in India - Arunachal24,Arunachal24.in,2021-06-02,13:08:44Z
|
Facebook doubles down on business tools with WhatsApp API updates^ Login Connect for Messenger and more - TechCrunch,TechCrunch,2021-06-02,17:21:52Z
|
||||||
CDPR Won’t Reveal ‘Cyberpunk 2077’ Q1 Sales^ But Estimates Are Shockingly Low - Forbes,Forbes,2021-06-02,13:05:30Z
|
BJP leader Rakesh Pandit shot dead by terrorists outside his home in Kashmir - Moneycontrol.com,Moneycontrol,2021-06-02,17:19:38Z
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
name,current,opening
|
name,current,opening
|
||||||
MSFT,248.47,248.125
|
MSFT,247.3,248.125
|
||||||
NFLX,500.285,499.82
|
NFLX,499.24,499.82
|
||||||
GOOG,2430.27,2435.31
|
GOOG,2421.28,2435.31
|
||||||
|
|
BIN
display_image
BIN
display_image
Binary file not shown.
Before Width: | Height: | Size: 2.2 KiB After Width: | Height: | Size: 2.1 KiB |
137
test.py
137
test.py
@ -13,6 +13,9 @@ import finnhub
|
|||||||
import pexpect
|
import pexpect
|
||||||
from pycoingecko import CoinGeckoAPI
|
from pycoingecko import CoinGeckoAPI
|
||||||
from newsapi import NewsApiClient
|
from newsapi import NewsApiClient
|
||||||
|
import configparser
|
||||||
|
import requests
|
||||||
|
import json
|
||||||
|
|
||||||
import sys, os
|
import sys, os
|
||||||
try:
|
try:
|
||||||
@ -22,28 +25,128 @@ except Exception as e:
|
|||||||
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
|
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
|
||||||
print(exc_type, fname, exc_tb.tb_lineno, exc_obj, exc_tb)
|
print(exc_type, fname, exc_tb.tb_lineno, exc_obj, exc_tb)
|
||||||
|
|
||||||
|
|
||||||
|
def update_weather():
|
||||||
|
api_key = 'bd5d5096a5ba30bbcfb57ead42ab3fee'
|
||||||
|
location = 'London'
|
||||||
|
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__':
|
if __name__ == '__main__':
|
||||||
|
|
||||||
newsapi = NewsApiClient(api_key='cf08652bd17647b89aaf469a1a8198a9')
|
update_weather()
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
api_key = 'bd5d5096a5ba30bbcfb57ead42ab3fee'
|
||||||
|
location = 'London'
|
||||||
|
|
||||||
|
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)
|
||||||
|
print()
|
||||||
|
print('main weather: ', weather['weather'][0]['main'])
|
||||||
|
print('description: ', weather['weather'][0]['description'])
|
||||||
|
print('temp (degrees C): ', weather['main']['temp'])
|
||||||
|
print('min: ', weather['main']['temp_min'])
|
||||||
|
print('max: ', weather['main']['temp_max'])
|
||||||
|
print('feels like: ', weather['main']['feels_like'])
|
||||||
|
print('humidity (%): ', weather['main']['humidity'])
|
||||||
|
print('clouds: ', weather['clouds']['all'])
|
||||||
|
print('wind speed (m/s): ', weather['wind']['speed'], 'direction:', weather['wind']['deg'])
|
||||||
|
print('visibility (metres): ', weather['visibility'])
|
||||||
|
print('UV index: ', r.json()['current']['uvi'])
|
||||||
|
|
||||||
|
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']
|
||||||
|
print()
|
||||||
|
print(current_weather)
|
||||||
|
|
||||||
|
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+' ))
|
||||||
|
|
||||||
|
|
||||||
|
print()
|
||||||
|
print(weather)
|
||||||
|
print()
|
||||||
|
print(r.json()['daily'][0])
|
||||||
|
print()
|
||||||
|
print(daily_weather)
|
||||||
|
|
||||||
|
|
||||||
top_headlines = newsapi.get_top_headlines(q='bitcoin',
|
|
||||||
category='business',
|
|
||||||
country='us')
|
|
||||||
|
|
||||||
|
|
||||||
'''
|
|
||||||
top_headlines = newsapi.get_everything(q='bitcoin',
|
|
||||||
sources='bbc-news,the-verge',
|
|
||||||
page=1)
|
|
||||||
'''
|
|
||||||
#print(top_headlines)
|
|
||||||
headline_titles = [top_headline['title'] for top_headline in top_headlines['articles']]
|
|
||||||
headline_ids = [top_headline['source']['name'] for top_headline in top_headlines['articles']]
|
|
||||||
headline_times = [top_headline['publishedAt']for top_headline in top_headlines['articles']]
|
|
||||||
|
|
||||||
#print([top_headline.keys()for top_headline in top_headlines['articles']])
|
|
||||||
print(headline_titles)
|
|
||||||
print(headline_ids)
|
|
||||||
print(headline_times)
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user