-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
executable file
·186 lines (162 loc) · 6.93 KB
/
main.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import datetime
from flask import Flask, render_template, request, url_for
import webview
import os
import pickle
import argparse
import multiprocessing
import subprocess
"""
The program first reads for the required info stored in the ./data/data.dat binary file.
"""
app = Flask(__name__)
parser = argparse.ArgumentParser()
parser.add_argument("--type", type=str, required=False)
args = parser.parse_args()
with open(r"/usr/share/linuxDynamicWallpapers/data/data.dat", "rb") as fr:
data = pickle.load(fr)
@app.context_processor
def override_url_for():
return dict(url_for=dated_url_for)
def dated_url_for(endpoint, **values):
if endpoint == "static":
filename = values.get("filename", None)
if filename:
file_path = os.path.join(app.root_path, endpoint, filename)
values["q"] = int(os.stat(file_path).st_mtime)
return url_for(endpoint, **values)
def setDEWallpaper(desktop_environment: str, style: str):
if style in [
"bitday",
"firewatch",
"gradient",
]: # only these 3 types have .png file extension.
file_extension = ".png"
else:
file_extension = ".jpg"
if desktop_environment.lower() in [
"/usr/share/xsessions/plasma",
"plasmawayland",
"neon",
"plasma",
"kde",
]: # Set Wallpaper for Plasma DE
print("Inside", desktop_environment)
os.system(
'qdbus org.kde.plasmashell /PlasmaShell org.kde.PlasmaShell.evaluateScript \'var allDesktops = desktops();print (allDesktops);for (i=0;i<allDesktops.length;i++) {d = allDesktops[i];d.wallpaperPlugin = "org.kde.image";d.currentConfigGroup = Array("Wallpaper", "org.kde.image", "General");d.writeConfig("Image", "file:///usr/share/linuxDynamicWallpapers/images/'
+ style
+ "/"
+ str(datetime.datetime.now().hour)
+ file_extension
+ "\")}'"
)
elif desktop_environment.lower() == "cinnamon": # Set Wallpaper for Cinnamon DE
print("Inside", desktop_environment)
os.system(
f'gsettings set org.cinnamon.desktop.background picture-uri "file:///usr/share/linuxDynamicWallpapers/images/{style}/{datetime.datetime.now().hour}'
+ f'{file_extension}"'
)
elif desktop_environment.lower() in [
"xfce session",
"xfce",
"xubuntu",
]: # Set Wallpaper for XFCE DE
print("Inside", desktop_environment)
SCREEN = subprocess.getoutput(
"echo $(xrandr --listactivemonitors | awk -F ' ' 'END {print $1}' | tr -d \:)"
)
MONITOR = subprocess.getoutput(
"echo $(xrandr --listactivemonitors | awk -F ' ' 'END {print $2}' | tr -d \*+)"
)
os.system(
f"xfconf-query --channel xfce4-desktop --property /backdrop/screen{SCREEN}/monitor{MONITOR}/workspace0/last-image --set usr/share/linuxDynamicWallpapers/images/{style}/{datetime.datetime.now().hour}"
+ f"{file_extension}"
)
elif desktop_environment.lower() == "mate": # Set Wallpaper for Mate DE
print("Inside", desktop_environment)
os.system(
f"gsettings set org.mate.background picture-filename usr/share/linuxDynamicWallpapers/images/{style}/{datetime.datetime.now().hour}"
+ f"{file_extension}"
)
elif desktop_environment.lower() == "lxde": # Set Wallpaper for LXDE
print("Inside", desktop_environment)
os.system(
f'pcmanfm --set-wallpaper="usr/share/linuxDynamicWallpapers/images/{style}/{datetime.datetime.now().hour}'
+ f'{file_extension}"'
)
elif desktop_environment.lower() in [
"pantheon",
"gnome",
"gnome-xorg",
"ubuntu",
"deepin",
]: # Set Wallpaper for Ubuntu, Pop, Pantheon DE
print("Inside", desktop_environment)
os.system(
f"gsettings set org.gnome.desktop.background picture-uri file:///usr/share/linuxDynamicWallpapers/images/{style}/{datetime.datetime.now().hour}"
+ f"{file_extension}"
)
elif desktop_environment.lower() == "pop":
print("Inside", desktop_environment)
if (
subprocess.getoutput("gsettings get org.gnome.desktop.interface gtk-theme")
== "'Pop-dark'"
):
os.system(
f"gsettings set org.gnome.desktop.background picture-uri-dark file:///usr/share/linuxDynamicWallpapers/images/{style}/{datetime.datetime.now().hour}"
+ f"{file_extension}"
)
else:
os.system(
f"gsettings set org.gnome.desktop.background picture-uri file:///usr/share/linuxDynamicWallpapers/images/{style}/{datetime.datetime.now().hour}"
+ f"{file_extension}"
)
else:
print("Inside", desktop_environment)
os.system(
f"feh --bg-fill usr/share/linuxDynamicWallpapers/images/{style}/{datetime.datetime.now().hour}"
+ f"{file_extension}"
)
@app.route("/")
def index():
return render_template("index.html") # Display the Flask Frontend.
@app.route("/setWallpaper")
def setWallpaper():
wallpaper = request.args.get("wallpaper").lower()
DE = subprocess.getoutput("echo $DESKTOP_SESSION")
previousWallpaper = data["currentWallpaper"]
print(previousWallpaper)
setDEWallpaper(DE, wallpaper)
data["DE"] = DE
data["currentWallpaper"] = wallpaper
with open("/usr/share/linuxDynamicWallpapers/data/data.dat", "wb") as fw:
pickle.dump(data, fw)
os.system(
f'(crontab -u {subprocess.getoutput("whoami")} -l ; echo "0 * * * * env PATH={subprocess.getoutput("echo $PATH")} DISPLAY={subprocess.getoutput("echo $DISPLAY")} DESKTOP_SESSION={subprocess.getoutput("echo $DESKTOP_SESSION")} DBUS_SESSION_BUS_ADDRESS="{subprocess.getoutput("echo $DBUS_SESSION_BUS_ADDRESS")}" setdwl {wallpaper}") | crontab -u {subprocess.getoutput("whoami")} -'
) # Sets a cronjob for the wallpaper to change every hour.
os.system(
f'crontab -u {subprocess.getoutput("whoami")} -l | grep -v "{previousWallpaper}" | crontab -u {subprocess.getoutput("whoami")} -'
)
os.system("/etc/init.d/cron reload")
os.system(
f'notify-send "Linux Dynamic Wallpapers" "Set wallpaper to {wallpaper.upper()}" '
)
def runServer():
app.run(port=6969)
def onclose():
p1.kill()
if __name__ == "__main__":
if args.type is None: # For setting the Wallpaper using GUI
p1 = multiprocessing.Process(target=runServer)
p1.start()
window = webview.create_window(
"Linux Dynamic Wallpapers", "http://localhost:6969"
)
window.closing += onclose
webview.start(http_server=True)
else:
with open(
"/usr/share/linuxDynamicWallpapers/data/data.dat", "rb"
) as f: # For changing the wallpaper every hour, managed by setdwl.sh
data = pickle.load(f)
setDEWallpaper(data["DE"], args.type)