-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprayer_time.py
More file actions
248 lines (171 loc) · 7.31 KB
/
prayer_time.py
File metadata and controls
248 lines (171 loc) · 7.31 KB
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
#!/usr/bin/env python3
'''program to get a notification when prayer time comes'''
# For web
import requests, re
# For files
import sys
import os
# For notifications and other things
from plyer import notification
import time
def Errors(err, error_time):
'''this function deal with errors and log the error if it was repeated 3 times
for example :
If connection error happend, it will retry 3 times every 20 minutes
then it will log the error and close the program.'''
global _attempts
_attempts += 1
if _attempts == 3:
try:
print("Couldn't fix the problem, check the error log..")
with open(error_log, 'a') as file:
file.write(f'[ERROR] [{error_time}] --> {err}\n\n')
file.write(f'Error type : {type(err).__name__}\n')
file.write(f'Line : {(sys.exc_info()[2]).tb_lineno}\n')
file.write(f'For additional help, you can contact me at : sultanxx575@gmail.com\n')
file.write(f"{'-'*80}\n")
sys.exit()
except Exception as e:
error_time_2 = time.strftime("%Y-%m-%d %H:%M", time.localtime()) # time of the error
print(
f"""The program was trying to write an error to the error log but it seems that another
error appeared in the error log function, so this process has failed. These are the errors :
- error information of the error that faced the error log function are:
error name : {e}
error time : {error_time_2}
error line : {(sys.exc_info()[2]).tb_lineno}
error type : {type(e).__name__}
- error information for the second error that happend somewhere that isn't in the error log function are:
error name : {err}
error time : {error_time}
error type : {type(err).__name__}
if you keep getting the same problem contact me at
sultanxx575@gmail.com"""
)
sys.exit()
else:
time.sleep(1200)
return main()
def PrayerNotification(name):
'''Notifications function'''
global _attempts
try:
#------------------------------------------------------------------------------
if name == "c": # Error in connection
return notification.notify(
app_name="أوقات الصلاة",
title="أوقات الصلاة",
message='تأكد من إتصالك بالإنترنت',
app_icon='', # Icon path should be determined by you
)
elif name == 'n': # None or something else wrong with finding prayer times
return notification.notify(
app_name="أوقات الصلاة",
title="أوقات الصلاة",
message='هنالك مشكلة في البحث عن أوقات الصلاة',
app_icon='', # Icon path should be determined by you
)
elif name == 'w': # wrong city name
return notification.notify(
app_name="أوقات الصلاة",
title="أوقات الصلاة",
message='هنالك خطأ, تأكد من اسم المدينة\nError, check the city name',
app_icon='', # Icon path should be determined by you
)
#------------------------------------------------------------------------------
_attempts = 0
if time.strftime("%A") == "Friday" and name == "الظهر":
name = 'الجمعة'
if name == "الضحى": # Al duha
time.sleep(1500) # 20 minutes and extra 5 minutes to avoid any errors from the site
# For the 6 prayers
# 1- Fajir
# 2- Al duhr (Al jumaah if it was friday)
# 3- Al asr
# 4- Al maghrib
# 5- Al isha
# 6- Al duha
return notification.notify(
app_name="أوقات الصلاة",
title="أوقات الصلاة",
message='حان الآن وقت صلاة ' + name,
app_icon='', # Icon path should be determined by a user
)
except Exception as e:
error_time = time.strftime("%Y-%m-%d %H:%M", time.localtime())
Errors(e, error_time)
def main():
'''getting the time of the prayer and calling the notifications function'''
prayers_times = {}
try:
response = requests.get(f'https://saudi-arabia.prayertiming.net/en/{city}')
except Exception as e:
error_time = time.strftime("%Y-%m-%d %H:%M", time.localtime())
PrayerNotification('c')
Errors(e, error_time)
try:
# regular expression to get the prayer times from the page
regex = re.compile(r'<td>([0-9]{2}:[0-9]{2} (?:AM|PM))</td>')
if regex.findall(response.text):
response = regex.findall(response.text)
# Assigning the prayers names as key and the times as variables
prayers_times["الفجر"] = response[0]
prayers_times["الضحى"] = response[1]
prayers_times["الظهر"] = response[2]
prayers_times["العصر"] = response[3]
prayers_times["المغرب"] = response[4]
prayers_times["العشاء"] = response[5]
while True:
for prayer in prayers_times:
if time.strftime("%H:%M %p") == prayers_times[prayer]:
# checking if the current time equals that prayer time
# if it does then it will call the notification function PrayerNotification()
PrayerNotification(name=prayer)
time.sleep(1200) # let the program have a break for 30 minutes
time.sleep(1)
except Exception as e:
error_time = time.strftime("%Y-%m-%d %H:%M", time.localtime())
PrayerNotification('n')
Errors(e, error_time)
''' cities manual :
- makkah
- jeddah
- riyadh
- jaizan
- al kharj
- buraidah
- hafr albatin
'''
# Optional, default is makkah
city = 'makkah' # city name, check the city manual above for more info
# Optional, default is the directory of this file
current_folder: str = re.search(r'(.+\\).+$', __file__)[1]
# Optional, default is the directory of this file
error_log: str = current_folder + 'error.log'
# these are the paths of each city in the website I get the prayer times from
cities = {
"makkah": "c16/Makkah", #
"jeddah": "c13/Jeddah",
"riyadh": "c10/Riyadh",
"jaizan": "c12/Jaizan",
"al kharj": "c6/Al+kharj",
"buraidah": "c11/Buraidah",
"hafr albatin": "c1967/Hafr+albatin"
}
try:
city = cities[city.lower()]
except KeyError as e:
error_time = time.strftime("%Y-%m-%d %H:%M", time.localtime())
print(
'''Either you entered the city name wrong or the city is not supported in the program
contact me for adding new cities or if you have any problems or questions :
Github account : https://github.com/SultanCYB
my Email account : sultanxx575@gmail.com'''
)
PrayerNotification('w')
Errors(e)
sys.exit()
# global variable
_attempts: int = 0
if __name__ == '__main__':
main()