Advertisement
Guest User

Stock Ticker for LED

a guest
Jul 11th, 2014
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.64 KB | None | 0 0
  1. import os
  2. from PIL import ImageFont
  3. from PIL import Image
  4. from PIL import ImageDraw
  5. from threading import Thread
  6. import time
  7. import urllib
  8.  
  9. # ************* BEGIN STOCK SCRIPT **************
  10.  
  11. class Stocks(Thread):
  12. def __init__(self, i):
  13. super(Stocks, self).__init__()
  14. self.running = True
  15.  
  16. def run(self):
  17. time.sleep(5)
  18. symlist = ["AAPL", "GOOG", "TSLA"] #Insert stock ticker symbols here
  19.  
  20. def __request(symbol, stat):
  21. url = 'http://finance.yahoo.com/d/quotes.csv?s=%s&f=%s' % (symbol, stat)
  22. return urllib.urlopen(url).read().strip().strip('"')
  23.  
  24. def get_all(symbol):
  25. """
  26. Get all available quote data for the given ticker symbol.
  27.  
  28. Returns a dictionary.
  29. """
  30. values = __request(symbol, 'l1c1va2xj1b4j4dyekjm3m4rr5p5p6s7').split(',')
  31. stock_data = {}
  32. stock_data['price'] = values[0]
  33. stock_data['change'] = values[1]
  34. stock_data['volume'] = values[2]
  35. stock_data['avg_daily_volume'] = values[3]
  36. stock_data['stock_exchange'] = values[4]
  37. stock_data['market_cap'] = values[5]
  38. stock_data['book_value'] = values[6]
  39. stock_data['ebitda'] = values[7]
  40. stock_data['dividend_per_share'] = values[8]
  41. stock_data['dividend_yield'] = values[9]
  42. stock_data['earnings_per_share'] = values[10]
  43. stock_data['52_week_high'] = values[11]
  44. stock_data['52_week_low'] = values[12]
  45. stock_data['50day_moving_avg'] = values[13]
  46. stock_data['200day_moving_avg'] = values[14]
  47. stock_data['price_earnings_ratio'] = values[15]
  48. stock_data['price_earnings_growth_ratio'] = values[16]
  49. stock_data['price_sales_ratio'] = values[17]
  50. stock_data['price_book_ratio'] = values[18]
  51. stock_data['short_ratio'] = values[19]
  52. return stock_data
  53.  
  54. def get_price(symbol):
  55. return __request(symbol, 'l1') # get Last Trade (Price Only)
  56.  
  57. def get_change(symbol):
  58. return __request(symbol, 'c1') # get Change
  59.  
  60. def get_volume(symbol):
  61. return __request(symbol, 'v') # get more info
  62.  
  63. def get_avg_daily_volume(symbol):
  64. return __request(symbol, 'a2') # get Average Daily Volume
  65.  
  66. def get_stock_exchange(symbol):
  67. return __request(symbol, 'x') # get Stock Exchange
  68.  
  69. def get_market_cap(symbol):
  70. return __request(symbol, 'j1') # get Market Capitalization
  71.  
  72. def get_book_value(symbol):
  73. return __request(symbol, 'b4') # get Book Value
  74.  
  75. def get_ebitda(symbol):
  76. return __request(symbol, 'j4') # get EBITDA
  77.  
  78. def get_dividend_per_share(symbol):
  79. return __request(symbol, 'd') # get Divident per share
  80.  
  81. def get_dividend_yield(symbol):
  82. return __request(symbol, 'y') # get Dividend Yield
  83.  
  84. def get_earnings_per_share(symbol):
  85. return __request(symbol, 'e') # get Earnings per Share
  86.  
  87. def get_52_week_high(symbol):
  88. return __request(symbol, 'k') # get 52 Week High
  89.  
  90. def get_52_week_low(symbol):
  91. return __request(symbol, 'j') # get 52 week Low
  92.  
  93. def get_50day_moving_avg(symbol):
  94. return __request(symbol, 'm3') # get 50 Day Moving Average
  95.  
  96. def get_200day_moving_avg(symbol): # get 200 Day Moving Average
  97. return __request(symbol, 'm4')
  98.  
  99. def get_price_earnings_ratio(symbol):
  100. return __request(symbol, 'r') # get P/E Ratio
  101.  
  102. def get_price_earnings_growth_ratio(symbol):
  103. return __request(symbol, 'r5') # get PEG Ratio
  104.  
  105. def get_price_sales_ratio(symbol):
  106. return __request(symbol, 'p5') # get Price / Sales
  107.  
  108. def get_price_book_ratio(symbol):
  109. return __request(symbol, 'p6') # get Price / Book
  110.  
  111. def get_short_ratio(symbol):
  112. return __request(symbol, 's7') # get Short Ratio
  113.  
  114. def get_prev_close(symbol):
  115. return __request(symbol, 'p') # get Prev close
  116.  
  117. def get_open(symbol):
  118. return __request(symbol, 'o') # get Open
  119.  
  120. for sym in symlist:
  121. if float(get_change(sym)) > 0:
  122. text = "((""\"" + sym + " \", (95, 158, 160)), " + "(\"$" + get_price(sym) + " " + get_change(sym) + "\", (0, 255, 0)))"
  123. elif float(get_change(sym)) < 0:
  124. text = "((""\"" + sym + " \", (95, 158, 160)), " + "(\"$" + get_price(sym) + " " + get_change(sym) + "\", (255, 0, 0)))"
  125. else:
  126. text = "((""\"" + sym + " \", (95, 158, 160)), " + "(\"$" + get_price(sym) + " " + get_change(sym) + "\", (0, 0, 255)))"
  127.  
  128. # ************* BEGIN LED DISPLAY **************
  129.  
  130. font = ImageFont.truetype("/usr/share/fonts/truetype/freefont/FreeSans.ttf", 16)
  131. all_text = ""
  132. for text_color_pair in text:
  133. t = text_color_pair[0]
  134. all_text = all_text + t
  135.  
  136. print(all_text)
  137. width, ignore = font.getsize(all_text)
  138. print(width)
  139.  
  140.  
  141. im = Image.new("RGB", (width + 30, 16), "black")
  142. draw = ImageDraw.Draw(im)
  143.  
  144. x = 0;
  145. for text_color_pair in text:
  146. t = text_color_pair[0]
  147. c = text_color_pair[1]
  148. print("t=" + t + " " + str(c) + " " + str(x))
  149. draw.text((x, 0), t, c, font=font)
  150. x = x + font.getsize(t)[0]
  151.  
  152.  
  153. im.save("test.ppm")
  154.  
  155. os.system("./led-matrix 1 test.ppm")
  156.  
  157. # ************* END LED DISPLAY **************
  158.  
  159. if self.running == False:
  160. return
  161. self.run()
  162.  
  163. thread = Stocks(0)
  164. thread.start()
  165.  
  166. while True:
  167. prompt = raw_input('')
  168. if prompt == 'quit':
  169. thread.running = False
  170. break
  171. # ************* END STOCK SCRIPT **************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement