-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathyoutube-subscriber.py
125 lines (100 loc) · 3.15 KB
/
youtube-subscriber.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python
"""
Raspberry Pi Youtube Counter for Elektor TV Channel
elektor.tv
Helped me:
http://stackoverflow.com/questions/36252027/how-to-use-youtube-api-v3-for-realtime-subscribers
https://max7219.readthedocs.io/en/latest/
http://raspi.tv/2013/8-x-8-led-array-driven-by-max7219-on-the-raspberry-pi-via-python
http://www.raspberrypi-spy.co.uk/2012/06/simple-guide-to-the-rpi-gpio-header-and-pins/
http://raspberrypi.stackexchange.com/questions/8734/execute-script-on-start-up
"""
#import RPi.GPIO as GPIO
import math
import os
import sys
import httplib2
import json
from urllib.request import urlopen
import time
from datetime import datetime
import random
import socket
from select import select
# Needed to get MAX7219 support
from luma.led_matrix.device import max7219
from luma.core.interface.serial import spi, noop
from luma.core.virtual import viewport, sevensegment
# Channel ID and API key, get them from your YouTube channel.
channel_id = "my channel id"
api_key = "my API key"
lookup_url = "https://www.googleapis.com/youtube/v3/channels?part=statistics&id=" + channel_id + "&key=" + api_key
#Change brightness here
#brightness = 1
def is_connected():
try:
host = socket.gethostbyname("www.google.com")
s = socket.create_connection((host, 80), 2)
return True
except:
pass
return False
def date(display):
now = datetime.now()
display.text = now.strftime("%d-%m-%y")
def clock(display,seconds):
interval = 0.5
for i in range(int(seconds/interval)):
now = datetime.now()
if i%2==0:
display.text = now.strftime("%H_%M_%S")
else:
display.text = now.strftime("%H %M %S")
time.sleep(interval)
def show_message_vp(device, msg, delay=0.1):
# Implemented with virtual viewport
width = device.width
padding = " " * width
msg = padding + msg + padding
n = len(msg)
virtual = viewport(device, width=n, height=8)
sevensegment(virtual).text = msg
for i in reversed(list(range(n - width))):
virtual.set_position((i, 0))
time.sleep(delay)
def main():
# create seven segment device
serial = spi(port=0,device=0,gpio=noop())
device = max7219(serial,cascaded=1)
display = sevensegment(device)
#display = led.sevensegment(cascaded=1)
#display.brightness(brightness_setting)
#display.clear()
display.text = " ---- "
while 1:
show_message_vp(device,"ELEKtor TV SubScribErS",0.2)
date(display)
time.sleep(3)
try:
# Catches the webpage from google
soup = urlopen(lookup_url)
markup = soup.read()
# Access the part of the JSON object that we care about
feed_json = json.loads(markup)
sub_count = feed_json["items"][0]["statistics"]["subscriberCount"]
# Tells us how great we are (writes to display)
if len(sub_count)>8:
sub_count = "99999999"
padding = " " * (8-len(sub_count))
display.text = padding + sub_count
print(sub_count)
except:
# If can't get new number, screen goes blank
# display.clear()
display.text = "Error"
print("some exception happened")
#time.sleep(1)
time.sleep(5)
clock(display,seconds=10)
if __name__ == '__main__':
main()