Skip to content

Commit

Permalink
Merge pull request #1 from adeeonline/feature/v2
Browse files Browse the repository at this point in the history
Added json file to configure parameters
  • Loading branch information
adeeonline authored Apr 16, 2020
2 parents de82aa7 + 62a641b commit 924417c
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 94 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.vscode
.vscode
__pycache__
40 changes: 22 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,40 @@
### Installation

1. Clone this repository
2. Run `pip install adhan`
3. Run `python azan-cron.py`
2. Run `pip install -r requirements.txt`
3. Open `config.json` and fill in details
4. Run `python azaan-cron.py`

### Options
### Configure config.json

Azaan Times are calculated based on various options. Here are some ways on how to configure them:

**Geo location Coordinates**
You can change the current location coordinates by opening `azan-cron.py` file and change this line `coordinates = (37.3441254,-121.9806518)` to use your location coordinates.
Azaan Times are calculated based on various options which you can change in `config.json`. Here are some ways on how to configure them:

**coordinates**
You can change the `lat` and `lng` in `config.json`
*Default: `(37.3441254,-121.9806518)`*

**Azan Time Calculation Method**
Azan times are calculated based on some method there are various methods provided by the Adhan library I am using. You can change the caculation method by going to `azan-cron.py`
**calculation-method**
- isna
- muslim-world-league
- egypt
- makkah
- karachi
- tehran
- shia

1. Import the method you want here `from adhan.methods import ISNA, ASR_HANAFI`
2. Update these line with yoru methods
`params.update(ISNA)`
`params.update(ASR_HANAFI)`
*Default: `isna`*

For all available options please visit the original library page here https://github.com/hayalasalah/adhan.py
**asr-method**
- standard
- hanafi

*Default: `ISNA` and `ASR_HANAFI`*
*Default: `hanafi`*

**Adding More Azan Sounds**
You can add more Azan sounds. Goto `assets/audio` and add more Azan but make sure the file is in this format `azan-<increment-number>` E.g: `azan-1.mp3`
### Adding More Azan Sounds
You can add more Azan sounds by copying azaan sound files to `assets/audio` directory for regular azaan or `assets/audio/fajr` for fajr azaan.

*Default: `1 Fajr, 2 Regular`*


### Credits
I used this library https://github.com/hayalasalah/adhan.py for calculating azan times.

Expand Down
Binary file modified assets/audio/fajr/azan-fajr.mp3
Binary file not shown.
13 changes: 13 additions & 0 deletions azaan-cron.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import json
from src.AzaanClock import AzaanClock

with open('config.json') as jsonFile:
configs = json.load(jsonFile)

azaanClock = AzaanClock(
configs["calculation-method"],
configs["asr-method"],
configs["coordinates"]
)

azaanClock.start()
74 changes: 0 additions & 74 deletions azan-cron.py

This file was deleted.

8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"coordinates": {
"lat": 37.3441254,
"lng": -121.9806518
},
"calculation-method": "isna",
"asr-method": "hanafi"
}
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
adhan
pygame
112 changes: 112 additions & 0 deletions src/AzaanClock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
from datetime import datetime as dt
from adhan import adhan
import adhan.methods
import datetime, sys, random, time, pygame, os, os.path, calendar

class AzaanClock:

def __init__(self, caculationMethod, asrMethod, coordinates):
self.caculationMethod = caculationMethod
self.asrMethod = asrMethod
self.currentUtcOffset = -((calendar.timegm(time.gmtime()) - calendar.timegm(time.localtime()))) / 3600
self.coordinates = (coordinates["lat"], coordinates["lng"])
dirname = os.path.dirname(__file__)
assetsDir = '../assets/audio/'
self.basePath = os.path.join(dirname, assetsDir)


def playAzaan(self, azaan):
filePath = ""
if azaan == "fajr":
filePath = self.basePath + "fajr/" + self.getRandomFile(self.basePath + "fajr/")
else:
filePath = self.basePath + self.getRandomFile(self.basePath)

self.playSoundAtPath(filePath)


def getCurrentAzaan(self, adhanTimes):
azaanName = ""
azaanSleepInterval = 86400
azaanTime = ""

for key in adhanTimes:
d = adhanTimes[key] - dt.now()
nextAzaanTime = int(d.total_seconds())

if nextAzaanTime < azaanSleepInterval and nextAzaanTime > 0:
azaanSleepInterval = nextAzaanTime
azaanName = key
azaanTime = adhanTimes[key]

return (azaanName, azaanSleepInterval, azaanTime)


def getRandomFile(self, path):
files = []
for (_, _, filenames) in os.walk(path):
files.extend(filenames)
break
print(files)
fileCount = len(files) - 1
random.seed(time.clock())
index = random.randint(0, fileCount)
return files[index]


def playSoundAtPath(self, path):
pygame.mixer.init()
pygame.mixer.music.load(path)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy() == True:
continue


def getAzaanNameAndTime(self, date):
offset = self.currentUtcOffset
methodsMap = {
"isna": adhan.methods.ISNA,
"muslim-world-league": adhan.methods.MUSLIM_WORLD_LEAGUE,
"egypt": adhan.methods.EGYPT,
"makkah": adhan.methods.MAKKAH,
"karachi": adhan.methods.KARACHI,
"tehran": adhan.methods.TEHRAN,
"shia": adhan.methods.SHIA
}
asrMethodMap = {
"hanafi": adhan.methods.ASR_HANAFI,
"standard": adhan.methods.ASR_STANDARD
}

params = {}
params.update(methodsMap[self.caculationMethod])
params.update(asrMethodMap[self.asrMethod])
azaanDate = date

adhanTimes = adhan.adhan(
day=azaanDate,
location=self.coordinates,
parameters=params,
timezone_offset=offset,
)

del adhanTimes["shuruq"]
return self.getCurrentAzaan(adhanTimes)


def start(self):
while True:
date = dt.today()
nameAndTime = self.getAzaanNameAndTime(date)

if nameAndTime[0] == "":
date = date + datetime.timedelta(days = 1)
nameAndTime = self.getAzaanNameAndTime(date)

print(nameAndTime)

time.sleep(nameAndTime[1])
self.playAzaan(nameAndTime[0])



2 changes: 1 addition & 1 deletion system-services/azaan-clock.service
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Description=Run Azaan clock as a Service

[Service]
ExecStart=python -u /home/pi/azaan-clock/azan-cron.py
ExecStart=python -u /home/pi/azaan-clock/azaan-cron.py
StandardOutput=inherit
StandardError=inherit
Restart=always
Expand Down

0 comments on commit 924417c

Please sign in to comment.