From d1c5bf514be300931609b5c2213fc3aa630d18fd Mon Sep 17 00:00:00 2001 From: Michaƫl Ball Date: Wed, 13 Oct 2021 13:56:58 +0100 Subject: Initial implementation --- main.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 main.py diff --git a/main.py b/main.py new file mode 100644 index 0000000..4a014b9 --- /dev/null +++ b/main.py @@ -0,0 +1,47 @@ +import argparse +from configparser import ConfigParser +from datetime import datetime, timezone +import sys + +from suntime import Sun, SunTimeException +from xdg import xdg_config_home + + +def get_location(args): + config = ConfigParser() + config_location = xdg_config_home().joinpath("auto-dark-mode", "config") + config.read(config_location) + + if args.latitude and args.longitude: + return latitude, longitude + + try: + latitude = config.getfloat("DEFAULT", "latitude") + longitude = config.getfloat("DEFAULT", "longitude") + except KeyError: + sys.exit(f"Please supply latitude and longitude with the --latitude and --longtide flags, or set them in {config_location}") + + return latitude, longitude + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description="Check if we're in day (after sunrise but before sunset) or night (after sunset but before sunrise) time") + parser.add_argument("--day", action="store_true", default=False, help="return successfully if it's day") + parser.add_argument("--night", action="store_true", default=True, help="return successfully if it's night (default)") + parser.add_argument("--latitude", type=float, help="specify your latitude") + parser.add_argument("--longitude", type=float, help="specify your longitude") + + args = parser.parse_args() + + latitude, longitude = get_location(args) + sun = Sun(latitude, longitude) + + utc_now = datetime.now(timezone.utc) + + if utc_now > sun.get_sunrise_time() and utc_now < sun.get_sunset_time(): + if not args.day: + sys.exit(1) + + else: + if args.day: + sys.exit(1) -- cgit v1.2.3