Created
September 19, 2018 02:05
-
-
Save ShinJJang/458f9a2bb56ddae5daa569f3fde70eb2 to your computer and use it in GitHub Desktop.
Version of changing title like stock board https://getbitbar.com/plugins/Finance/realtime-stock-tracker.5s.py
This file contains hidden or 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/env python | |
# | |
# <bitbar.title>Realtime Stock Tracker</bitbar.title> | |
# <bitbar.version>v1.0</bitbar.version> | |
# <bitbar.author>Bogdan Mosincat</bitbar.author> | |
# <bitbar.author.github>bogdan1304</bitbar.author.github> | |
# <bitbar.desc>Shows realtime stock price and daily percentage change for each stock in the list.</bitbar.desc> | |
# <bitbar.image>https://i.imgur.com/hQoCXFL.png</bitbar.image> | |
# <bitbar.dependencies>python</bitbar.dependencies> | |
import json, urllib2 | |
import time | |
# print '\xF0\x9F\x93\x8A' | |
# print '---' | |
def get_stock_price(stock): | |
response = urllib2.urlopen('https://api.iextrading.com/1.0/stock/' + stock + '/quote') | |
return json.loads(response.read()) | |
def create_output_string(stock, response): | |
output = stock | |
output += " - $" | |
output += "{:0.2f}".format(response["latestPrice"]) | |
output += " (" + "{:0.2f}".format(response["changePercent"] * 100.00) + "%)" | |
color = "red" if response["changePercent"] < 0 else "green" | |
quote_url = 'https://www.finance.yahoo.com/quote/' + stock | |
output += " | color=" + color + " href=" + quote_url | |
return output | |
def render(stock): | |
response = get_stock_price(stock) | |
print(create_output_string(stock, response)) | |
stocks = ["NVDA", "AMD", "PYPL", "FB", "AAPL", "MSFT", "GOOGL", "AMZN", "NFLX"] | |
show_index = int(time.time()) / 5 % len(stocks) | |
render(stocks[show_index]) | |
print('---') | |
for stock in stocks: | |
render(stock) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment