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,8 +257,25 @@ if __name__ == '__main__':
symbols, stock_info = readCSV('csv/tickers.csv', max_stocks)
updateStockPrices(symbols)
updateUpdate(NY_time)
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)
@ -274,5 +315,8 @@ if __name__ == '__main__':
updateStockPrices(symbols)
updateUpdate(NY_time)
t = time.time()
except Exception as e:
logf.write(srt(e))
time.sleep(sleeptime*60)

View File

@ -1,3 +1,3 @@
symbol,name,base,current,24hr change
BTC,bitcoin,usd,38061,0.15138810082760248
ETH,ethereum,gbp,1841.33,1.197151365974607
BTC,bitcoin,usd,39062,1.451872309860798
ETH,ethereum,gbp,1961.49,1.6934708417131437

1 symbol name base current 24hr change
2 BTC bitcoin usd 38061 39062 0.15138810082760248 1.451872309860798
3 ETH ethereum gbp 1841.33 1961.49 1.197151365974607 1.6934708417131437

View File

@ -1 +1 @@
25/05/2021 14:54:13
27/05/2021 15:08:39

1 25/05/2021 14:54:13 27/05/2021 15:08:39

View File

@ -1,21 +1,21 @@
headline
COVID-19 LIVE UPDATES | SA says consensus reached for talks on pandemic treaty - TimesLIVE
WHO, DOST set crucial vaccine tests for Pinoys - INQUIRER.net
Huawei to unveil new OS - The Manila Times
Total lunar eclipse 2021: How to see the super flower blood moon - MARCA.com
NBI given 10 days to file initial probe findings into 'vaccine for sale' scam | Joel R. San Juan - Business Mirror
BTS single "Butter" breaks three world records across YouTube and Spotify - Guinness World Records
“Super Flower Blood Moon” to be visible to southern Albertans early Wednesday morning - Lethbridge News Now
U.S. will reopen consulate in Jerusalem, Secretary of State Blinken announces - CBC.ca
18 COVID-19 community cases in Singapore, including Sengkang General Hospital housekeeper - CNA
When to see the total lunar eclipse and 'flower' supermoon - CNN
Match Highlights BAN vs SL 2nd ODI Updates: Ton-Up Mushfiqur Rahim And Bowlers Shine as Bangladesh Beat Sri L - India.com
Central Bank bans former mortgage firm MD as gardaí investigate - The Irish Times
Build 2021: Heres everything Microsoft announced at its annual developer conference - The Indian Express
GLOBALink | Exploring Mars with China's Zhurong rover - Daily News Egypt
Axiom Space lines up second private flight to the ISS, led by record-holding astronaut Peggy Whitson - CNBC
COVID-19: Councils say travel not prohibited after meeting government officials over local lockdown fears - Sky News
Prince Harry makes heartbreakingly sweet comment about 'mummy' Princess Diana in unearthed video showing his once strong bond with Prince William - woman&home
COVID: India's 'white fungus' infections raise new health concerns - DW (English)
Cllr slammed by party colleagues over child benefit 'subsidy to have sex' claim - Dublin Live
Rand Paul points finger at 80s pop star after receiving suspicious powder - The Guardian
headline,source,date,time
Competitor Fears Elon Musk's SpaceX Could "Monopolise" Space - NDTV,NDTV News,2021-05-27,18:31:03Z
Meme stocks mount a comeback as AMC^ GameStop shareholders squeeze short sellers again - CBC.ca,CBC News,2021-05-27,18:22:41Z
U.S. administers 290.7 mln doses of COVID-19 vaccines - CDC - Financial Post,Financial Post,2021-05-27,18:13:22Z
Covid-19: Up to 75% of new UK cases could be Indian variant - Matt Hancock - BBC News,BBC News,2021-05-27,18:09:12Z
Super Moon vs Blood Moon - India Today,India Today,2021-05-27,18:04:39Z
Vaccine target for majority of adults likely to be delayed Varadkar - The Irish Times,The Irish Times,2021-05-27,18:00:00Z
'Its a Champions League final but its just a football game for me' - Mahrez relaxed for Man City's big night - Goal.com,Sporting News,2021-05-27,18:00:00Z
UK aid cuts directly hamper fight against HIV^ warn politicians and Aids groups - Financial Times,Financial Times,2021-05-27,17:58:01Z
New York Knicks ban fan from Madison Square Garden for spitting on Atlanta Hawks' Trae Young - ESPN,ESPN,2021-05-27,17:46:50Z
I couldn't sleep for 8-9 days while I was playing: Ashwin on why he had to leave IPL midway - Times of India,The Times of India,2021-05-27,17:39:00Z
New studies claim COVID-19 immunity may last years - New York Post ,New York Post,2021-05-27,17:35:00Z
COVID-19: Matt Hancock says 'we worked as hard as we could to protect care homes' after Dominic Cummings's criticism - Sky News,Sky.com,2021-05-27,17:34:18Z
Mixed reaction from experts to new guidelines on dining out - The Irish Times,The Irish Times,2021-05-27,17:33:24Z
Gold-standard contact tracing swamped as Covid sites soar - Herald Sun,Herald Sun,2021-05-27,17:32:53Z
Shami receives first dose of COVID-19 vaccine - DTNext,Dtnext.in,2021-05-27,17:29:36Z
Some Canadians falsely assuming Pfizer COVID-19 vaccine more effective than Moderna - CP24 Toronto's Breaking News,CP24 Toronto's Breaking News,2021-05-27,17:19:00Z
China's Zhurong Mars rover lays down wheel tracks on the red planet - CNET,CNET,2021-05-27,17:13:00Z
Packers still not budging on Aaron Rodgers trade possibility - NBC Sports,NBCSports.com,2021-05-27,17:09:00Z
How John Krasinski tortured Emily Blunt (again) for 'A Quiet Place Part II' by exploiting 'her greatest weakness' - Yahoo Entertainment,Yahoo Entertainment,2021-05-27,17:01:50Z
Dark matter mapped using light from 100 million galaxies - The Irish News,The Irish News,2021-05-27,17:01:41Z

Can't render this file because it contains an unexpected character in line 7 and column 12.

0
csv/news_settings.csv Normal file
View File

0
csv/news_settings.json Normal file
View File

View File

@ -1,2 +1,2 @@
speed,brightness
f,5
s,9
1 speed brightness
2 f s 5 9

View File

@ -1,4 +1,4 @@
name,current,opening
MSFT,251.215,251.769
NFLX,500.28,506
GOOG,2406.31,2420
MSFT,249.62,251.17
NFLX,501.1229,501.8
GOOG,2431.28,2436.94

1 name current opening
2 MSFT 251.215 249.62 251.769 251.17
3 NFLX 500.28 501.1229 506 501.8
4 GOOG 2406.31 2431.28 2420 2436.94

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.1 KiB

After

Width:  |  Height:  |  Size: 2.9 KiB

BIN
final.ppm

Binary file not shown.

Binary file not shown.

View File

@ -14,6 +14,7 @@ import threading
import csv
import pexpect
import time
import json
from multiprocessing import Process
from subprocess import Popen, PIPE
#stock_ticker = StockTicker()
@ -147,6 +148,36 @@ def DisplayText():
ticker.sendline('T')
return hello()
@app.route("/SetNews", methods=['POST'])
def SetNews():
text = request.form['text']
#args = ['q', 'sources', 'category', 'country']
args = ['q', 'category', 'country']
arg_dict = {}
lst = text.split(',')
try:
for i, arg in enumerate(args):
if len(lst[i])>0:
arg_dict[arg] = lst[i]
print(arg_dict)
json.dump( arg_dict, open( "csv/news_settings.json", 'w+' ))
print(settings)
except Exception as e:
#reset settings
print(e)
f = open( "csv/news_settings.json", 'w+' )
f.close()
api_caller.sendline('R')
return hello()
@app.route("/DisplayImage", methods=['POST'])
def DisplayImage():
if request.method == 'POST':

View File

@ -243,32 +243,64 @@ class StockTicker():
self.scrollImageTransition(['scroll_text.ppm', 'scroll_text.ppm'], offset_x = 128, offset_y = 0, stocks = False)
def displayNews(self):
font = ImageFont.load("./fonts/8x13B.pil")
headline_font = ImageFont.load("./fonts/8x13B.pil")
source_font = ImageFont.load("./fonts/5x8.pil")
while True:
headlines = []
source_date_times = []
f = open('csv/news.csv', 'r')
CSV = csv.reader(f)
next(CSV)
for row in CSV:
headlines.append(','.join(row))
headline, source, date, time = row
headlines.append(headline)
source_date_times.append(source + ': ' + date + ' ' + time)
f.close()
for headline in headlines:
if len(headlines) > 0:
for i, headline in enumerate(headlines):
headline = headline.replace("^", ",")
headline = headline.replace("", "'")
headline = headline.replace("", "'")
headline = headline.replace('', '"')
headline = headline.replace('', '"')
img = self.textImage(headline, font)
headline_img = self.textImage(headline, headline_font, matrix_height = True)
source_img = self.textImage(source_date_times[i], source_font, r=255, g=255, b=0, matrix_height = True)
try:
logos_path = os.path.join(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'logos'), 'news_logos')
logo = Image.open(os.path.join(logos_path, 'techcrunch' + '.png'))
print(logo.size)
img = Image.new('RGB', (headline_img.size[0], 32))
img.paste(headline_img, (2, 0))
img.paste(source_img, (2,16))
img= self.stitchImage([logo,img])
except Exception as e:
print(e)
img = Image.new('RGB', (headline_img.size[0], 32))
img.paste(headline_img, (0,0))
img.paste(source_img, (0,16))
img.save('image.ppm')
killed = self.scrollImage(img, offset_x = 128, offset_y = 0)
#killed = self.scrollImageStacked(img, offset_x = 128, offset_y = 0)
if killed: break
else:
print('new failed')
if killed: break
@ -372,6 +404,7 @@ class StockTicker():
new_im = Image.new('RGB', (total_width, max_height))
x_offset = 0
for im in image_list:
print(x_offset)
new_im.paste(im, (x_offset,0))
x_offset += im.size[0]

View File

@ -36,6 +36,12 @@
<input type="submit" value="Display"style="height:30px">
</form>
<p>Set news search settings. keyword,category,country e.g. bitcoin,business,us/p>
<form action="/SetNews" method="POST">
<input name="text" placeholder="bitcoin,business,us"style="height:24px">
<input type="submit" value="Display"style="height:30px">
</form>
<p>Display an image</p>
<form action="/DisplayImage" method="POST" enctype=multipart/form-data>
<p><input type=file name=file style="height:30px">

26
test.py
View File

@ -12,16 +12,30 @@ import finnhub
import pexpect
from pycoingecko import CoinGeckoAPI
from newsapi import NewsApiClient
if __name__ == '__main__':
coingecko_client = CoinGeckoAPI()
newsapi = NewsApiClient(api_key='cf08652bd17647b89aaf469a1a8198a9')
top_headlines = newsapi.get_top_headlines(q='bitcoin',
category='business',
country='us')
response = coingecko_client.get_price(ids=['bitcoin', 'ethereum'], vs_currencies = ['gbp', 'usd'], include_24hr_change=True)
print(response)
#print(dir( coingecko_client))
#print(dir( coingecko_client))
'''
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)