2 # -*- coding: utf-8 -*-
6 Copyright : MIT License
8 Description : This app return the left time to the next pray.
9 uses aladhan API and caching.
14 from datetime import datetime
15 from datetime import timedelta
17 # from requests import ConnectionError
19 def str_to_datetime(the_date, the_time):
20 """Get a string format return date time format."""
21 concat_time_date = str(the_date + " " + the_time)
22 return datetime.strptime(concat_time_date, '%d-%m-%Y %H:%M')
25 def str_date(date_string):
26 """Convert a string to date time format."""
27 return datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S')
30 def duration_format(duration):
31 """Get hours and minutes only."""
32 regex_hm = re.search(r"(\d{1,2}:\d{1,2})", str(duration))
33 return str(regex_hm.groups()[0])
36 def file_exists(file_name):
37 """Check file existance."""
38 return bool(os.path.isfile(file_name))
40 def send_notification(title, body):
41 os.system(f"notify-send \"{title}\" \"{body}\"")
52 self.today = datetime.strftime(datetime.now(), "%d_%m_%Y")
53 self.current_time = datetime.now()
54 self.today_cache_file_name = self.cache_dir + self.today + ".txt"
56 # Check if there is a cache file
57 if file_exists(self.today_cache_file_name):
58 self.read_cache_file()
60 self.get_from_internet()
66 self.maghrib: "Maghrib",
68 self.tomorrow_fajr: "Fajr",
71 self.print_next_pray()
74 # print("/home/hassan/Dropbox/development/linux_conf/tint2/icons/")
77 def remaining_duration(self, comming_time):
78 return duration_format(comming_time - self.current_time)
81 def read_cache_file(self):
82 """Reads cache file."""
83 with open(self.today_cache_file_name, 'r') as file:
84 # lines = f.readlines()
85 lines = file.read().splitlines()
87 self.fajr = str_date(lines[0])
88 self.duhr = str_date(lines[1])
89 self.asr = str_date(lines[2])
90 self.maghrib = str_date(lines[3])
91 self.isha = str_date(lines[4])
92 self.tomorrow_fajr = str_date(lines[5])
93 self.hijri_date = lines[6][:5]
96 def get_from_internet(self):
97 """Use aladhan API and create a file with data."""
99 api_url = "http://api.aladhan.com/v1/timingsByCity?city=Mekka&"
100 api_url += "country=Saudi%20Arabia&method=8"
101 the_request = requests.get(api_url)
102 data = the_request.json()
104 pray_times = data["data"]["timings"]
105 gregorian_date = data["data"]["date"]["gregorian"]["date"]
106 hijri_date = data["data"]["date"]["hijri"]["date"]
108 fajr = str_to_datetime(gregorian_date, pray_times["Fajr"])
109 duhr = str_to_datetime(gregorian_date, pray_times["Dhuhr"])
110 asr = str_to_datetime(gregorian_date, pray_times["Asr"])
111 maghrib = str_to_datetime(gregorian_date, pray_times["Maghrib"])
112 isha = str_to_datetime(gregorian_date, pray_times["Isha"])
113 tomorrow_fajr = fajr + timedelta(days=1)
115 # Create a cache file for today
116 with open(self.today_cache_file_name, 'w') as file:
121 str(maghrib) + "\n" +
123 str(tomorrow_fajr) + "\n" +
126 except requests.ConnectionError:
131 def print_next_pray(self):
132 """Compares current time with pray times and print the left time."""
134 for each_pray_time in self.pray_dic:
136 if self.current_time < each_pray_time:
138 # "<span background=\"green\" foreground=\"white\">" +
139 str(self.hijri_date) + " " +
140 self.pray_dic[each_pray_time] + " " +
141 self.remaining_duration(each_pray_time)
145 if (self.remaining_duration(each_pray_time)) == "0:00":
148 body=self.pray_dic[each_pray_time])
152 if __name__ == "__main__":
156 except KeyboardInterrupt:
157 print("Keyboard Interrupted")