scrolling news headlines added

This commit is contained in:
Neythen 2021-05-21 14:24:37 +01:00
parent 7486ac1125
commit b0f106c64f
17 changed files with 195 additions and 72 deletions

View File

@ -8,6 +8,8 @@ import datetime as dt
import sys, os, base64, hashlib, hmac
import requests
from pycoingecko import CoinGeckoAPI
from newsapi import NewsApiClient
def readCSV(file_path, max_stocks):
@ -144,8 +146,25 @@ def updateCrypto(coins):
CSV.write(coin+ ',' + str(response[coin]['usd']) + ',' + str(response[coin]['usd_24h_change']) + '\n')
CSV.close()
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')
CSV.close()
if __name__ == '__main__':
newsapi = NewsApiClient(api_key='cf08652bd17647b89aaf469a1a8198a9')
finnhubAPIkey = "c24qddqad3ickpckgg80" #Finnhub
finnhubsandboxAPIkey = "sandbox_c24qddqad3ickpckgg8g" #Finnhub
sleeptime = 2 #minutes
@ -173,12 +192,13 @@ if __name__ == '__main__':
NY_time = datetime.now(NY_zone)
print(NY_time)
coins, coin_info = readCSV('csv/crypto.csv', max_stocks)
print(coins, coin_info)
updateCrypto(coins)
sys.exit()
while True:
coins, coin_info = readCSV('csv/crypto.csv', max_stocks)
updateCrypto(coins)
#updateNews()
NY_time = datetime.now(NY_zone)
symbols, stock_info = readCSV('csv/tickers.csv', max_stocks)

View File

@ -1,3 +1,3 @@
name,current,24hr change
bitcoin,40905,1.9630605505752174
ethereum,2756.88,2.711695798229129
bitcoin,41158,-3.112342608605254
ethereum,2691.26,-8.560574933024819

1 name current 24hr change
2 bitcoin 40905 41158 1.9630605505752174 -3.112342608605254
3 ethereum 2756.88 2691.26 2.711695798229129 -8.560574933024819

View File

@ -1 +1 @@
14/05/2021 06:29:45
21/05/2021 09:21:44

1 14/05/2021 06:29:45 21/05/2021 09:21:44

21
csv/news.csv Normal file
View File

@ -0,0 +1,21 @@
headline, ' doesnt display, sort out comma
Total of 43 Changi Airport workers test positive for Covid-19; source likely worker who helped infected family from South Asia - The Straits Times
Covid-19 (May 21) - 6493 new cases, nationwide uptrend; 50 deaths - Malaysiakini
Lady Gaga says rape as teenager left her pregnant and caused psychotic break - The Guardian
Coronavirus: Spain to lift restrictions for UK and Japanese travellers - BBC News
Walsh wonderful as Warriors win thriller over Tigers - NRL.COM
Diana interview: Whistleblower wants apology from BBC bosses - BBC News
Latest Gombak death-in-custody due to heart attack, says S'gor CPO - Malaysiakini
Prince Harry tells Oprah that Diana's death led him to drink and drugs, accuses royals of 'total neglect' - NBC News
Manchester United passed on signing Man City star Ruben Dias - Manchester Evening News
Prince Harry: pain of Diana's death pushed me to drink and drugs - The Star Online
Kim Kardashian Accused Kourtney Kardashian Of Constantly "Degrading" Her Staff After A Brutal Confrontation With Her Nanny - BuzzFeed News
Overwatch 2 PvP is 5v5, Bastion reworked "from the ground up" - Eurogamer.net
Hospital staff 'should have checked for blood clots', say SA family - 9News
Cloudy skies to continue in Southern Luzon, Visayas and Mindanao due to ITCZ - INQUIRER.net
16-year-old Pune boy merges 50,000 images of Moon to create detailed picture - The Tribune
UK to offer Australia tariff-free trade deal despite farmers' fears - BBC News
Disha Patani does backflip to new BTS song Butter and Tiger Shroff is impressed, watch - Hindustan Times
Mop-up operations begin in Cape Town after flooding caused by heavy rain - News24
New type of coronavirus originating in dogs found — study - The Edge Markets MY
House panel OKs bill for creation of PH virology institute, CDC - ABS-CBN News
Can't render this file because it contains an unexpected character in line 12 and column 58.

View File

@ -1,2 +1,2 @@
speed,brightness
m,5
f,5
1 speed brightness
2 m f 5

View File

@ -1,22 +1,22 @@
name,current,opening
MSFT,243.03,241.8
NFLX,486.66,489.13
GOOG,2261.97,2261.09
TSLA,571.69,601.545
AAPL,124.97,124.58
INTC,54.01,54.04
TXN,178.99,179.99
HPQ,32.39,32
HOG,46.25,45.35
LUV,59.93,58.97
WMT,138.24,136.23
BJ,45.99,45.63
ETSY,156.59,163.3368
G,44.51,43.84
GDDY,79.29,80.92
GNRC,293.36,294.17
PEP,146.37,144.24
STM,34.81,35.28
YELP,38.14,37.39
XRAY,66.61,65.83
ZTS,170.85,168.37
MSFT,246.48,243.96
NFLX,501.67,489.55
GOOG,2356.09,2328.04
TSLA,586.78,575
AAPL,127.31,125.23
INTC,55.95,55.57
TXN,185.3,182.28
HPQ,32.04,31.98
HOG,48.28,48.99
LUV,59.92,60.92
WMT,142.42,141.5
BJ,46.16,48.26
ETSY,172.36,166.28
G,44.26,43.83
GDDY,81.4,81.18
GNRC,308.53,304.56
PEP,147.23,145.25
STM,36.18,35.79
YELP,38.38,37.84
XRAY,67.95,67.72
ZTS,175.8,171.52

1 name current opening
2 MSFT 243.03 246.48 241.8 243.96
3 NFLX 486.66 501.67 489.13 489.55
4 GOOG 2261.97 2356.09 2261.09 2328.04
5 TSLA 571.69 586.78 601.545 575
6 AAPL 124.97 127.31 124.58 125.23
7 INTC 54.01 55.95 54.04 55.57
8 TXN 178.99 185.3 179.99 182.28
9 HPQ 32.39 32.04 32 31.98
10 HOG 46.25 48.28 45.35 48.99
11 LUV 59.93 59.92 58.97 60.92
12 WMT 138.24 142.42 136.23 141.5
13 BJ 45.99 46.16 45.63 48.26
14 ETSY 156.59 172.36 163.3368 166.28
15 G 44.51 44.26 43.84 43.83
16 GDDY 79.29 81.4 80.92 81.18
17 GNRC 293.36 308.53 294.17 304.56
18 PEP 146.37 147.23 144.24 145.25
19 STM 34.81 36.18 35.28 35.79
20 YELP 38.14 38.38 37.39 37.84
21 XRAY 66.61 67.95 65.83 67.72
22 ZTS 170.85 175.8 168.37 171.52

Binary file not shown.

Before

Width:  |  Height:  |  Size: 747 B

After

Width:  |  Height:  |  Size: 821 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.2 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

BIN
final.ppm

Binary file not shown.

0
log.txt Executable file
View File

Binary file not shown.

View File

@ -229,8 +229,8 @@ def AddLogo():
@app.route("/matrix", methods=['POST'])
def matrix():
global LastCommand
if "Run Display" in request.form:
global LastCommand
if "Run Stocks" in request.form:
pass
print('run display')
#stock_ticker.runStockTicker(command, DelayTime, speedTime)
@ -240,13 +240,24 @@ def matrix():
#child.sendline('sent from server')
#process = Popen(["sudo", "-E", "python3", "stockTicker.py"], stdin=PIPE, stdout=PIPE, stderr=PIPE)
ticker.sendline('K')
ticker.sendline('S')
print('back in flask')
LastCommand = 'Run display'
elif "Stop Display (at next refresh)" in request.form:
if "Run News" in request.form:
pass
print('run display')
ticker.sendline('K')
ticker.sendline('N')
print('back in flask')
LastCommand = 'Run display'
elif "Stop Display (at next refresh)" in request.form:
pass
print('run display')
#ticker.kill()
@ -256,14 +267,14 @@ def matrix():
ticker.sendline('K')
except:
print("none running")
elif "Shutdown the pi" in request.form:
elif "Shutdown the pi" in request.form:
pass
try:
LastCommand = 'shutdown'
os.system("sudo shutdown now")
except:
print("couldn't shutdown")
return hello()
return hello()
if __name__ == "__main__":
app.run(host='0.0.0.0', port=1024, debug=False) # the debuggger causes flickering

View File

@ -97,7 +97,21 @@ class StockTicker():
offset_x -= 1
self.setImage(image, offset_x = offset_x, offset_y = offset_y)
time.sleep(delay)
try:
msg = getInput()
if msg == 'K':
self.resetMatrix()
return True
self.process_msg(msg)
except KeyboardInterrupt:
sys.stdout.flush()
pass
time.sleep(self.delay)
return False
def scrollImageTransition(self, image_files, offset_x = 0, offset_y = 0, stocks = True):
# use two image files and switch between them with a seemless transition
@ -138,9 +152,12 @@ class StockTicker():
time.sleep(self.delay)
try:
msg = getInput()
if msg == 'K':
self.resetMatrix()
kill = True
image1.close()
image2.close()
break
self.process_msg(msg)
@ -148,7 +165,9 @@ class StockTicker():
sys.stdout.flush()
pass
image1.close()
image2.close()
if kill: break
if stocks:
@ -159,7 +178,9 @@ class StockTicker():
current_img = 1
offset_x = 0
def displayText(self):
def displayTextRepeating(self):
f = open('csv/scroll_text.csv', 'r')
CSV = csv.reader(f)
@ -181,37 +202,80 @@ class StockTicker():
img.save('scroll_text.ppm')
self.scrollImageTransition(['scroll_text.ppm', 'scroll_text.ppm'], offset_x = 128, offset_y = 0, stocks = False)
def displayText(self, text, font):
def displayGIF(self, gif_file):
im = Image.open(gif_file)
width, height = self.get_text_dimensions(text, font)
print(text)
print('dims:', width, height)
img = Image.new('RGB', (width + 50, 32))
d = ImageDraw.Draw(img)
# To iterate through the entire gif
i = 0
while 1:
print(im.tell())
try:
im.seek(i)
except EOFError:
print('finished')
i = 0
im.seek(i)
# do something to im
self.setImage(im.convert('RGB'))
time.sleep(0.5)
i += 1
try:
msg = getInput()
if msg == 'K':
self.resetMatrix()
break
self.process_msg(msg)
except KeyboardInterrupt:
sys.stdout.flush()
pass
d.text((4, 0), text, fill=(255, 255, 255), font=font)
img.save('scroll_text.ppm')
return self.scrollImage('scroll_text.ppm', offset_x = 128, offset_y = 0)
def displayNews(self):
font = ImageFont.load("./fonts/8x13.pil")
while True:
headlines = []
f = open('csv/news.csv', 'r')
CSV = csv.reader(f)
next(CSV)
for row in CSV:
headlines.append(','.join(row))
f.close()
for headline in headlines:
killed = self.displayText(headline, font)
if killed:
kill = True
if kill: break
if kill: break
def displayGIF(self, gif_file):
with open('log.txt', "w") as log:
try:
im = Image.open(gif_file)
# To iterate through the entire gif
i = 0
while 1:
print(im.tell())
try:
im.seek(i)
except EOFError:
print('finished')
i = 0
im.seek(i)
# do something to im
self.setImage(im.convert('RGB'))
time.sleep(0.5)
i += 1
try:
msg = getInput()
if msg == 'K':
im.close()
self.resetMatrix()
break
self.process_msg(msg)
except KeyboardInterrupt:
sys.stdout.flush()
pass
except Exception as e:
log.write(str(e))
#Using change between min and day price give appropriate arrow
@ -361,6 +425,8 @@ class StockTicker():
#os.system("sudo ./demo -D1 final.ppm -t " + str(displayTime) +" -m "+ str(speedDisplay) +" --led-gpio-mapping=adafruit-hat --led-rows=32 --led-cols=256")
#os.system("sudo ./demo -D1 final.ppm -t " + str(self.displayTime) +" -m "+ str(self.speedDisplay) +" --led-gpio-mapping=adafruit-hat --led-rows=64 --led-cols=64 --led-slowdown-gpio=4 ")
self.scrollImageTransition(['final.ppm', 'final.ppm'], offset_x = 0, offset_y = 0)
#Retrieve symbols and stock info from the csv file
def readCSV(self):
@ -441,6 +507,8 @@ class StockTicker():
self.getFullStockImage(1)
self.displayStocks()
elif msg == 'N': #news
self.displayNews()
# speed settings
elif msg == 's':
self.delay = 0.03
@ -459,7 +527,7 @@ class StockTicker():
elif msg == 'T':# text
self.displayText()
self.displayTextRepeating()
elif msg == 'I': # image
@ -471,6 +539,7 @@ class StockTicker():
self.displayGIF(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'display_gif'))
elif msg == 'K': # kill
self.resetMatrix()
@ -478,7 +547,10 @@ if __name__ == '__main__':
#print(sys.stdin.readlines())
stock_ticker = StockTicker()
#stock_ticker.process_msg('f')
#stock_ticker.displayNews()
#stock_ticker.displayGIF('/home/pi/Desktop/stock_ticker/gifs/open.gif')
#stock_ticker.displayGIF('/home/pi/Desktop/stock_ticker/gifs/close.gif')

View File

@ -10,7 +10,8 @@
<br><br>
<form action="/matrix" method="POST">
<input type="submit" name="Run Display" value="Run Display" style="height:50px" >
<input type="submit" name="Run Stocks" value="Run Stocks" style="height:50px" >
<input type="submit" name="Run News" value="Run News" style="height:50px" >
<input type="submit" name="Stop Display (at next refresh)" value="Stop Display"style="height:50px">
<input type="submit" name="Shutdown the pi" value="Shutdown the pi"style="height:50px">
</form>

View File

@ -14,11 +14,7 @@ import pexpect
from pycoingecko import CoinGeckoAPI
if __name__ == '__main__':
cg = CoinGeckoAPI()
print(cg.get_price(ids='bitcoin, ethereum', vs_currencies = 'usd', include_24hr_change=True))

2
test.txt Executable file
View File

@ -0,0 +1,2 @@
start
else: message: