news formatting and user input added

This commit is contained in:
Neythen
2021-05-27 20:10:57 +01:00
parent 87fe5a67d6
commit 50a777ada4
17 changed files with 228 additions and 100 deletions

View File

@@ -3,13 +3,20 @@ import time
import csv
import pytz
from datetime import datetime
import json
import datetime as dt
import sys, os, base64, hashlib, hmac
import sys, os, base64, hashlib, hmac, select
import requests
from pycoingecko import CoinGeckoAPI
from newsapi import NewsApiClient
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
def readCSV(file_path, max_stocks):
@@ -188,13 +195,30 @@ def updateCrypto(coins, coin_info, unique_bases):
def updateNews():
top_headlines = newsapi.get_top_headlines()
headline_titles = [top_headline['title'] for top_headline in top_headlines['articles']]
CSV = open('csv/news.csv', 'w+')
CSV.write('headline\n')
for title in headline_titles:
CSV.write(title + '\n')
try:
#load user settings
settings = json.load(open('csv/news_settings.json', 'r'))
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']]
CSV = open('csv/news.csv', 'w+')
CSV.write('headline,source,date,time\n')
for i, title in enumerate(headline_titles):
date, time = headline_times[i].split('T')
CSV.write(title.replace(',', '^') + ',' + headline_sources[i] + ',' + date + ',' + time + '\n')
CSV.close()
@@ -233,46 +257,66 @@ if __name__ == '__main__':
symbols, stock_info = readCSV('csv/tickers.csv', max_stocks)
updateStockPrices(symbols)
updateUpdate(NY_time)
while True:
coins, coin_info, unique_bases = readCryptoCSV('csv/crypto.csv', max_stocks)
updateCrypto(coins, coin_info, unique_bases)
updateNews()
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')
coins, coin_info, unique_bases = readCryptoCSV('csv/crypto.csv', max_stocks)
updateCrypto(coins, coin_info, unique_bases)
updateNews()
t = time.time()
logf = open("log.txt", "w")
try:
while True:
msg = getInput()
if msg == 'R' or time.time() - t > sleeptime*60:
coins, coin_info, unique_bases = readCryptoCSV('csv/crypto.csv', max_stocks)
updateCrypto(coins, coin_info, unique_bases)
updateNews()
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')
updateStockPrices(symbols)
updateUpdate(NY_time)
elif emptyInfo(symbols, stock_info): # if theres any empty stocks
updateStockPrices(symbols)
updateUpdate(NY_time)
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")
if last_update < yday_closing:
updateStockPrices(symbols)
updateUpdate(NY_time)
elif emptyInfo(symbols, stock_info): # if theres any empty stocks
updateStockPrices(symbols)
updateUpdate(NY_time)
updateStockPrices(symbols)
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")
updateUpdate(NY_time)
if last_update < yday_closing:
updateStockPrices(symbols)
updateUpdate(NY_time)
t = time.time()
except Exception as e:
logf.write(srt(e))
time.sleep(sleeptime*60)