-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstockticker-ledboard.py
189 lines (147 loc) · 4.48 KB
/
stockticker-ledboard.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Raspberry Pi and MAX7219 LED Board Stock Symbol Price Monitor
# A simple stock ticker price change monitor of today's positive or negative change using a MAX7219 LED Board
import re
import time
import argparse
import atexit
import sys
import os
import decimal
import errno
import signal
import urllib2
# import ystockquote
import json
import requests
from luma.led_matrix.device import max7219
from luma.core.interface.serial import spi, noop
from luma.core.render import canvas
from luma.core.virtual import viewport
from luma.core.legacy import text, show_message
from luma.core.legacy.font import proportional, CP437_FONT, TINY_FONT, SINCLAIR_FONT, LCD_FONT
from socket import error as SocketError
debug = 0
# Stock quote configuration:
tickerSymbol = 'AAPL'
# MAX7219 configuration:
serial = spi(port=0, device=0, gpio=noop())
device = max7219(serial, cascaded=4, block_orientation=-90, rotate=0)
# Turn off all the LEDs
def lightsOut():
device.cleanup()
# Run lightsOut at exit
atexit.register(lightsOut)
# Console message and LEDs off while market closed
def marketClosed():
print "Stock Market Closed.", now
lightsOut()
time.sleep(60)
# Debugging
if debug == 1:
allInfo = ystockquote.get_all(tickerSymbol)
print allInfo
decimal.getcontext().prec = 8
# Handle timeouts gracefully
def timeoutHandler(signum, frame):
print "Error: Timeout fetching quote."
lightsOut()
time.sleep(10)
signal.alarm(0)
return
signal.signal(signal.SIGALRM, timeoutHandler)
# Scroll message across the LCD Panel
# msg = "This is a test! 123456789 ABCDEFG"
# # Main program logic follows:
# if __name__ == '__main__':
# try:
# while True:
# print(msg)
# show_message(device, msg, fill="white", font=proportional(LCD_FONT))
# Main function to get the quote and animate the LEDs
def getQuote(change):
if debug == 1:
print "Function getQuote"
try:
signal.alarm(10)
rsp = requests.get('https://finance.google.com/finance?q=' + tickerSymbol + '&output=json')
if rsp.status_code in (200,):
fin_data = json.loads(rsp.content[6:-2].decode('unicode_escape'))
print('Change: {}'.format(fin_data['c']))
change = format(fin_data['c'])
except urllib2.HTTPError as err:
if err.code == 404:
print "Error: 404 Not Found."
else:
print "Error: ", err.code
except urllib2.URLError as err:
print "Error: Connection reset by peer."
except SocketError as err:
if err.errno == errno.ECONNRESET:
print "Error: Connection reset by peer."
# price = ystockquote.get_price(tickerSymbol)
# while change == 'N/A':
# print "Error: No price change data."
# time.sleep(60)
# change = ystockquote.get_change(tickerSymbol)
# changedecimal = decimal.Decimal(change)
# pricedecimal = Decimal(price)
# changedecimal = 0
# Reset timeout
signal.alarm(0)
# Console message with price change
print "$ Change: ", change
# print pricedecimal
# lastclose = Decimal(pricedecimal) - Decimal(changedecimal)
# print lastclose
show_message(device, (str (change)), fill="white", font=proportional(LCD_FONT))
time.sleep(1)
# sense.show_message(str (changedecimal), text_colour=[LED_BRIGHTNESS, 0, 0])
# Main program logic follows:
if __name__ == '__main__':
try:
while True:
now = datetime.datetime.now()
# print now
# Between Monday - Friday
if 0 <= now.weekday() <= 5:
if debug == 1:
print "Weekday: ", now.weekday()
# print now.hour
# Between 9AM - 5PM
if now.hour == 9:
if debug == 1:
print "Hour 9AM"
if now.minute >= 30:
if debug == 1:
print "Minute 30 or later"
if 9 <= now.hour <= 17:
# print "Hour: ", now.hour
# print now.time()
if debug == 1:
print "Calling function getQuote"
getQuote(0)
else:
marketClosed()
else:
if 9 <= now.hour <= 15:
if debug == 1:
print "Hour between 10AM-4PM"
# print "Hour: ", now.hour
# print now.time()
if debug == 1:
print "Calling function getQuote"
getQuote(0)
else:
marketClosed()
else:
marketClosed()
# Handle Ctrl-C gracefully
except KeyboardInterrupt:
print ' - Exiting'
lightsOut()
try:
sys.exit(0)
except SystemExit:
os._exit(0)