from datetime import datetime
import pathlib
import pytz
from suntime import Sun

# Locate sky-cam.conf next to this script (works regardless of cwd).
_here = pathlib.Path(__file__).resolve().parent

def _read_conf(path):
    conf = {}
    with open(path) as f:
        for line in f:
            line = line.strip()
            if line and not line.startswith('#') and '=' in line:
                k, v = line.split('=', 1)
                conf[k.strip()] = v.strip()
    return conf

conf = _read_conf(_here / 'sky-cam.conf')

for _key in ('LATITUDE', 'LONGITUDE', 'TIMEZONE'):
    if _key not in conf:
        raise SystemExit(f"sunrise.py: {_key} not set in sky-cam.conf")

latitude  = float(conf['LATITUDE'])
longitude = float(conf['LONGITUDE'])
tz        = pytz.timezone(conf['TIMEZONE'])

sun = Sun(latitude, longitude)
now = datetime.now(tz)
sunrise_utc = sun.get_sunrise_time(now)
sunrise_time = sunrise_utc.astimezone(tz)

print(sunrise_time.strftime('%Y-%m-%d %H:%M:%S'))
