-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweatherwidget.py
More file actions
122 lines (97 loc) · 4.3 KB
/
weatherwidget.py
File metadata and controls
122 lines (97 loc) · 4.3 KB
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
import tkinter as tk
import source_helper
import requests
import feedparser
import subprocess, sys
from win11toast import toast
import threading
import os
import datetime
global response, feed, warning_title, warning_summary, current_title, title_var, warning_title_var
current_version = "WeatherPeg Version 3.0"
def dldata(event=None):
print("[LOG] Getting new data...")
response = requests.get(source_helper.RSS_URL)
feed = feedparser.parse(response.content)
for entry in feed.entries:
if entry.category == "Current Conditions":
current_title = entry.title
for entry in feed.entries:
if entry.category == "Warnings and Watches":
if not entry.summary == "No watches or warnings in effect.":
warning_summary = entry.summary
if entry.summary == "No watches or warnings in effect.":
warning_summary = "No watches or warnings in effect."
warning_title = entry.title
if not "No watches" in warning_title:
threading.Thread(target=threading_warning_notif, daemon=True).start()
def threading_warning_notif(event=None):
threading.Thread(target=warning_notif, daemon=True).start()
def warning_notif(event=None):
toast("Weather Warning!", "WeatherPeg has detected a potential weather warning or watch!")
def logger():
filename = "txt/history.txt"
logged_time = (f"Current time is {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
if os.path.exists(filename):
print(f"[LOG] Found {filename}")
else:
print(f"[LOG] Could not find {filename}, but created it")
with open(filename, "a") as f:
f.write(f"{current_title}\n")
f.write(f"Current warning: {warning_title}\n")
f.write(f"Logged time: {logged_time}\n")
f.write("This was written with WeatherPeg Desktop Widget")
f.write("-" * 50 + "\n")
print(f"[LOG] Logged current weather to {filename}")
response = requests.get(source_helper.RSS_URL)
feed = feedparser.parse(response.content)
for entry in feed.entries:
if entry.category == "Current Conditions":
current_title = entry.title
for entry in feed.entries:
if entry.category == "Warnings and Watches":
if not entry.summary == "No watches or warnings in effect.":
warning_summary = entry.summary
if entry.summary == "No watches or warnings in effect.":
warning_summary = "No watches or warnings in effect."
warning_title = entry.title
logger()
def refresh_weather(event=None):
dldata()
title_var.set(current_title)
warning_title_var.set(warning_title)
def open_large_program(event=None):
print("Opening main program...")
subprocess.Popen(["python", "weather-cmd.py"])
sys.exit()
print("Welcome to the WeatherPeg Desktop Widget")
root = tk.Tk()
root.title("WeatherPeg Widget")
root.configure(bg="black")
root.geometry("")
root.bind("<F5>", lambda event: refresh_weather())
root.bind("<F8>", open_large_program)
# root.bind("<F9>", threading_warning_notif)
warning_title_var = tk.StringVar(value=warning_title)
warning_summary_var = tk.StringVar(value=warning_summary)
words_to_remove = ["Current", "Conditions:"]
current_title = " ".join(word for word in current_title.split() if word not in words_to_remove)
print(current_title)
print(warning_title)
warning_title_label = tk.Label(root, textvariable=warning_title_var, fg="lime", bg="black",
font=("Courier", 11, "bold"), justify="left")
warning_title_label.pack()
# warning_summary_label = tk.Label(root, textvariable=warning_summary_var, fg="lime", bg="black",
# font=("Courier", 16, "bold"), justify="left")
# warning_summary_label.pack()
title_var = tk.StringVar(value=current_title)
title_label = tk.Label(root, textvariable=title_var, fg="lime", bg="black",
font=("Courier", 16, "bold"), justify="left")
title_label.pack()
version_label = tk.Label(
root, text=current_version,
fg="cyan", bg="black",
font=("Courier", 10), justify="left"
)
version_label.pack(side=tk.BOTTOM, pady=10, padx=10)
root.mainloop()