forked from Jessewb786/Silaty
-
Notifications
You must be signed in to change notification settings - Fork 3
/
prayertime.py
executable file
·459 lines (383 loc) · 16.1 KB
/
prayertime.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# prayertime.py
#
# Copyright 2010 ahmed youssef <[email protected]>
# Copyright 2014 Jesse Brandao <[email protected]>
# Copyright 2019 AXeL-dev <[email protected]>
#
# This program is free software you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
__author__ = "Jesse Wayde Brandão (Abdul Hakim)"
__all__ = ['Calendar', 'Prayertime', 'Madhab', 'as_pytime', 'as_pydatetime']
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('Notify', '0.7')
from gi.repository import Gtk, Gst, GObject, Gio, GLib, GdkPixbuf, Notify
from math import degrees, radians, atan, atan2, asin, acos, cos, sin, tan, fabs
from datetime import date, timedelta
from time import strptime
from options import *
from translate import translate_text as _
import datetime
import time
import os
import subprocess
class Prayertime(object):
def __init__(self):
GLib.threads_init()
Gst.init(None)
self.options = Options()
year = datetime.datetime.now().year
month = datetime.datetime.now().month
day = datetime.datetime.now().day
self.date = date(year, month, day)
self._shrouk = None
self._fajr = None
self._zuhr = None
self._asr = None
self._maghrib = None
self._isha = None
self._nextprayer = ""
self._tnprayer = 0
self.dec = 0
def shrouk_time(self):
"""Gets the time of shrouk."""
fmt = to_hrtime(self._shrouk, True)
if "00" in fmt:
fmt = fmt.replace('00','12')
return fmt
def fajr_time(self):
"""Gets the time of fajr."""
fmt = to_hrtime(self._fajr, True)
if "00" in fmt:
fmt = fmt.replace('00','12')
return fmt
def zuhr_time(self):
"""Gets the time of zuhr."""
fmt = to_hrtime(self._zuhr, True)
if "00" in fmt:
fmt = fmt.replace('00','12')
return fmt
def asr_time(self):
"""Gets the time of asr."""
fmt = to_hrtime(self._asr)
if "00" in fmt:
fmt = fmt.replace('00','12')
return fmt
def maghrib_time(self):
"""Gets the time of maghrib"""
fmt = to_hrtime(self._maghrib)
if "00" in fmt:
fmt = fmt.replace('00','12')
return fmt
def isha_time(self):
"""Gets the time of isha"""
fmt = to_hrtime(self._isha)
if "00" in fmt:
fmt = fmt.replace('00','12')
return fmt
def next_prayer(self):
return self._nextprayer
def time_to_next_prayer(self):
return self._tnprayer
def calculate(self, notify_also = True):
"""Calculations of prayertimes."""
year = self.date.year
month = self.date.month
day = self.date.day
longitude = self.options.longitude
latitude = self.options.latitude
zone = self.options.timezone
julian_day = (367*year)-int(((year+int((month+9)/12))*7)/4)+int(275*month/9)+day-730531.5
sun_length = (280.461+0.9856474*julian_day)%360
middle_sun = (357.528+0.9856003*julian_day)%360
lamda = (sun_length+1.915*sin(radians(middle_sun))+0.02*sin(radians(2*middle_sun)))%360
obliquity = 23.439-0.0000004*julian_day
alpha = degrees(atan(cos(radians(obliquity))*tan(radians(lamda))))
if 90 < lamda < 180:
alpha += 180
elif 179 < lamda < 360:
alpha += 360
ST = (100.46+0.985647352*julian_day)%360
self.dec = degrees(asin(sin(radians(obliquity))*sin(radians(lamda))))
noon = alpha-ST
if noon < 0:
noon += 360
UTNoon = noon-longitude
zuhr = (UTNoon/15)+zone # Zuhr Time.
maghrib = zuhr+self._equation(-0.8333)/15 # Maghrib Time
shrouk = zuhr-self._equation(-0.8333)/15 # Shrouk Time
fajr_alt = 0
isha_alt = 0
if self.options.calculation_method == Calendar.UmmAlQuraUniv:
fajr_alt = -19
elif self.options.calculation_method == Calendar.EgyptianGeneralAuthorityOfSurvey:
fajr_alt = -19.5
isha_alt = -17.5
elif self.options.calculation_method == Calendar.MuslimWorldLeague:
fajr_alt = -18
isha_alt = -17
elif self.options.calculation_method == Calendar.IslamicSocietyOfNorthAmerica:
fajr_alt = isha_alt = -15
elif self.options.calculation_method == Calendar.UnivOfIslamicSciencesKarachi:
fajr_alt = isha_alt = -18
fajr = zuhr-self._equation(fajr_alt)/15 # Fajr Time
isha = zuhr+self._equation(isha_alt)/15 # Isha Time
if self.options.calculation_method == Calendar.UmmAlQuraUniv :
isha = maghrib+1.5
asr_alt = 0
if self.options.madhab == Madhab.Hanafi :
asr_alt = 90 - degrees(atan(2+tan(radians(abs(latitude - self.dec)))))
else:
asr_alt = 90 - degrees(atan(1 + tan(radians(abs(latitude - self.dec)))))
asr = zuhr+self._equation(asr_alt)/15 # Asr Time.
# Add one hour to all times if the season is Summmer.
if self.options.daylight_saving_time and is_dst() :
fajr += 1
shrouk += 1
zuhr += 1
asr += 1
maghrib += 1
isha += 1
self._shrouk = shrouk
self._fajr = fajr
self._zuhr = zuhr
self._asr = asr
self._maghrib = maghrib
self._isha = isha
# Transform Times To DateTimes ,so We Can Make Calculations on it
Fajr = datetime.datetime.strptime(self.fajr_time(),"%I:%M:%S %p")
Dhuhr = datetime.datetime.strptime(self.zuhr_time(),"%I:%M:%S %p")
Asr = datetime.datetime.strptime(self.asr_time(),"%I:%M:%S %p")
Maghrib = datetime.datetime.strptime(self.maghrib_time(),"%I:%M:%S %p")
Isha = datetime.datetime.strptime(self.isha_time(),"%I:%M:%S %p")
# Assign Times to A List
PrayerTimes = [Fajr, Dhuhr, Asr, Maghrib, Isha]
Time = datetime.datetime.now()# Time Now
Time = Time.replace(microsecond=0, year=1900, month=1, day=1)# Replace year,month and day to be the same on PrayerTimes
NotifTime = Time+datetime.timedelta(minutes=self.options.notification_time)# Ten Minutes Before Next Prayer
ClosestPrayer = self.closest(Time, PrayerTimes)# Get The Closest Prayer Time
PrayerIndex = PrayerTimes.index(ClosestPrayer)# Get Index From List
if PrayerIndex == 0:
CurrentPrayer='Fajr'
if Time < Fajr:
PrevPrayer = 'Isha'
NextPrayer = 'Fajr'
NextPrayerDT = Fajr
else:
PrevPrayer = 'Fajr'
NextPrayer = 'Dhuhr'
NextPrayerDT = Dhuhr
if PrayerIndex == 1:
CurrentPrayer = 'Dhuhr'
if Time < Dhuhr:
PrevPrayer = 'Fajr'
NextPrayer = 'Dhuhr'
NextPrayerDT = Dhuhr
else:
PrevPrayer = 'Dhuhr'
NextPrayer = 'Asr'
NextPrayerDT = Asr
elif PrayerIndex == 2:
CurrentPrayer = 'Asr'
if Time < Asr:
PrevPrayer = 'Dhuhr'
NextPrayer = 'Asr'
NextPrayerDT = Asr
else:
PrevPrayer = 'Asr'
NextPrayer = 'Maghrib'
NextPrayerDT = Maghrib
elif PrayerIndex == 3:
CurrentPrayer = 'Maghrib'
if Time < Maghrib:
PrevPrayer = 'Asr'
NextPrayer = 'Maghrib'
NextPrayerDT = Maghrib
else:
PrevPrayer = 'Maghrib'
NextPrayer = 'Isha'
NextPrayerDT = Isha
elif PrayerIndex == 4:
CurrentPrayer = 'Isha'
if Time < Isha:
PrevPrayer = 'Maghrib'
NextPrayer = 'Isha'
NextPrayerDT = Isha
else:
PrevPrayer = 'Isha'
NextPrayer = 'Fajr'
NextPrayerDT = Fajr
# Calculate Time to The Next Prayer
TimeToNextPrayer = NextPrayerDT-Time
if TimeToNextPrayer.total_seconds() < 0:
# Add a day to fix the timining
TimeToNextPrayer = TimeToNextPrayer + timedelta(days=1)
self._nextprayer = NextPrayer
self._tnprayer = TimeToNextPrayer
# Notification
if notify_also:
for time in PrayerTimes:
if time == NotifTime:
self.notify(_('Get Ready'), _('%s minutes left until the %s prayer.') % (str(int(self.options.notification_time)), _(NextPrayer)))
elif time == Time:
self.notify(_('Prayer time for %s') % _(CurrentPrayer), _("It's time for the %s prayer.") % _(CurrentPrayer), self.options.audio_notifications, CurrentPrayer)
def muteApps(self, value = 'true'):
try:
inputs = subprocess.getoutput('pacmd list-sink-inputs | sed -n "s/^\\s*index: \\([[:digit:]]*\\)/\\1/p"').split('\n')
clients = subprocess.getoutput('pacmd list-sink-inputs | sed -n "s/^\\s*client: [[:digit:]]* <\\(.*\\)>/\\1/p"').split('\n')
for i in range(len(inputs)):
if clients[i] not in ['silaty-indicator', 'ZOOM VoiceEngine']:
subprocess.Popen(['pacmd', 'set-sink-input-mute', inputs[i], value])
except:
print ("DEBUG: Cannot mute apps @", (str(datetime.datetime.now())))
def notify(self, title, message, play_audio = False, current_prayer = ''):
Notify.init("Silaty")
notif = Notify.Notification.new(title, message)
icon = GdkPixbuf.Pixbuf.new_from_file(os.path.dirname(os.path.realpath(__file__)) + "/icons/hicolor/128x128/apps/silaty.svg")
notif.set_icon_from_pixbuf(icon)
if play_audio:
self.muteApps()
if current_prayer == 'Fajr':
uri = "file://" + os.path.dirname(os.path.realpath(__file__)) + "/audio/Fajr/" + self.options.fajr_adhan + ".ogg"
self.fajrplayer = Gst.ElementFactory.make("playbin", "player")
fakesink = Gst.ElementFactory.make("fakesink", "fakesink")
self.fajrplayer.set_property('uri', uri)
self.fajrplayer.set_property("video-sink", fakesink)
self.fajrplayer.set_state(Gst.State.PLAYING)
bus = self.fajrplayer.get_bus()
bus.add_signal_watch()
bus.connect("message", self.on_fajrplayer_message)
else:
uri = "file://" + os.path.dirname(os.path.realpath(__file__)) + "/audio/Normal/" + self.options.normal_adhan + ".ogg"
self.normalplayer = Gst.ElementFactory.make("playbin", "player")
fakesink = Gst.ElementFactory.make("fakesink", "fakesink")
self.normalplayer.set_property('uri', uri)
self.normalplayer.set_property("video-sink", fakesink)
self.normalplayer.set_state(Gst.State.PLAYING)
bus = self.normalplayer.get_bus()
bus.add_signal_watch()
bus.connect("message", self.on_normalplayer_message)
notif.set_app_name('Silaty')
notif.show()
def on_fajrplayer_message(self, bus, message):
t = message.type
if t == Gst.MessageType.EOS: # track is finished
self.fajrplayer.set_state(Gst.State.NULL)
self.muteApps('false')
elif t == Gst.MessageType.ERROR:
self.fajrplayer.set_state(Gst.State.NULL)
self.muteApps('false')
err, debug = message.parse_error()
print ("Error: %s" % err, debug)
def on_normalplayer_message(self, bus, message):
t = message.type
if t == Gst.MessageType.EOS: # track is finished
self.normalplayer.set_state(Gst.State.NULL)
self.muteApps('false')
elif t == Gst.MessageType.ERROR:
self.normalplayer.set_state(Gst.State.NULL)
self.muteApps('false')
err, debug = message.parse_error()
print ("Error: %s" % err, debug)
def closest(self, target, collection) :# Returns the closest Adhan
return min((abs(target - i), i) for i in collection)[1]
def _equation(self, alt):
return degrees( acos( (sin(radians(alt)) - sin(radians(self.dec)) * sin(radians(self.options.latitude)))/(cos(radians(self.dec))*cos(radians(self.options.latitude)))))
def report(self):
"""Simple report of all prayertimes."""
print ('Fajr Time is %s' % self.fajr_time())
print ('Shrouk Time is %s' % self.shrouk_time())
print ('Zuhr Time is %s' % self.zuhr_time())
print ('Asr Time is %s' % self.asr_time())
print ('Maghrib Time is %s' % self.maghrib_time())
print ('Ishaa Time is %s' % self.isha_time())
def get_qibla(self):
k_lat = radians(21.423333);
k_lon = radians(39.823333);
longitude = radians(self.options.longitude)
latitude = radians(self.options.latitude)
numerator = sin(k_lon - longitude)
denominator = (cos(latitude) * tan(k_lat)) - (sin(latitude) * cos(k_lon - longitude))
q = atan2(numerator,denominator)
q = degrees(q)
if q < 0 : q += 360
return q
def qibla_distance(self):
k_lat = radians(21.423333);
k_lon = radians(39.823333);
longitude = radians(self.options.longitude)
latitude = radians(self.options.latitude)
r = 6378.7 # kilometers
return acos(sin(k_lat) * sin(latitude) + cos(k_lat) * cos(latitude) * cos(longitude-k_lon)) * r
if __name__=="__main__":
pt = Prayertime()
print ("%s" % pt.get_qibla())
print ("%s" % pt.qibla_distance())
pt.calculate()
pt.report()
def is_dst():
# Find out whether or not daylight savings is in effect
return bool(time.localtime().tm_isdst)
def fill_zeros(time):
fill = lambda var: [var, '0'+var] [len(var) <2]
return ':'.join(map(fill, time.split(':')))
def to_hrtime(var, isAM=False):
"""var: double -> human readable string of format "%I:%M:%S %p" """
time = ''
hours = int(var) # cast var (initially a double) as an int
hours_mod = hours % 12
# Check if time correspond to morning or afternoon
#print ('%s %s' % (hours, hours_mod))
if isAM:
if hours_mod > 1 and hours_mod < 12:
zone = "AM"
else:
zone = "PM"
else:
zone = "PM"
# This will give us the time from 0 to 12
if hours > 12:
time += str(hours_mod)
else:
time += str(hours)
time += ":"
var -= hours
# Get the minutes from the left over time
minutes = int(var*60)
time += str(abs(minutes))
time += ":"
var -= minutes/100
# And get the seconds from what's left over of that
sec = int(fabs(60*var))
time += str(sec)
time = fill_zeros(time)
time += " "
# Add the AM or PM
time += zone
#print ('%s' % time)
return time
def as_pytime(string_to_parse, fmt="%I:%M:%SS %p"):
"""returns time.tm_struct by parsing string_to_parse."""
return strptime(string_to_parse, fmt)
def as_pydatetime(d, ts):
"""returns a datetime object.
d: date object
ts: tm_struct
"""
return datetime(year=d.year, month=d.month, day=d.day, \
hour=ts.tm_hour, minute=ts.tm_min, second=ts.tm_sec)