2021-05-05 16:22:01 +01:00
import finnhub
import time
import csv
2021-05-05 20:26:56 +01:00
import pytz
2021-06-09 19:06:21 +01:00
from datetime import datetime , timedelta
2021-05-27 20:10:57 +01:00
import json
2021-05-06 20:59:27 +01:00
import datetime as dt
2021-05-27 20:10:57 +01:00
import sys , os , base64 , hashlib , hmac , select
2021-05-08 12:10:05 +01:00
import requests
2021-05-14 13:02:22 +01:00
from pycoingecko import CoinGeckoAPI
2021-05-21 14:24:37 +01:00
from newsapi import NewsApiClient
2021-05-27 20:10:57 +01:00
def getInput ( Block = False ) :
if Block or select . select ( [ sys . stdin ] , [ ] , [ ] , 0 ) == ( [ sys . stdin ] , [ ] , [ ] ) :
msg = sys . stdin . read ( 1 )
#sys.stdin.flush()
else :
msg = ' '
return msg
2021-05-05 20:26:56 +01:00
2021-05-14 13:02:22 +01:00
def readCSV ( file_path , max_stocks ) :
2021-05-06 20:59:27 +01:00
symbols = [ ]
stock_info = { }
2021-05-14 13:02:22 +01:00
f = open ( file_path , ' r ' )
2021-05-06 20:59:27 +01:00
CSV = csv . reader ( f )
next ( CSV )
2021-05-07 19:39:42 +01:00
i = 0
2021-05-06 20:59:27 +01:00
for row in CSV :
2021-05-07 19:39:42 +01:00
if i < max_stocks :
i + = 1
try :
symbol , current_price , opening_price = row
symbols . append ( symbol )
stock_info [ symbol ] = [ current_price , opening_price ]
except :
symbol = row [ 0 ]
symbols . append ( symbol )
stock_info [ symbol ] = [ ]
else :
print ( ' max stocks exceeded ' )
break
2021-05-06 20:59:27 +01:00
f . close ( )
return symbols , stock_info
2021-05-24 20:59:42 +01:00
def readCryptoCSV ( file_path , max_stocks ) :
symbols = [ ]
names = [ ]
stock_info = { }
f = open ( file_path , ' r ' )
CSV = csv . reader ( f )
next ( CSV )
i = 0
2021-05-25 19:57:34 +01:00
unique_bases = [ ]
2021-05-24 20:59:42 +01:00
for row in CSV :
print ( row )
if i < max_stocks :
i + = 1
try :
2021-05-25 19:57:34 +01:00
symbol , name , base , current_price , opening_price = row
2021-05-24 20:59:42 +01:00
symbols . append ( symbol )
names . append ( name )
2021-05-25 19:57:34 +01:00
stock_info [ name ] = [ symbol , base , current_price , opening_price ]
if base not in unique_bases :
unique_bases . append ( base )
2021-05-24 20:59:42 +01:00
except :
2021-05-25 19:57:34 +01:00
symbol , name , base = row
if base not in unique_bases :
unique_bases . append ( base )
2021-05-24 20:59:42 +01:00
symbols . append ( symbol )
names . append ( name )
2021-05-25 19:57:34 +01:00
stock_info [ name ] = [ symbol , base ]
2021-05-24 20:59:42 +01:00
else :
print ( ' max stocks exceeded ' )
break
f . close ( )
2021-05-25 19:57:34 +01:00
return names , stock_info , unique_bases
2021-05-06 20:59:27 +01:00
def emptyInfo ( symbols , stock_info ) :
update = False
for symbol in symbols :
if len ( stock_info [ symbol ] ) == 1 : # stock with no info
update = True
return update
def updateUpdate ( NY_time ) :
NY_str = NY_time . strftime ( " %d / % m/ % Y % H: % M: % S " )
f = open ( ' csv/last_update.csv ' , ' w+ ' )
f . write ( NY_str + ' \n ' )
f . close ( )
2021-05-05 20:26:56 +01:00
2021-05-06 20:59:27 +01:00
def updateStockPrices ( symbols ) :
try :
2021-05-08 12:10:05 +01:00
quotes = [ finnhubClient . quote ( symbol ) for symbol in symbols ]
current_prices = [ quote [ ' c ' ] for quote in quotes ]
opening_prices = [ quote [ ' o ' ] for quote in quotes ]
2021-05-25 19:57:34 +01:00
2021-05-14 13:02:22 +01:00
2021-05-06 20:59:27 +01:00
CSV = open ( ' csv/tickers.csv ' , ' w+ ' )
CSV . write ( ' name,current,opening \n ' )
for i , symbol in enumerate ( symbols ) :
2021-05-07 19:39:42 +01:00
2021-05-06 20:59:27 +01:00
CSV . write ( symbol + ' , ' + str ( current_prices [ i ] ) + ' , ' + str ( opening_prices [ i ] ) + ' \n ' )
CSV . close ( )
print ( ' API called successfully ' )
except Exception as e :
print ( " Could not fetch data - API CALLS REACHED? - Will display old image " )
print ( e )
apiCalledError = True
2021-05-08 12:10:05 +01:00
def updateStockPricesIEX ( symbols ) :
symbols_str = ' , ' . join ( symbols )
method = ' GET '
host = ' https://cloud.iexapis.com/stable '
lastEndpoint = ' /tops/last '
querystring = ' ?symbols= ' + symbols_str + ' &token= ' + iexAPIkey
last_request_url = host + lastEndpoint + querystring
print ( ' \n BEGIN REQUEST++++++++++++++++++++++++++++++++++++ ' )
print ( ' Request URL = ' + last_request_url )
last_response = requests . get ( last_request_url )
if last_response . status_code == 200 :
print ( ' last success ' )
current_prices = [ ]
for stock in last_response . json ( ) :
current_prices . append ( stock [ ' price ' ] )
for symbol in symbols :
symbol = ' MSFT '
method = ' GET '
host = ' https://cloud.iexapis.com/stable '
lastEndpoint = ' /tops/last '
intradayEndpoint = ' /stock/ ' + symbol + ' /intraday-prices '
querystring = ' ?chartIEXOnly=true&token= ' + iexAPIkey
intraday_request_url = host + intradayEndpoint + querystring
print ( ' \n BEGIN REQUEST++++++++++++++++++++++++++++++++++++ ' )
print ( ' Request URL = ' + intraday_request_url )
intraday_response = requests . get ( intraday_request_url )
print ( ' \n RESPONSE++++++++++++++++++++++++++++++++++++ ' )
print ( ' Response code: %d \n ' % intraday_response . status_code )
print ( intraday_response . text )
for time_point in intraday_response . json ( ) :
print ( time_point [ ' date ' ] )
print ( time_point [ ' open ' ] )
CSV = open ( ' csv/tickers.csv ' , ' w+ ' )
CSV . write ( ' name,current,opening \n ' )
for i , symbol in enumerate ( symbols ) :
CSV . write ( symbol + ' , ' + str ( current_prices [ i ] ) + ' , ' + str ( opening_prices [ i ] ) + ' \n ' )
CSV . close ( )
2021-05-14 13:02:22 +01:00
2021-05-25 19:57:34 +01:00
def updateCrypto ( coins , coin_info , unique_bases ) :
response = coingecko_client . get_price ( ids = ' , ' . join ( coins ) , vs_currencies = unique_bases , include_24hr_change = True )
2021-05-14 13:02:22 +01:00
CSV = open ( ' csv/crypto.csv ' , ' w+ ' )
2021-05-25 19:57:34 +01:00
CSV . write ( ' symbol,name,base,current,24hr change \n ' )
2021-05-14 13:02:22 +01:00
for coin in coins :
2021-05-25 19:57:34 +01:00
info = coin_info [ coin ]
print ( info )
CSV . write ( info [ 0 ] + ' , ' + coin + ' , ' + info [ 1 ] + ' , ' + str ( response [ coin ] [ info [ 1 ] ] ) + ' , ' + str ( response [ coin ] [ ' usd_24h_change ' ] ) + ' \n ' )
2021-05-14 13:02:22 +01:00
CSV . close ( )
2021-05-21 14:24:37 +01:00
def updateNews ( ) :
2021-05-27 20:10:57 +01:00
try :
#load user settings
settings = json . load ( open ( ' csv/news_settings.json ' , ' r ' ) )
2021-06-14 20:36:17 +01:00
2021-05-27 20:10:57 +01:00
print ( settings )
headlines = newsapi . get_top_headlines ( * * settings )
except :
#if no settings just get top headlines
headlines = newsapi . get_top_headlines ( )
headline_titles = [ headline [ ' title ' ] for headline in headlines [ ' articles ' ] ]
headline_sources = [ headline [ ' source ' ] [ ' name ' ] for headline in headlines [ ' articles ' ] ]
headline_times = [ headline [ ' publishedAt ' ] for headline in headlines [ ' articles ' ] ]
2021-05-21 14:24:37 +01:00
CSV = open ( ' csv/news.csv ' , ' w+ ' )
2021-05-27 20:10:57 +01:00
CSV . write ( ' headline,source,date,time \n ' )
2021-05-21 14:24:37 +01:00
2021-05-27 20:10:57 +01:00
for i , title in enumerate ( headline_titles ) :
date , time = headline_times [ i ] . split ( ' T ' )
CSV . write ( title . replace ( ' , ' , ' ^ ' ) + ' , ' + headline_sources [ i ] + ' , ' + date + ' , ' + time + ' \n ' )
2021-05-21 14:24:37 +01:00
CSV . close ( )
2021-06-09 19:06:21 +01:00
def updateWeather ( api_key ) :
f = open ( " csv/weather_location.txt " , ' r ' )
location = f . read ( )
f . close ( )
2021-06-02 21:16:15 +01:00
url = " https://api.openweathermap.org/data/2.5/weather?q= {} &units=metric&appid= {} " . format ( location , api_key )
r = requests . get ( url )
weather = r . json ( )
2021-06-03 21:56:25 +01:00
print ( weather )
2021-06-02 21:16:15 +01:00
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 ' ]
2021-06-08 21:16:08 +01:00
current_weather [ ' rain_chance ' ] = r . json ( ) [ ' hourly ' ] [ 0 ] [ ' pop ' ]
2021-06-02 21:16:15 +01:00
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 ' ]
2021-06-07 21:03:03 +01:00
dct [ ' max_temp ' ] = day [ ' temp ' ] [ ' max ' ]
2021-06-02 21:16:15 +01:00
daily_weather . append ( dct )
2021-06-08 21:16:08 +01:00
2021-06-02 21:16:15 +01:00
json . dump ( daily_weather , open ( " csv/daily_weather.json " , ' w+ ' ) )
2021-06-09 19:06:21 +01:00
def updateCurrencies ( api_key ) :
base = ' USD '
yesterday = datetime . now ( ) - timedelta ( 1 )
str_tod = datetime . strftime ( datetime . now ( ) , ' % Y- % m- %d ' )
str_yest = datetime . strftime ( yesterday , ' % Y- % m- %d ' )
url = ' https://api.frankfurter.app/ {} .. {} ?from= {} ' . format ( str_yest , str_tod , base )
r = requests . get ( url )
all_data = r . json ( )
currencies = [ ' AUD ' , ' CAD ' , ' CHF ' , ' EUR ' , ' GBP ' , ' JPY ' , ' NZD ' ]
c_dict = { }
print ( all_data )
for curr in currencies :
current = all_data [ ' rates ' ] [ str_tod ] [ curr ]
yesterday = all_data [ ' rates ' ] [ str_yest ] [ curr ]
change = current - yesterday
c_dict [ curr ] = [ current , yesterday ]
print ( c_dict )
json . dump ( [ base , c_dict ] , open ( " csv/currency.json " , ' w+ ' ) )
2021-06-14 20:36:17 +01:00
2021-06-15 19:35:04 +01:00
def updateSports ( api_key ) :
2021-06-16 19:23:14 +01:00
url = ' https://www.thesportsdb.com/api/v1/json/ {} /lookuptable.php?l=4328&s=2020-2021 ' . format ( api_key ) # premier league table
' https://www.thesportsdb.com/api/v1/json/ {} /eventsnext.php?id=133602 ' . format ( api_key ) # next five events by team ID (paid) use this for upcoming team games
2021-06-14 20:36:17 +01:00
r = requests . get ( url )
all_data = r . json ( )
print ( [ all_data [ ' table ' ] [ i ] [ ' intRank ' ] for i in range ( len ( all_data [ ' table ' ] ) ) ] )
premier_teams = [ ]
print ( all_data [ ' table ' ] [ 0 ] . keys ( ) )
for i in range ( len ( all_data [ ' table ' ] ) ) :
team = { }
team [ ' name ' ] = all_data [ ' table ' ] [ i ] [ ' strTeam ' ]
team [ ' wins ' ] = all_data [ ' table ' ] [ i ] [ ' intWin ' ]
team [ ' loss ' ] = all_data [ ' table ' ] [ i ] [ ' intLoss ' ]
team [ ' draw ' ] = all_data [ ' table ' ] [ i ] [ ' intDraw ' ]
team [ ' played ' ] = all_data [ ' table ' ] [ i ] [ ' intPlayed ' ]
team [ ' standing ' ] = all_data [ ' table ' ] [ i ] [ ' intRank ' ]
team [ ' points ' ] = all_data [ ' table ' ] [ i ] [ ' intPoints ' ]
premier_teams . append ( team )
json . dump ( premier_teams , open ( " csv/sports/premier_league/team_stats.json " , ' w+ ' ) )
2021-06-16 19:23:14 +01:00
#url = 'https://www.thesportsdb.com/api/v1/json/{}/eventsseason.php?id=4328&s=2020-2021'.format(api_key) # all past events in premier league
url = ' https://www.thesportsdb.com/api/v1/json/ {} /eventspastleague.php?id=4328 ' . format ( api_key ) #last 15 events on the league (premium only)
print ( url )
2021-06-14 20:36:17 +01:00
r = requests . get ( url )
all_data = r . json ( )
print ( )
print ( all_data [ ' events ' ] [ 0 ] )
#print([all_data['events'][i]['strTimestamp'] for i in range(len(all_data['events']))])
events = [ ]
2021-06-16 19:23:14 +01:00
for i in range ( len ( all_data [ ' events ' ] ) ) :
2021-06-14 20:36:17 +01:00
event = { }
event [ ' date ' ] = all_data [ ' events ' ] [ i ] [ ' dateEvent ' ]
2021-06-16 19:23:14 +01:00
print ( event [ ' date ' ] )
2021-06-14 20:36:17 +01:00
event [ ' time ' ] = all_data [ ' events ' ] [ i ] [ ' strTime ' ]
event [ ' home_team ' ] = all_data [ ' events ' ] [ i ] [ ' strHomeTeam ' ]
event [ ' home_score ' ] = all_data [ ' events ' ] [ i ] [ ' intHomeScore ' ]
event [ ' away_team ' ] = all_data [ ' events ' ] [ i ] [ ' strAwayTeam ' ]
event [ ' away_score ' ] = all_data [ ' events ' ] [ i ] [ ' intAwayScore ' ]
event [ ' round ' ] = all_data [ ' events ' ] [ i ] [ ' intRound ' ]
events . append ( event )
json . dump ( events , open ( " csv/sports/premier_league/past_games.json " , ' w+ ' ) )
2021-06-16 19:23:14 +01:00
url = ' https://www.thesportsdb.com/api/v1/json/ {} /eventsnextleague.php?id=4328 ' . format ( api_key ) #next 15 events on the league (premium only)
url = ' https://www.thesportsdb.com/api/v2/json/ {} /livescore.php?l=4380 ' . format ( api_key ) #live scores
print ( all_data )
2021-06-14 20:36:17 +01:00
2021-05-06 20:59:27 +01:00
if __name__ == ' __main__ ' :
2021-06-14 20:36:17 +01:00
2021-05-21 14:24:37 +01:00
newsapi = NewsApiClient ( api_key = ' cf08652bd17647b89aaf469a1a8198a9 ' )
2021-05-08 12:10:05 +01:00
finnhubAPIkey = " c24qddqad3ickpckgg80 " #Finnhub
finnhubsandboxAPIkey = " sandbox_c24qddqad3ickpckgg8g " #Finnhub
2021-06-02 21:16:15 +01:00
stock_time = 2 #minutes
news_time = 30 #minutes
weather_time = 10 #minutes
# TODO: different update times for stocks, weather and news
2021-05-08 12:10:05 +01:00
finnhubClient = finnhub . Client ( api_key = finnhubAPIkey )
max_stocks = 200
2021-05-07 19:39:42 +01:00
iexAPIkey = ' pk_68ef6a15902c41f887f0b544a0ca17cf ' #IEX
iexSandboxAPIkey = ' Tpk_0078dff413ef4f979137f7111452dc4b '
2021-05-08 12:10:05 +01:00
#updateStockPricesIEX(symbols)
2021-05-05 20:26:56 +01:00
2021-05-08 12:10:05 +01:00
finnhubClient = finnhub . Client ( api_key = finnhubAPIkey )
2021-05-14 13:02:22 +01:00
coingecko_client = CoinGeckoAPI ( )
2021-05-06 20:59:27 +01:00
NY_zone = pytz . timezone ( ' America/New_York ' )
2021-05-05 20:26:56 +01:00
2021-05-06 20:59:27 +01:00
NY_time = datetime . now ( NY_zone )
opening = NY_time . replace ( hour = 9 , minute = 30 , second = 0 , microsecond = 0 )
closing = NY_time . replace ( hour = 16 , minute = 0 , second = 0 , microsecond = 0 )
2021-05-05 20:26:56 +01:00
2021-05-06 19:23:17 +01:00
NY_time = datetime . now ( NY_zone )
2021-05-25 19:57:34 +01:00
2021-05-23 10:34:06 +01:00
symbols , stock_info = readCSV ( ' csv/tickers.csv ' , max_stocks )
updateStockPrices ( symbols )
updateUpdate ( NY_time )
2021-05-27 20:10:57 +01:00
coins , coin_info , unique_bases = readCryptoCSV ( ' csv/crypto.csv ' , max_stocks )
2021-06-02 21:16:15 +01:00
2021-06-09 19:06:21 +01:00
weather_key = ' bd5d5096a5ba30bbcfb57ead42ab3fee '
currency_key = ' 862dbb6d1101ce0c5136 '
2021-06-15 19:35:04 +01:00
sports_key = ' 97436974 '
2021-06-09 19:06:21 +01:00
2021-06-16 19:23:14 +01:00
updateSports ( sports_key )
sys . exit ( )
2021-06-09 19:06:21 +01:00
updateCurrencies ( currency_key )
2021-06-02 21:16:15 +01:00
2021-06-09 19:06:21 +01:00
updateWeather ( weather_key )
2021-05-27 20:10:57 +01:00
updateCrypto ( coins , coin_info , unique_bases )
updateNews ( )
t = time . time ( )
logf = open ( " log.txt " , " w " )
try :
while True :
msg = getInput ( )
2021-06-02 21:16:15 +01:00
if msg == ' R ' or time . time ( ) - t > stock_time * 60 :
2021-05-27 20:10:57 +01:00
coins , coin_info , unique_bases = readCryptoCSV ( ' csv/crypto.csv ' , max_stocks )
updateCrypto ( coins , coin_info , unique_bases )
2021-06-09 19:06:21 +01:00
updateCurrencies ( currency_key )
2021-05-27 20:10:57 +01:00
updateNews ( )
2021-06-02 21:16:15 +01:00
updateWeather ( weather_location , weather_key )
2021-06-15 19:35:04 +01:00
updateSports ( sports_key )
2021-05-27 20:10:57 +01:00
NY_time = datetime . now ( NY_zone )
symbols , stock_info = readCSV ( ' csv/tickers.csv ' , max_stocks )
if opening < NY_time < closing and datetime . today ( ) . weekday ( ) < 5 : # we need to do real time updating
print ( ' market open ' )
2021-05-14 13:02:22 +01:00
2021-05-27 20:10:57 +01:00
updateStockPrices ( symbols )
updateUpdate ( NY_time )
elif emptyInfo ( symbols , stock_info ) : # if theres any empty stocks
updateStockPrices ( symbols )
updateUpdate ( NY_time )
2021-05-08 12:10:05 +01:00
2021-05-27 20:10:57 +01:00
else :
# update if last update was before the previous days closing
f = open ( ' csv/last_update.csv ' , ' r ' )
CSV = csv . reader ( f )
last_update_str = next ( CSV ) [ 0 ]
last_update = datetime . strptime ( last_update_str , " %d / % m/ % Y % H: % M: % S " )
yday_closing = closing - dt . timedelta ( days = 1 )
yday_str = yday_closing . strftime ( " %d / % m/ % Y % H: % M: % S " )
yday_closing = datetime . strptime ( yday_str , " %d / % m/ % Y % H: % M: % S " )
2021-05-05 20:26:56 +01:00
2021-05-27 20:10:57 +01:00
if last_update < yday_closing :
updateStockPrices ( symbols )
updateUpdate ( NY_time )
t = time . time ( )
except Exception as e :
logf . write ( srt ( e ) )
2021-05-05 20:26:56 +01:00
2021-05-27 20:10:57 +01:00