#!/bin/python3 # Title: SunMoon.py # Author: TheOuterLinux (https://theouterlinux.gitlab.io) # Purpose: I got tired of how hard it was to find a Python script that # could just display sunrise/set and moonrise/set times and # all you need, more or less, is a latitude and longitude of # your location. This script also automatically uses your # system's timezone information to display times. # License: MIT # Last updated: 2023/01/20 # Requirements: # - A system with Python 3.7 or newer installed # - 'astral' Python module (sudo pip install astral) from astral import LocationInfo NAME = 'London' REGION = 'England' TIMEZONE_NAME = 'Europe/London' LATITUDE = 51.5 LONGITUDE = -0.116 city = LocationInfo(NAME, REGION, TIMEZONE_NAME, LATITUDE, LONGITUDE) import datetime from astral import moon from astral.moon import moonrise, moonset from astral.sun import sunrise, sunset from astral.location import Location local_timezone = datetime.datetime.now(datetime.timezone.utc).astimezone().tzinfo currentDateTime = datetime.datetime.now(local_timezone) date = currentDateTime.date() YEAR = date.strftime("%Y") MONTH = date.strftime("%m") DAY = date.strftime("%d") dt = datetime.date(int(YEAR), int(MONTH), int(DAY)) sunrise = sunrise(city.observer, dt, city.tzinfo) print('Sun Rise: ', sunrise) sunset = sunset(city.observer, dt, city.tzinfo) print('Sun Set: ', sunset) #rise = moonrise(city.observer, dt) # returns a UTC time moonrise = moonrise(city.observer, dt, city.tzinfo) print('Moon Rise: ', moonrise) moonset = moonset(city.observer, dt, city.tzinfo) print('Moon Set: ', moonset) moonphase = moon.phase(dt) if moonphase > 0 and moonphase < 6.99 : moonphase = 'New Moon' elif moonphase > 6.99 and moonphase < 13.99 : moonphase = 'First Quarter' elif moonphase > 13.99 and moonphase < 20.99 : moonphase = 'Full Moon' elif moonphase > 20.99 and moonphase < 27.99 : moonphase = 'Last Quarter' print('Moon Phase: ', moonphase)