Created
April 12, 2022 17:18
-
-
Save stolk/508bd6d98d599034d7ceb275cbb0f2ad to your computer and use it in GitHub Desktop.
TurboLEDz Stock Ticker Customization Script.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# | |
# set_ticker_portfolio.py | |
# | |
# (c)2022 Game Studio Abraham Stolk Inc. | |
# | |
# This utility sets the ticker symbols that the TurboLEDz stock ticker will retrieve. | |
# Default set that the device ships with is comprised of the corporations in the DOW30 index: | |
# | |
# Only works with: | |
# * ticker symbols of length 4 or shorter. | |
# * equities/ETFs but not mutual funds. | |
# * symbols without a dot character (E.g. HOT.UN or RY.TO not allowed.) | |
# * up to 30 symbols. | |
# | |
# Sample usage: | |
# ./set_ticker_portfolio.py 10.0.99.127 RBLX SBRA MCD AAPL UAN PFE NYMT EBF BGSF TG PAC INN BROS VYM | |
# | |
# If less than 30 symbols are supplied, the list is padded with DOW30 stocks. | |
import socket | |
import sys | |
import json | |
from urllib.request import urlopen | |
argc = len(sys.argv) | |
# We need at least one ticker symbol. | |
if argc < 2+1 : | |
print("Usage:", sys.argv[0], "device-ip-addr AAPL [IBM GOOG ... CSCO]") | |
sys.exit(1) | |
UDP_IP = sys.argv[1] | |
UDP_PORT = 28 | |
# Check if all symbols are valid ticker symbols on NYSE/NASDAQ. | |
symbolstr = str(sys.argv[2:]).replace(" ","").replace("'","").replace("[","").replace("]","") | |
url = "https://query1.finance.yahoo.com/v7/finance/quote?symbols=" + symbolstr + "&fields=regularMarketPrice,regularMarketChangePercent" | |
f = urlopen(url) | |
response = json.load(f) | |
qr = response['quoteResponse'] | |
quotes = qr['result'] | |
validsymbols=[] | |
for q in quotes: | |
sym = q['symbol'] | |
if len(sym)<=4 and (not '.' in sym) and 'regularMarketChangePercent' in q and 'regularMarketPrice' in q : | |
validsymbols.append(sym) | |
print("Valid symbols:", validsymbols) | |
if not validsymbols: | |
sys.exit(2) | |
# Pad to 30 symbols | |
for padsym in [ "AXP","AMGN","AAPL","BA","CAT","CSCO","CVX","GS","HD","HON","IBM","INTC","JNJ","KO","JPM","MCD","MMM","MRK","MSFT","NKE","PG","TRV","UNH","CRM","VZ","V","WBA","WMT","DIS","DOW" ] : | |
if len(validsymbols) < 30 and padsym not in validsymbols: | |
validsymbols.append(padsym) | |
MESSAGE = "" | |
for sym in validsymbols: | |
MESSAGE += sym | |
MESSAGE += ',' | |
MESSAGE = MESSAGE[:-1] | |
print("UDP target IP: %s" % UDP_IP) | |
print("UDP target port: %s" % UDP_PORT) | |
print("message: %s" % MESSAGE) | |
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # Internet,UDP | |
sock.sendto(MESSAGE.encode(), (UDP_IP, UDP_PORT)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment