diff --git a/__pycache__/stockTicker.cpython-37.pyc b/__pycache__/stockTicker.cpython-37.pyc index 7f82519..a90e8ab 100644 Binary files a/__pycache__/stockTicker.cpython-37.pyc and b/__pycache__/stockTicker.cpython-37.pyc differ diff --git a/api_caller.py b/api_caller.py index e335861..c941a0e 100644 --- a/api_caller.py +++ b/api_caller.py @@ -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) diff --git a/csv/crypto.csv b/csv/crypto.csv index d7fad47..1a752af 100644 --- a/csv/crypto.csv +++ b/csv/crypto.csv @@ -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 diff --git a/csv/last_update.csv b/csv/last_update.csv index 7ff27cf..9a20847 100644 --- a/csv/last_update.csv +++ b/csv/last_update.csv @@ -1 +1 @@ -14/05/2021 06:29:45 +21/05/2021 09:21:44 diff --git a/csv/news.csv b/csv/news.csv new file mode 100644 index 0000000..8aa2bb9 --- /dev/null +++ b/csv/news.csv @@ -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 diff --git a/csv/settings.csv b/csv/settings.csv index bcf18cd..c970da5 100644 --- a/csv/settings.csv +++ b/csv/settings.csv @@ -1,2 +1,2 @@ speed,brightness -m,5 \ No newline at end of file +f,5 \ No newline at end of file diff --git a/csv/tickers.csv b/csv/tickers.csv index 089745b..f9cd618 100644 --- a/csv/tickers.csv +++ b/csv/tickers.csv @@ -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 diff --git a/display_gif b/display_gif index c78d7a0..1eadae4 100644 Binary files a/display_gif and b/display_gif differ diff --git a/display_image b/display_image index 1b52aea..7538ced 100644 Binary files a/display_image and b/display_image differ diff --git a/final.ppm b/final.ppm index 72c19b9..009e314 100755 Binary files a/final.ppm and b/final.ppm differ diff --git a/log.txt b/log.txt new file mode 100755 index 0000000..e69de29 diff --git a/scroll_text.ppm b/scroll_text.ppm index 9a254cf..f4d2363 100755 Binary files a/scroll_text.ppm and b/scroll_text.ppm differ diff --git a/server.py b/server.py index bbb882e..30e2aaa 100644 --- a/server.py +++ b/server.py @@ -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 diff --git a/stockTicker.py b/stockTicker.py index 2acd89a..190970b 100644 --- a/stockTicker.py +++ b/stockTicker.py @@ -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') diff --git a/templates/index.html b/templates/index.html index 0799a28..2a8bb68 100644 --- a/templates/index.html +++ b/templates/index.html @@ -10,7 +10,8 @@

- + +
diff --git a/test.py b/test.py index d277b27..201a80c 100644 --- a/test.py +++ b/test.py @@ -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)) - + diff --git a/test.txt b/test.txt new file mode 100755 index 0000000..574dc93 --- /dev/null +++ b/test.txt @@ -0,0 +1,2 @@ +start +else: message: \ No newline at end of file