Created
December 8, 2021 07:57
-
-
Save WillemJan/6b57ccb52ae32562e643003fb2d6b212 to your computer and use it in GitHub Desktop.
This file contains 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/env python3 | |
import cv2 | |
import datetime | |
import ftplib | |
import ping3 | |
import pygame | |
import requests | |
import time | |
import os | |
import feedparser | |
from PIL import Image | |
from io import BytesIO | |
from lxml import html | |
hosts = [ | |
"audio", | |
"desk", | |
"gang", | |
"garden", | |
"garden1", | |
"gordijn", | |
"kast0", | |
"kast1", | |
"neopixel", | |
"schilderij", | |
"shelf", | |
"zapper", | |
] | |
commons = 'https://commons.wikimedia.org/w/api.php?action=featuredfeed&feed=potd&feedformat=rss&language=nl' | |
def fetch_commons(): | |
data = feedparser.parse(commons) | |
curr = '<html>' + data.get('entries')[-1].get('summary') + '</html>' | |
data = html.document_fromstring(curr) | |
for item in data.iter(): | |
if item.tag == 'img': | |
url = item.attrib.get('src').replace('300px', '460px') | |
rsp = requests.get(url) | |
print(url, rsp, rsp.status_code) | |
pilimage = Image.open(BytesIO(rsp.content)).convert("RGBA") | |
img = pygame.image.fromstring(pilimage.tobytes(), pilimage.size, pilimage.mode) | |
return img | |
def fetch_knmi(): | |
try: | |
ftp = ftplib.FTP('ftp.knmi.nl') | |
ftp.login() | |
ftp.cwd('/pub_weerberichten/') | |
files_avail = ftp.nlst() | |
except: | |
files_avail = [] | |
if files_avail: | |
for f in files_avail: | |
if f == 'MET10_RGB-HRV-RGB_8bit-wwwloop_920x591.gif': | |
fh = open('/mnt/ramdisk/sat.gif', 'wb') | |
ftp.retrbinary('RETR %s' % f, fh.write) | |
fh.close() | |
ftp.quit() | |
def cv2ImageToSurface(cv2Image): | |
size = cv2Image.shape[1::-1] | |
format = 'RGBA' if cv2Image.shape[2] == 4 else 'RGB' | |
cv2Image[:, :, [0, 2]] = cv2Image[:, :, [2, 0]] | |
surface = pygame.image.frombuffer(cv2Image.flatten(), size, format) | |
return surface.convert_alpha() if format == 'RGBA' else surface.convert() | |
def loadGIF(filename): | |
gif = cv2.VideoCapture(filename) | |
frames = [] | |
while True: | |
ret, cv2Image = gif.read() | |
if not ret: | |
break | |
pygameImage = cv2ImageToSurface(cv2Image) | |
for i in range(5): | |
frames.append(pygameImage) | |
return frames | |
def render_time(time_str): | |
font = pygame.font.SysFont('Verdana', 60) | |
img = font.render(time_str, True, (155,155,155)) | |
return(img) | |
def render_host_status(screen): | |
font = pygame.font.SysFont('arial', 40) | |
for i, hostname in enumerate(hosts): | |
img = font.render(hostname, True, (144, 199, 144)) | |
screen.blit(img, (100, 1000 + i * 40)) | |
if ping3.ping(hostname, timeout=0.01) is not None: | |
stat_img = font.render("up", True, (0, 200, 0)) | |
screen.blit(img, (100, 1000 + i * 40)) | |
screen.blit(stat_img, (300, 1000 + i * 40)) | |
else: | |
stat_img = font.render("down", True, (200, 0, 0)) | |
screen.blit(img, (100, 1000 + i * 40)) | |
screen.blit(stat_img, (300, 1000 + i * 40)) | |
def get_load_sat(): | |
try: | |
os.remove("/mnt/ramdisk/sat.gif") | |
except: | |
pass | |
fetch_knmi() | |
gifFrameList = loadGIF("/mnt/ramdisk/sat.gif") | |
return gifFrameList | |
pygame.init() | |
window = pygame.display.set_mode((1016,1856)) | |
clock = pygame.time.Clock() | |
gif_FrameList = get_load_sat() | |
time_str = datetime.datetime.now().strftime("%Y-%m-%d %H:%M") | |
try: | |
rsp = requests.get('http://www.scheveningenlive.nl/sport.jpg') | |
pilimage = Image.open(BytesIO(rsp.content)).convert("RGBA") | |
img = pygame.image.fromstring(pilimage.tobytes(), pilimage.size, pilimage.mode) | |
img = pygame.transform.scale(img, (440, 295)) | |
except: | |
pass | |
now = render_time(time_str) | |
gif_currentFrame = 0 | |
gif_direction_up = True | |
run = True # Use this to teminate main loop. | |
comm = fetch_commons() | |
while run: | |
clock.tick(60) | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
run = False | |
window.fill(0) | |
try: | |
rect = gif_FrameList[gif_currentFrame].get_rect(center = (460, 295)) | |
window.blit(gif_FrameList[gif_currentFrame], (48,100) ) | |
window.blit(img, (48,700)) | |
window.blit(comm, (640,700)) | |
except: | |
pass | |
window.blit(now, (500,0)) | |
render_host_status(window) | |
if gif_direction_up: | |
if gif_currentFrame + 1 < len(gif_FrameList): | |
gif_currentFrame += 1 | |
else: | |
gif_direction_up = False | |
else: | |
if gif_currentFrame -1 > 0: | |
gif_currentFrame -= 1 | |
else: | |
gif_direction_up = True | |
cur_min = int(datetime.datetime.now().strftime("%M")) | |
# Update every 30 min. | |
if cur_min % 30 == 0: | |
gif_FrameList = get_load_sat() | |
if cur_hour == 6: | |
comm = fetch_commons() | |
if not time_str == datetime.datetime.now().strftime("%Y-%m-%d %H:%M"): | |
time_str = datetime.datetime.now().strftime("%Y-%m-%d %H:%M") | |
now = render_time(time_str) | |
try: | |
response = requests.get('http://www.scheveningenlive.nl/sport.jpg') | |
pilimage = Image.open(BytesIO(response.content)).convert("RGBA") | |
img = pygame.image.fromstring(pilimage.tobytes(), pilimage.size, pilimage.mode) | |
img = pygame.transform.scale(img, (460, 295)) | |
except: | |
pass | |
pygame.display.flip() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment