Skip to content

Instantly share code, notes, and snippets.

@mtayseer
Created June 9, 2019 12:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mtayseer/31a4cf5d0a69de095f4e3fb09ee5ed33 to your computer and use it in GitHub Desktop.
Save mtayseer/31a4cf5d0a69de095f4e3fb09ee5ed33 to your computer and use it in GitHub Desktop.
A cron job to cast islamic azan sound to a Google home device
import requests, googlehomepush, time, datetime, json, os, codecs, logging
custom_alarms = {'07:50': 'You have to leave now',
'07:30': 'You have 20 minutes'}
# We don't want to read prayer times from the web all the time, so we cache it
# for the day
current_dir = os.path.abspath(os.path.dirname(__file__))
today = datetime.datetime.today()
today_file = os.path.join(current_dir, today.strftime('%d-%m-%Y.prayer_times'))
logging.basicConfig(level=logging.INFO)
handler = logging.FileHandler(os.path.join(current_dir, today.strftime('%d-%m-%Y.log')))
handler.setLevel(logging.INFO)
logger = logging.getLogger(__name__)
logger.addHandler(handler)
logger.info('Today file: %s', today_file)
# Delete old prayer times cache files
for f in os.listdir(current_dir):
if f.endswith('.prayer_times') and os.path.abspath(f) != today_file:
logging.info('Deleting: %s', f)
os.remove(f)
if os.path.exists(today_file):
logger.info('Prayer times cache file exists. Reading from it.')
prayer_times = json.load(codecs.open(today_file, encoding='utf-8'))
else:
logger.info('Reading prayer times from the server')
url = today.strftime(
'http://api.aladhan.com/v1/timings/'
'%d-%m-%Y'
'?latitude=47.111'
'&longitude=8.111'
'&method=2')
prayer_times = requests.get(url).json()
json.dump(prayer_times, codecs.open(today_file, 'wb', encoding='utf-8'))
# Remove non-azan timing
for p in ['Sunset', 'Midnight', 'Imsak', 'Sunrise']:
del prayer_times['data']['timings'][p]
# If now is a prayer time, then cast azan sound
now = datetime.datetime.now().strftime('%H:%M')
gh = googlehomepush.GoogleHome('Google home speaker')
if now in prayer_times['data']['timings'].values():
logger.info('Casting azan at: %s', now)
gh.cc.set_volume(.35)
gh.play(
'https://ia800303.us.archive.org/5/items/Naseer.Al.Qtami.Azan/'
'Naseer.Al.Qtami.Azan.mp3',
'audio/mp3')
gh.cc.set_volume(.5)
elif now in custom_alarms:
gh.say(custom_alarms[now])
else:
logger.info('%s is not azan time', now)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment