Last active
July 12, 2017 02:46
-
-
Save michael34435/1973ae9e6a9223e9d3a2f235ebd02af6 to your computer and use it in GitHub Desktop.
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/python | |
# -*- coding: utf-8 -*- | |
# <bitbar.title>Yahoo Finance Stock Ticker</bitbar.title> | |
# <bitbar.version>1.0</bitbar.version> | |
# <bitbar.author>Michael.K</bitbar.author> | |
# <bitbar.author.github>michael34435</bitbar.author.github> | |
# <bitbar.desc>Provides a rotating stock ticker in your menu bar</bitbar.desc> | |
# <bitbar.image>https://s3.amazonaws.com/jamieson.io/bitbar_gfinance.png</bitbar.image> | |
# <bitbar.dependencies>python</bitbar.dependencies> | |
import urllib2 | |
import urllib | |
import StringIO | |
import csv | |
stocks=["TSLA", "XAUUSD=X"] | |
query = { | |
"f": "sc1l1", | |
"s": "+".join(stocks) | |
} | |
url = "http://download.finance.yahoo.com/d/quotes.csv?%s" % urllib.urlencode(query) | |
u = urllib2.urlopen(url) | |
scsv = u.read() | |
f = StringIO.StringIO(scsv) | |
for ticker in csv.reader(f, delimiter=','): | |
symbol = ticker[0] | |
change = float(ticker[1]) | |
price = float(ticker[2]) | |
start = price - change | |
percent = (price - start) / start | |
change = round(change, 2) | |
price = round(price, 2) | |
percent = round(percent * 100, 2) | |
change = ("+" + str(change)) if change > 0 else change | |
print "{} {} {} {}({}%)| font=黑體-繁 size=12 color=".format("▼" if change < 0 else "▲", symbol, price, change, percent), "red" if change < 0 else "green" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment