Skip to content

Instantly share code, notes, and snippets.

@aelindeman
Last active December 11, 2024 21:32
Show Gist options
  • Save aelindeman/f9a05058246c073bb09a92c2b6ced944 to your computer and use it in GitHub Desktop.
Save aelindeman/f9a05058246c073bb09a92c2b6ced944 to your computer and use it in GitHub Desktop.
updates the GNOME Night Theme Switcher extension's night mode times to your local sunrise/sunset in the absence of geoclue
#!/usr/bin/python3
# run `pip3 install --user astral` or `apt install python3-astral` first
# install to `~/.local/libexec/update-theme-switcher-sunrise-sunset.py`
import json
import os
import subprocess
from pathlib import Path
from astral import Observer, sun
s = sun.sun(Observer(os.environ.get("LAT"), os.environ.get("LON"), 10), tzinfo=os.environ.get("TZ"))
for e in ("sunrise", "sunset"):
v = s[e].hour + s[e].minute / 60.0
subprocess.run([
"gsettings",
"--schemadir",
"{}".format(
Path("~/.local/share/gnome-shell/extensions/[email protected]/schemas").expanduser()
),
"set",
"org.gnome.shell.extensions.nightthemeswitcher.time",
e,
"{}".format(v),
])
print("set", e, v)
# run `systemctl edit --user --force --full update-theme-switcher-sunrise-sunset.service` and paste this in
# LAT: your latitude (e.g. for New York, 40.75)
# LON: your longitude (e.g. for New York, -73.98)
# TZ: your timezone (in tzdata format, e.g. America/New_York or Asia/Tokyo)
[Unit]
Description=Update theme switcher sunrise and sunset times
After=graphical-session.target
[Service]
Type=oneshot
Environment=LAT=40.75
Environment=LON=-73.98
Environment=TZ=America/New_York
ExecStart=python3 %h/.local/libexec/update-theme-switcher-sunrise-sunset.py
[Install]
WantedBy=graphical-session.target
# run `systemctl edit --user --force --full update-theme-switcher-sunrise-sunset.timer` and paste this in
[Unit]
Description=Run update-theme-switcher-sunrise-sunset daily
[Timer]
AccuracySec=1h
OnCalendar=daily
OnClockChange=yes
OnTimezoneChange=yes
Persistent=yes
RandomizedDelaySec=1h
[Install]
WantedBy=timers.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment