Skip to content

Commit 3331c80

Browse files
authored
Merge pull request achaudhry#7 from hammady/master
Support configuration as script arguments
2 parents 6aeb1e0 + 1a56395 commit 3331c80

File tree

6 files changed

+109
-64
lines changed

6 files changed

+109
-64
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

README.md

+19-19
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,23 @@ This projects uses a python script which automatically calculates [adhan](https:
1212
1. Install git: Go to raspberry pi terminal (command line interface) and install `git`
1313
* `$ sudo apt-get install git`
1414
2. Clone repo: Clone this repository on your raspberry pi in your `home` directory. (Tip: run `$ cd ~` to go to your home directory)
15-
* `$ git clone git@github.com:achaudhry/adhan.git`
15+
* `$ git clone <get repo clone url from github and put it here>`
1616
* After doing that you should see an `adhan` direcotry in your `home` directory.
17-
3. Go into `adhan` directory: `$cd adhan`
18-
4. Open `updateAzaanTimers.py` in your favorite editor. For instance, `nano` is a simple one: `$ nano updateAzaanTimers.py`
1917

20-
## Configuration
21-
The original python script is super configurable. Please see the [manual](http://praytimes.org/manual) for advanced instructions. However, below are the three basic things you'll need to change to get it up and running.
18+
## Run it for the first time
19+
Run this command:
2220

23-
* Set the latitude and longitude so it can calculate accurate prayer times for that location. Modify the following lines:
24-
```
25-
#Set latitude and longitude here
26-
#--------------------
27-
lat = 42.3601
28-
long = -71.0589
29-
```
30-
* Set adhan time [calculation method](http://praytimes.org/manual#Set_Calculation_Method). Modify the following line:
21+
```bash
22+
$ python /home/pi/adhan/updateAzaanTimers.py --lat <YOUR_LAT> --lng <YOUR_LNG> --method <METHOD>
3123
```
32-
PT.setMethod('ISNA')
33-
```
34-
Save your changes by pressing `Control X` and then `Y`.
3524

36-
## Run it for the first time
37-
Run this command `$ python /home/pi/adhan/updateAzaanTimers.py`. If everythig worked, your output will look something like this:
25+
Replace the arguments above with your location information and calculation method:
26+
* Set the latitude and longitude so it can calculate accurate prayer times for that location.
27+
* Set adhan time [calculation method](http://praytimes.org/manual#Set_Calculation_Method).
28+
29+
If everythig worked, your output will look something like this:
3830
```
31+
20 60 Egypt 0 0
3932
05:51
4033
11:52
4134
14:11
@@ -53,8 +46,16 @@ Script execution finished at: 2017-01-06 21:22:31.512667
5346

5447
If you look at the last few lines, you'll see that 5 adhan times have been scheduled. Then there is another line at the end which makes sure that at 1am every day the same script will run and calculate adhan times for that day. And lastly, there is a line to clear logs on a monthly basis so that your log file doesn't grow too big.
5548

49+
Note that for later runs you do not have to supply any arguments as they are saved in `/home/pi/adhan/.settings`.
50+
5651
VOILA! You're done!! Plug in your speakers and enjoy!
5752

53+
Please see the [manual](http://praytimes.org/manual) for advanced configuration instructions.
54+
55+
There are 2 additional arguments that are optional, you can set them in the first run or
56+
further runs: `--fajr-azaan-volume` and `azaan-volume`. You can control the volume of the Azaan
57+
by supplying numbers in millibels. To get more information on how to select the values, run the command with `-h`.
58+
5859
## Tips:
5960
1. You can see your currently scheduled jobs by running `crontab -l`
6061
2. The output of the job that runs at 1am every night is being captured in `/home/pi/adhan/adhan.log`. This way you can keep track of all successful runs and any potential issues. This file will be truncated at midnight on the forst day of each month. To view the output type `$ cat /home/pi/adhan/adhan.log`
@@ -64,4 +65,3 @@ I have made modifications / bug fixes but I've used the following as starting po
6465
* Python code to calculate adhan times: http://praytimes.org/code/
6566
* Basic code to turn the above into an adhan clock: http://randomconsultant.blogspot.co.uk/2013/07/turn-your-raspberry-pi-into-azaanprayer.html
6667
* Cron scheduler: https://pypi.python.org/pypi/python-crontab/
67-

adhan.log

-14
This file was deleted.

crontab/crontab.pyc

-42.5 KB
Binary file not shown.

praytimes.pyc

-13.1 KB
Binary file not shown.

updateAzaanTimers.py

+89-31
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,71 @@
33
import datetime
44
import time
55
import sys
6-
sys.path.insert(0, '/home/pi/adhan/crontab')
6+
from os.path import dirname, abspath, join as pathjoin
7+
import argparse
8+
9+
root_dir = dirname(abspath(__file__))
10+
sys.path.insert(0, pathjoin(root_dir, 'crontab'))
711

812
from praytimes import PrayTimes
913
PT = PrayTimes()
1014

1115
from crontab import CronTab
1216
system_cron = CronTab(user='pi')
1317

14-
now = datetime.datetime.now()
15-
strPlayFajrAzaanMP3Command = 'omxplayer -o local /home/pi/adhan/Adhan-fajr.mp3 > /dev/null 2>&1'
16-
strPlayAzaanMP3Command = 'omxplayer -o local /home/pi/adhan/Adhan-Makkah.mp3 > /dev/null 2>&1'
17-
strUpdateCommand = 'python /home/pi/adhan/updateAzaanTimers.py >> /home/pi/adhan/adhan.log 2>&1'
18-
strClearLogsCommand = 'truncate -s 0 /home/pi/adhan/adhan.log 2>&1'
19-
strJobComment = 'rpiAdhanClockJob'
20-
21-
#Set latitude and longitude here
22-
#--------------------
23-
lat = 42.288788
24-
long = -71.551678
25-
26-
#Set calculation method, utcOffset and dst here
27-
#By default system timezone will be used
28-
#--------------------
29-
PT.setMethod('ISNA')
30-
utcOffset = -(time.timezone/3600)
31-
isDst = time.localtime().tm_isdst
32-
33-
3418
#HELPER FUNCTIONS
3519
#---------------------------------
3620
#---------------------------------
3721
#Function to add azaan time to cron
22+
def parseArgs():
23+
parser = argparse.ArgumentParser(description='Calculate prayer times and install cronjobs to play Adhan')
24+
parser.add_argument('--lat', type=float, dest='lat',
25+
help='Latitude of the location, for example 30.345621')
26+
parser.add_argument('--lng', type=float, dest='lng',
27+
help='Longitude of the location, for example 60.512126')
28+
parser.add_argument('--method', choices=['MWL', 'ISNA', 'Egypt', 'Makkah', 'Karachi', 'Tehran', 'Jafari'],
29+
dest='method',
30+
help='Method of calculation')
31+
parser.add_argument('--fajr-azaan-volume', type=int, dest='fajr_azaan_vol',
32+
help='Volume for fajr azaan in millibels, 1500 is loud and -30000 is quiet (default 0)')
33+
parser.add_argument('--azaan-volume', type=int, dest='azaan_vol',
34+
help='Volume for azaan (other than fajr) in millibels, 1500 is loud and -30000 is quiet (default 0)')
35+
return parser
36+
37+
def mergeArgs(args):
38+
file_path = pathjoin(root_dir, '.settings')
39+
# load values
40+
lat = lng = method = fajr_azaan_vol = azaan_vol = None
41+
try:
42+
with open(file_path, 'rt') as f:
43+
lat, lng, method, fajr_azaan_vol, azaan_vol = f.readlines()[0].split(',')
44+
except:
45+
print('No .settings file found')
46+
# merge args
47+
if args.lat:
48+
lat = args.lat
49+
if lat:
50+
lat = float(lat)
51+
if args.lng:
52+
lng = args.lng
53+
if lng:
54+
lng = float(lng)
55+
if args.method:
56+
method = args.method
57+
if args.fajr_azaan_vol:
58+
fajr_azaan_vol = args.fajr_azaan_vol
59+
if fajr_azaan_vol:
60+
fajr_azaan_vol = int(fajr_azaan_vol)
61+
if args.azaan_vol:
62+
azaan_vol = args.azaan_vol
63+
if azaan_vol:
64+
azaan_vol = int(azaan_vol)
65+
# save values
66+
with open(file_path, 'wt') as f:
67+
f.write('{},{},{},{},{}'.format(lat or '', lng or '', method or '',
68+
fajr_azaan_vol or 0, azaan_vol or 0))
69+
return lat or None, lng or None, method or None, fajr_azaan_vol or 0, azaan_vol or 0
70+
3871
def addAzaanTime (strPrayerName, strPrayerTime, objCronTab, strCommand):
3972
job = objCronTab.new(command=strCommand,comment=strPrayerName)
4073
timeArr = strPrayerTime.split(':')
@@ -43,15 +76,15 @@ def addAzaanTime (strPrayerName, strPrayerTime, objCronTab, strCommand):
4376
job.minute.on(int(min))
4477
job.hour.on(int(hour))
4578
job.set_comment(strJobComment)
46-
print job
79+
print(job)
4780
return
4881

4982
def addUpdateCronJob (objCronTab, strCommand):
5083
job = objCronTab.new(command=strCommand)
5184
job.minute.on(15)
5285
job.hour.on(3)
5386
job.set_comment(strJobComment)
54-
print job
87+
print(job)
5588
return
5689

5790
def addClearLogsCronJob (objCronTab, strCommand):
@@ -60,22 +93,47 @@ def addClearLogsCronJob (objCronTab, strCommand):
6093
job.minute.on(0)
6194
job.hour.on(0)
6295
job.set_comment(strJobComment)
63-
print job
96+
print(job)
6497
return
6598
#---------------------------------
6699
#---------------------------------
67100
#HELPER FUNCTIONS END
68101

102+
#Parse arguments
103+
parser = parseArgs()
104+
args = parser.parse_args()
105+
#Merge args with saved values if any
106+
lat, lng, method, fajr_azaan_vol, azaan_vol = mergeArgs(args)
107+
print(lat, lng, method, fajr_azaan_vol, azaan_vol)
108+
#Complain if any mandatory value is missing
109+
if not lat or not lng or not method:
110+
parser.print_usage()
111+
sys.exit(1)
112+
113+
#Set calculation method, utcOffset and dst here
114+
#By default system timezone will be used
115+
#--------------------
116+
PT.setMethod(method)
117+
utcOffset = -(time.timezone/3600)
118+
isDst = time.localtime().tm_isdst
119+
120+
now = datetime.datetime.now()
121+
strPlayFajrAzaanMP3Command = 'omxplayer --vol {} -o local {}/Adhan-fajr.mp3 > /dev/null 2>&1'.format(fajr_azaan_vol, root_dir)
122+
strPlayAzaanMP3Command = 'omxplayer --vol {} -o local {}/Adhan-Madinah.mp3 > /dev/null 2>&1'.format(azaan_vol, root_dir)
123+
strUpdateCommand = 'python {}/updateAzaanTimers.py >> {}/adhan.log 2>&1'.format(root_dir, root_dir)
124+
strClearLogsCommand = 'truncate -s 0 {}/adhan.log 2>&1'.format(root_dir)
125+
strJobComment = 'rpiAdhanClockJob'
126+
69127
# Remove existing jobs created by this script
70128
system_cron.remove_all(comment=strJobComment)
71129

72130
# Calculate prayer times
73-
times = PT.getTimes((now.year,now.month,now.day), (lat, long), utcOffset, isDst)
74-
print times['fajr']
75-
print times['dhuhr']
76-
print times['asr']
77-
print times['maghrib']
78-
print times['isha']
131+
times = PT.getTimes((now.year,now.month,now.day), (lat, lng), utcOffset, isDst)
132+
print(times['fajr'])
133+
print(times['dhuhr'])
134+
print(times['asr'])
135+
print(times['maghrib'])
136+
print(times['isha'])
79137

80138
# Add times to crontab
81139
addAzaanTime('fajr',times['fajr'],system_cron,strPlayFajrAzaanMP3Command)
@@ -91,4 +149,4 @@ def addClearLogsCronJob (objCronTab, strCommand):
91149
addClearLogsCronJob(system_cron,strClearLogsCommand)
92150

93151
system_cron.write_to_user(user='pi')
94-
print 'Script execution finished at: ' + str(now)
152+
print('Script execution finished at: ' + str(now))

0 commit comments

Comments
 (0)