Skip to content

Commit fb46cb7

Browse files
committed
Get location and method from arguments and save them
1 parent 123981d commit fb46cb7

File tree

2 files changed

+52
-13
lines changed

2 files changed

+52
-13
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
*.log
33
*.pyc
44
adhan.log
5+
.settings

updateAzaanTimers.py

+51-13
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import time
55
import sys
66
from os.path import dirname, abspath, join as pathjoin
7+
import argparse
78

89
root_dir = dirname(abspath(__file__))
910
print("Running from {}".format(root_dir))
@@ -22,23 +23,42 @@
2223
strClearLogsCommand = 'truncate -s 0 {}/adhan.log 2>&1'.format(root_dir)
2324
strJobComment = 'rpiAdhanClockJob'
2425

25-
#Set latitude and longitude here
26-
#--------------------
27-
lat = 42.288788
28-
long = -71.551678
29-
30-
#Set calculation method, utcOffset and dst here
31-
#By default system timezone will be used
32-
#--------------------
33-
PT.setMethod('ISNA')
34-
utcOffset = -(time.timezone/3600)
35-
isDst = time.localtime().tm_isdst
36-
37-
3826
#HELPER FUNCTIONS
3927
#---------------------------------
4028
#---------------------------------
4129
#Function to add azaan time to cron
30+
def parseArgs():
31+
parser = argparse.ArgumentParser(description='Calculate prayer times and install cronjobs to play Adhan')
32+
parser.add_argument('--lat', type=float, dest='lat',
33+
help='Latitude of the location, for example 30.345621')
34+
parser.add_argument('--lng', type=float, dest='lng',
35+
help='Longitude of the location, for example 60.512126')
36+
parser.add_argument('--method', choices=['MWL', 'ISNA', 'Egypt', 'Makkah', 'Karachi', 'Tehran', 'Jafari'],
37+
dest='method',
38+
help='Method of calculation')
39+
return parser
40+
41+
def mergeArgs(args):
42+
file_path = pathjoin(root_dir, '.settings')
43+
# load values
44+
lat = lng = method = None
45+
try:
46+
with open(file_path, 'rt') as f:
47+
lat, lng, method = f.readlines()[0].split(',')
48+
except:
49+
print('No .settings file found')
50+
# merge args
51+
if args.lat:
52+
lat = args.lat
53+
if args.lng:
54+
lng = args.lng
55+
if args.method:
56+
method = args.method
57+
# save values
58+
with open(file_path, 'wt') as f:
59+
f.write('{},{},{}'.format(lat or '', lng or '', method or ''))
60+
return lat or None, lng or None, method or None
61+
4262
def addAzaanTime (strPrayerName, strPrayerTime, objCronTab, strCommand):
4363
job = objCronTab.new(command=strCommand,comment=strPrayerName)
4464
timeArr = strPrayerTime.split(':')
@@ -70,6 +90,24 @@ def addClearLogsCronJob (objCronTab, strCommand):
7090
#---------------------------------
7191
#HELPER FUNCTIONS END
7292

93+
#Parse arguments
94+
parser = parseArgs()
95+
args = parser.parse_args()
96+
#Merge args with saved values if any
97+
lat, lng, method = mergeArgs(args)
98+
print(lat, lng, method)
99+
#Complain if any value is missing
100+
if not lat or not lng or not method:
101+
parser.print_usage()
102+
sys.exit(1)
103+
104+
#Set calculation method, utcOffset and dst here
105+
#By default system timezone will be used
106+
#--------------------
107+
PT.setMethod(method)
108+
utcOffset = -(time.timezone/3600)
109+
isDst = time.localtime().tm_isdst
110+
73111
# Remove existing jobs created by this script
74112
system_cron.remove_all(comment=strJobComment)
75113

0 commit comments

Comments
 (0)