Mirror - where you can see the clock and the weather information
Thought is like most "miracles mirror" found on the web. I will not lie, thoughts are not mine. But i thought the thing could be used as:
- is a smart home management interface
- home sensor control interface
- home protection
- reception assistant (Hotel, Motel ...)
- promotion
- circular, and many more....
The difference is only in the Python interpreter 2.7 differs from 3., in the code tag where to change. Also there is a difference between the editor i use Pycharm with windows, Komodo Edit (I guess I get a free version) with Linux and Geany with Raspberry. I liked Komodo Edit the most. The truth is trouble, especially with libraries, but .... after all, it's just better to learn everything.
let's get started, I will post my comments as per the # sign, you will know what to delete
#!/usr/bin/env python
# ----------------------library for code --------------------------------
import sys
if sys.version_info[0] == 2:
import Tkinter as tk #Linux
else:
import tkinter as tk #Windose
#--------------------------------------------------------------------------
#If you used Python 3, you must delete or comment reload(sys)
#and sys.setdefaultencoding('utf8') in source
#--------------------------------------------------------------------------
reload(sys)
sys.setdefaultencoding('utf8')
import time
import datetime
#---------------------------------------------------------------------------
#BeautifulSoup, Requests, feedparser - powerful libraries
#that help to extract data from a web page. Technology called
#Web Scraping
#--------------------------------------------------------------------------
from bs4 import BeautifulSoup
import requests
import feedparser
from itertools import cycle
from PIL import Image, ImageTk
import csv
from tkinter import *
#-------------------------------------------------------------------------
#Internet addresses where data is taken for rendering
#as well as the cycle "if" ..., which changes web addresses
# if one no longer works
#-----------------------URL link----------------------------------------
page_link = ('https://____________________________________')
url = ('__________________________________________________')
#newss_link use for RSS chanel
#
#
news_link=('https://__________________________________')
news_link2= ('https://________________________________')
d = feedparser.parse(news_link)
if d == StopIteration:
pass
else:
d = feedparser.parse(news_link2)
post_list = cycle(d.entries)
#---------------------- ICON Folder---------------------------------
image_dir = '/root/Documents/MIRROR/weather-icons/'
#-----------------------------------------------------------------------
#-------------------------- Global variables -----------------------
b ='black',
w = 'white'
fontes ='lato'
time1 = ''
date_time1 = ''
WD1=''
img0=''
img3=''
row1=''
O_SD =''
#----------------------------------- GUI -----------------------------
window = Tk()
window.configure(background = 'black')
window.bind('<Escape>', lambda e: window.destroy())
window.attributes ('-fullscreen', True)
#-------------------------------------------------------------------------
#as the text is white, the screen will be black.
#The image will be across the screen. And the "secret" button
#<Escape> that the program would be closed
#-------------------------------------------------------------------------
#------------------ INTERFACE --------------------------------
frame1 = Frame(window)
frame1.grid(row = 0, column = 0)
screen_date=Frame(frame1, bg = b)
screen_date.grid(row =0)
data = Label(screen_date, fg = w, bg=b, font = (fontes,72))
data.grid(row=0)
td_label=Label(screen_date,fg=w, bg = b, font=(fontes,54), anchor = W)
td_label.grid(row = 1)
frame_description=Frame(window,bg=b)
frame_description.grid(row =1)
hl_label= Label(frame_description,fg=w, bg =b, font=(fontes, 26, 'bold'))
hl_label.grid()
######################## LABELS ##################################
frame_messages = Frame(frame_description, bg=b)
frame_messages.grid(row=1, column = 0)
wm_label=Label(frame_messages,fg='yellow', bg = b, font=(fontes,22),anchor = W)
wm_label.grid(row =1, column=0, sticky = W)
png2_label = Label(frame_messages,bg = b,)
png2_label.grid(row= 1, column = 1)
#------------------CLOCK------------------------------------------------
frame_tiktak = Frame(window, bg=b)
frame_tiktak.grid(row=2, column = 0, sticky = W)
frame_clock= Frame (frame_tiktak)
frame_clock.grid(row = 0, column=0)
clock = Label(frame_clock,fg = w, bg = b, font = (fontes, 200,'bold'),anchor = W)
clock.grid()
weat_frame=Frame(frame_tiktak, bg = b)
weat_frame.grid(row=0,column=1, sticky = W)
weat_label= Label(weat_frame,fg=w, bg = b, font=(fontes, 54))
weat_label.grid(row=0,column=0, sticky = W)
png_label = Label(weat_frame, bg= b)
png_label.grid(row=0, column=1, sticky =W)
frame_room = Frame(window, bg=b)
frame_room.grid(row=3, column = 0, sticky = W)
in_room = Frame (frame_room)
in_room.grid(row=0, column = 0)
sd_label = Label(frame_room,fg = w, bg = b, font = (fontes, 26, 'bold'), anchor = W)
sd_label.grid(row = 0, column = 0)
########################################################################
frame_wth = Frame (window, bg = b)
frame_wth.grid(row =4 , column=0, sticky =W)
wth_table_name= Label(frame_wth,fg= 'light blue', bg = b, font=(fontes, 20, 'bold'))
wth_table_name.grid()
WTH_table_name= '{:10s} {:16s} {:16s} {:20s} {:16s} {:10s}'.format(" Day"," Description", " Hi/Low",
" Chance of pricipitation"," Wind "," Humidity")
wth_table_name.config(text=WTH_table_name)
########################################################################
frame_5=Frame(window,bg = b)
frame_5.grid(row=5, column = 0, sticky = W)
table_frame = Frame(frame_5,bg = b)
table_frame.grid(row = 0, column = 3)
frame = Frame(table_frame, bg= b )
frame.grid()
#---------------------- RSS LABEL --------------------------------
frame_rss = Frame(window, bg=b)
frame_rss.place(x=10,y=1020) # don't tuoch :)
label_rss = Label(frame_rss, text=time, fg = w, bg = b, font = (fontes, 22))
label_rss.pack()
#-----------------------------Functions---------------------------------
############################# CLOCK ####################################
def tik_tak():
global time1 # get the current local time from the PC
time2 = time.strftime('%H:%M:%S') # if time changed, update it
if time2 != time1:
time1 = time2
clock.config(text=time2)
clock.after(1000, tik_tak)
############################## RSS NEWS ###############################
def rssnews():
post = next(post_list)
RSSNEWS = post.title
modTXT = 'News: ' + RSSNEWS
label_rss.config(text=modTXT)
window.after(8000, rssnews)
#print (post)
############################## DATE ####################################
def month_month():
global date_time1
date_time2 = time.strftime(' %Y %B %d, %A')
if date_time2 != date_time1:
date_time1 = date_time2
data.config(text=date_time2)
frame1.after(1800000,month_month)
############################# WEATHER #####################################
def weather ():
global WD1
global img0
global img3
try:
p_resp = requests.get(page_link, timeout=10)
if p_resp.status_code ==200:
p_cont = BeautifulSoup(p_resp.content, 'html.parser')
else:
lbl_ERROR = Label(window, text=page_response.status_code,fg = 'white', font=(fontes, 40))
lbl_ERROR.grid(row =0, column = 0)
time.sleep(10)
lbl_ERROR.destroy()
pass
except requests.Timeout as e:
lbl_ERROR = Label(window, text="Timeout page:"+page_link, fg = 'white', font=(fontes, 12))
lbl_ERROR.grid(row =0, column = 0)
time.sleep (5)
if p_resp is None:
pass
except AttributeError:
pass
temp=p_cont.find('div',attrs={"class":"today_nowcard-temp"}).text
today_name=p_cont.find('div',attrs={"class":"today_nowcard-phrase"}).text
if today_name == 'Cloudy':
img = PhotoImage(file = image_dir+'cloudy.png')
if img != img0:
img0 = img
png_label.config(image = img)
if today_name == 'Partly Cloudy':
img = PhotoImage(file = image_dir+'cloudy.png')
if img != img0:
img0 = img
png_label.config(image = img)
if today_name == 'Mist':
img = PhotoImage(file = image_dir+'mist.png')
if img != img0:
img0 = img
png_label.config(image = img)
if today_name == 'Mostly Cloudy':
img = PhotoImage(file = image_dir+'mcloudy.png')
if img != img0:
img0 = img
png_label.config(image = img)
if today_name == 'Light Snow':
img = PhotoImage(file = image_dir+'snow.png')
if img != img0:
img0 = img
png_label.config(image = img)
if today_name == 'Light Snow Shower':
today_name = 'Light Snow'
img = PhotoImage(file = image_dir+'snow.png')
if img != img0:
img0 = img
png_label.config(image = img)
if today_name == 'Light Rain':
img = PhotoImage(file = image_dir+'rain.png')
if img != img0:
img0 = img
png_label.config(image = img)
if today_name == 'Light Drizzle':
img = PhotoImage(file = image_dir+'rain.png')
if img != img0:
img0 = img
png_label.config(image = img)
if today_name == 'Snow':
img = PhotoImage(file = image_dir+'snow.png')
if img != img0:
img0 = img
png_label.config(image = img)
if today_name == 'Cloudy/Wind':
img = PhotoImage(file = image_dir+'wind.png')
if img != img0:
img0 = img
png_label.config(image = img)
if today_name == 'Wintry Mix':
img = PhotoImage(file = image_dir+'wintry.png')
if img != img0:
img0 = img
png_label.config(image = img)
if today_name == 'Fair':
img = PhotoImage(file = image_dir+'suncloud.png')
if img != img0:
img0 = img
png_label.config(image = img)
if today_name == 'Showers in the Vicinity':
today_name = 'Vicinity'
img = PhotoImage(file = image_dir+'suncloud.png')
if img != img0:
img0 = img
png_label.config(image = img)
if today_name == 'Light Rain Shower/Wind':
today_name = 'Shower / Wind'
img = PhotoImage(file = image_dir+'mist.png')
if img != img0:
img0 = img
png_label.config(image = img)
if today_name == 'Rain Shower':
img = PhotoImage(file = image_dir+'mist.png')
if img != img0:
img0 = img
png_label.config(image = img)
if today_name == 'Light Rain Shower':
today_name = 'Light shower'
img = PhotoImage(file = image_dir+'mist.png')
if img != img0:
img0 = img
png_label.config(image = img)
if today_name == 'Mostly Cloudy/Wind':
today_name = 'Cloudy/Wind'
img = PhotoImage(file = image_dir+'wind.png')
if img != img0:
img0 = img
png_label.config(image = img)
if today_name == 'Snow Shower':
img = PhotoImage(file = image_dir+'wintry.png')
if img != img0:
img0 = img
png_label.config(image = img)
feels_temp=p_cont.find('span',attrs={"classname":"deg-feels"}).text
today_H_L=p_cont.find('span',attrs={"class":"today-wx-descrip"}).text
sun_up =p_cont.find ('span',attrs={"class":"wx-dsxdate", "id":"dp0-details-sunrise"}).text
sun_down =p_cont.find ('span',attrs={"class":"wx-dsxdate", "id":"dp0-details-sunset"}).text
today_descript=p_cont.find ('span',attrs={"class":"today-wx-descrip"}).text
try:#-if WARN MESSAGES == 0 disable error messages ---------------------------------------
warn_message = p_cont.find('span',attrs={"classname":"warning-text"}).text
if warn_message == 'Potential disruption due to snow and ice':
img2 = PhotoImage(file = image_dir+'slide.png')
if img2 != img3:
img3 = img2
png2_label.config(image = img2)
WM=(warn_message)
wm_label.config(text=WM)
if warn_message == 'Potential disruption due to wind':
WM=(warn_message)
wm_label.config(text=WM)
if warn_message is None:
pass
except AttributeError:
pass
WD=(" Now: "+temp+","+"\n"+today_name+"\n"+"Feels like: "+feels_temp)
if WD != WD1:
WD1 = WD
weat_label.config(text=WD)
HL=(today_H_L)
hl_label.config(text=HL)
TD=(" Sunrise - "+sun_up+", "+"Sunset - "+sun_down)
td_label.config(text=TD)
weat_frame.after(120000,weather)
#--------------------------------------------------------------------------------
# change GUI labels data every 20 minutes
#-------------------------------------------------------------------------------
########################################################################
def deleteContent():
with open('weather_data.csv', "w"):
pass
######################## weather forecast 5 days #######################
def weather_5():
global row1
deleteContent()
try:
page_table = requests.get(url)
if page_table.status_code == 200:
p_content = BeautifulSoup(page_table.content, 'html.parser')
else:
lbl_ERROR = Label(root_frame, text="Uups..error load page" + page_table, fg='white', bg=b,
font=(fontes,22)).grid()
time.sleep(10)
lbl_ERROR.destroy()
pass
except requests.TimeoutError:
lbl_ERROR2 = Label(root_frame, text="Uups....time out", fg='white', bg=b, font=(fontes,22)).grid()
time.sleep(10)
lbl_ERROR2.destroy()
pass
table = p_content.find('table')
thead = table.find('thead')
tbody = table.find('tbody')
for i in tbody.findAll('tr'):
cell = i.findAll('td')
day = cell[1].find(text=True)
desc = cell[2].text.strip()
hl = cell[3].text.strip()
precip = cell[4].text.strip()
wind = cell[5].text.strip()
hum = cell[6].text.strip()
data = open('weather_data.csv', 'a')
format_lines = (day + "," + desc + "," + hl + "," + precip + "," + wind + "," + hum + "\n")
data.write(format_lines)
data.close()
for widget in frame.winfo_children():
widget.destroy()
with open("weather_data.csv") as file:
reader = csv.reader(file)
r = 0
for col in reader:
c = 0
for row in col:
if row != row1:
row1 = row
lbl = Label(frame, fg='white', bg='black', text=row, height=1,width = 16, borderwidth=1, font=(fontes, 20))
lbl.grid(row=r, column=c)
c += 1
r += 1
table_frame.after(900000, weather_5)
########################################################################
########################################################################
tik_tak()
month_month()
rssnews()
weather()
weather_5()
window.mainloop()