-
-
Save it-one-mm/68816f50fb995947e483583b8d614df1 to your computer and use it in GitHub Desktop.
THAILAND SETTRADE for scrollphathd
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
import requests | |
import time | |
import scrollphathd | |
from scrollphathd.fonts import font3x5 | |
class SETTRADE: | |
def __init__(self): | |
self.url = 'https://api.settrade.com/api/market/SET/info' | |
def run(self): | |
req = requests.get(self.url) | |
if req.status_code == 200: | |
return self.extract_data(req.json()) | |
@staticmethod | |
def extract_data(j): | |
data = { | |
# 'status': j['market_status'], | |
'last': j['index'][0]['last'], | |
'change': j['index'][0]['change'], | |
'percent_change': round(j['index'][0]['percent_change'], 2) | |
} | |
return ("SET:{:+.2f}({:+.2f}%){:.2f}".format( | |
data['change'], | |
data['percent_change'], | |
data['last'] | |
) | |
) | |
def scroll_message(message): | |
scrollphathd.clear() | |
length = scrollphathd.write_string(message, y=2) | |
scrollphathd.show() | |
time.sleep(0.5) | |
length -= scrollphathd.width | |
while length > 0: | |
scrollphathd.scroll(1) | |
scrollphathd.show() | |
length -= 1 | |
time.sleep(0.05) | |
time.sleep(0.5) | |
if __name__ == "__main__": | |
settrade = SETTRADE() | |
scrollphathd.set_font(font3x5) | |
scrollphathd.set_brightness(0.1) | |
try: | |
while True: | |
data = settrade.run() | |
if data: | |
print(data) | |
scroll_message(data) | |
time.sleep(2) | |
except KeyboardInterrupt: | |
print("Exiting!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment