Skip to content

Instantly share code, notes, and snippets.

@seanmckaybeck
Created March 2, 2015 02:48
Show Gist options
  • Save seanmckaybeck/08a4bac104601e8e3fb1 to your computer and use it in GitHub Desktop.
Save seanmckaybeck/08a4bac104601e8e3fb1 to your computer and use it in GitHub Desktop.
import time
from subprocess import Popen
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw
import requests
from bs4 import BeautifulSoup
URL = 'http://www.silverpriceoz.com/silver-price-per-ounce/'
def get_silver_price():
r = requests.get(URL)
soup = BeautifulSoup(r.content)
price_text = soup.find_all('b')[2].text.lstrip()
return price_text[0:5]
def save_image():
price = get_silver_price()
text = (("Price per ounce $", (0, 255, 0)), (price, (192, 192, 192)))
font = ImageFont.truetype("/usr/share/fonts/TTF/FreeSans.ttf", 16)
all_text = ""
for text_color_pair in text:
t = text_color_pair[0]
all_text = all_text + t
print(all_text)
width, ignore = font.getsize(all_text)
print(width)
im = Image.new("RGB", (width + 30, 16), "black")
draw = ImageDraw.Draw(im)
x = 0;
for text_color_pair in text:
t = text_color_pair[0]
c = text_color_pair[1]
print("t=" + t + " " + str(c) + " " + str(x))
draw.text((x, 0), t, c, font=font)
x = x + font.getsize(t)[0]
im.save("test.ppm")
def main():
while True:
save_image()
p = Popen(['./led-matrix', '1', 'test.ppm'])
time.sleep(600) # 10 minutes
p.terminate()
p.wait()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment