Skip to content

Instantly share code, notes, and snippets.

@bdebon
Last active May 10, 2019 12:16
Show Gist options
  • Select an option

  • Save bdebon/068920ec7fabdf9b21bd5177994d3832 to your computer and use it in GitHub Desktop.

Select an option

Save bdebon/068920ec7fabdf9b21bd5177994d3832 to your computer and use it in GitHub Desktop.
Raspberry Pi RGB LED MATRIX - YouTube Counter
#!/usr/bin/env python
# Display a runtext with double-buffering.
from samplebase import SampleBase
from rgbmatrix import graphics
import time
import urllib.request
import json
import datetime
class RunText(SampleBase):
def __init__(self, *args, **kwargs):
super(RunText, self).__init__(*args, **kwargs)
self.parser.add_argument("-t", "--text", help="The text to scroll on the RGB LED panel", default="000864")
def run(self):
offscreen_canvas = self.matrix.CreateFrameCanvas()
font = graphics.Font()
font.LoadFont("../../../fonts/8x13B.bdf")
textColor = graphics.Color(255, 255, 255)
pos = offscreen_canvas.width
my_text = self.args.text
max_brightness = self.matrix.brightness
# youtube part
key = "YOUR_KEY"
subs = "999999"
#### colors
youtubeRed = graphics.Color(255, 0, 0)
white = graphics.Color(255, 255, 255)
shadowWhite = graphics.Color(172, 172, 172)
def formatCount(subs):
if subs <= 9:
subs = "00000" + format(int(subs))
elif subs <= 99:
subs = "0000" + format(int(subs))
elif subs <= 999:
subs = "000" + format(int(subs))
elif subs <= 9999:
subs = "00" + format(int(subs))
elif subs <= 99999:
subs = "0" + format(int(subs))
return subs
def getYoutubeSubscribers(self):
try:
data = urllib.request.urlopen(
"https://www.googleapis.com/youtube/v3/channels?part=statistics&id=UCLOAPb7ATQUs_nDs9ViLcMw&key=" + key).read()
subs = json.loads(data.decode('utf-8'))["items"][0]["statistics"]["subscriberCount"]
print("Benjamin Code has " + formatCount(int(subs)) + " subscribers!")
return formatCount(int(subs))
except:
print("No network")
return '000000'
def getTime():
d = datetime.datetime.now()
print("Il est : " + str(d.hour) + ':' + str(d.minute))
def lowBrightness():
d = datetime.datetime.now()
# if we are in summer hour
if d.month >= 4 and d.month < 11 :
if d.hour >= 20 or d.hour <= 7 :
return True
else :
if d.hour >= 18 or d.hour <= 9 :
return True
return False
def DrawYouTubeLogo(self):
y = 0
graphics.DrawLine(offscreen_canvas, 1, y, 14, y, youtubeRed) # first line
y = 1
graphics.DrawLine(offscreen_canvas, 0, y, 15, y, youtubeRed) # seconde line
y = 2
graphics.DrawLine(offscreen_canvas, 0, y, 15, y, youtubeRed) # seconde line
y = 3
graphics.DrawLine(offscreen_canvas, 0, y, 15, y, youtubeRed) # seconde line
y = 4
graphics.DrawLine(offscreen_canvas, 0, y, 2, y, youtubeRed)
graphics.DrawLine(offscreen_canvas, 3, y, 5, y, shadowWhite)
graphics.DrawLine(offscreen_canvas, 6, y, 15, y, youtubeRed)
y = 5
graphics.DrawLine(offscreen_canvas, 0, y, 2, y, youtubeRed)
graphics.DrawLine(offscreen_canvas, 3, y, 4, y, white)
graphics.DrawLine(offscreen_canvas, 5, y, 7, y, shadowWhite)
graphics.DrawLine(offscreen_canvas, 8, y, 15, y, youtubeRed)
y = 6
graphics.DrawLine(offscreen_canvas, 0, y, 2, y, youtubeRed)
graphics.DrawLine(offscreen_canvas, 3, y, 6, y, white)
graphics.DrawLine(offscreen_canvas, 7, y, 9, y, shadowWhite)
graphics.DrawLine(offscreen_canvas, 10, y, 15, y, youtubeRed)
y = 7
graphics.DrawLine(offscreen_canvas, 0, y, 2, y, youtubeRed)
graphics.DrawLine(offscreen_canvas, 3, y, 8, y, white)
graphics.DrawLine(offscreen_canvas, 9, y, 11, y, shadowWhite)
graphics.DrawLine(offscreen_canvas, 12, y, 15, y, youtubeRed)
y = 8
graphics.DrawLine(offscreen_canvas, 0, y, 2, y, youtubeRed)
graphics.DrawLine(offscreen_canvas, 3, y, 12, y, white)
graphics.DrawLine(offscreen_canvas, 13, y, 15, y, youtubeRed)
y = 9
graphics.DrawLine(offscreen_canvas, 0, y, 2, y, youtubeRed)
graphics.DrawLine(offscreen_canvas, 3, y, 11, y, white)
graphics.DrawLine(offscreen_canvas, 12, y, 15, y, youtubeRed)
y = 10
graphics.DrawLine(offscreen_canvas, 0, y, 2, y, youtubeRed)
graphics.DrawLine(offscreen_canvas, 3, y, 9, y, white)
graphics.DrawLine(offscreen_canvas, 10, y, 15, y, youtubeRed)
y = 11
graphics.DrawLine(offscreen_canvas, 0, y, 2, y, youtubeRed)
graphics.DrawLine(offscreen_canvas, 3, y, 7, y, white)
graphics.DrawLine(offscreen_canvas, 8, y, 15, y, youtubeRed)
y = 12
graphics.DrawLine(offscreen_canvas, 0, y, 2, y, youtubeRed)
graphics.DrawLine(offscreen_canvas, 3, y, 5, y, white)
graphics.DrawLine(offscreen_canvas, 6, y, 15, y, youtubeRed)
y = 13
graphics.DrawLine(offscreen_canvas, 0, y, 15, y, youtubeRed)
y = 14
graphics.DrawLine(offscreen_canvas, 0, y, 15, y, youtubeRed)
y = 15
graphics.DrawLine(offscreen_canvas, 1, y, 14, y, youtubeRed)
while True:
self.matrix.brightness = max_brightness
if lowBrightness() :
self.matrix.brightness = 20
self.matrix.brightness = 20
offscreen_canvas.Clear()
count = getYoutubeSubscribers(self)
#count = '000999'
len = graphics.DrawText(offscreen_canvas, font, 17, 13, textColor, count)
DrawYouTubeLogo(self)
offscreen_canvas = self.matrix.SwapOnVSync(offscreen_canvas)
time.sleep(30)
# Main function
if __name__ == "__main__":
run_text = RunText()
if (not run_text.process()):
run_text.print_help()
@bdebon
Copy link
Author

bdebon commented May 10, 2019

The script above is the script I wrote to realise my YouTube Counter
https://www.youtube.com/watch?v=yFXkXsWhzqI

Download https://github.com/hzeller/rpi-rgb-led-matrix
And add that script in the bindings/python/samples folder.

Run the script with that command:
sudo python3.4 youtube-counter.py --led-rows=16 --led-cols=32 --led-chain=2 -t 000864 -b 70

And now it should work (of course you will have to replace my YouTube channel ID by yours! l.50)

Tell me if it's clear enough!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment