change vifm colors
[dotfiles_afify.git] / .scripts / bar_scripts / azan
blobc31dfef7e2c56aa2e3855d7675bcf45f43bf184f
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
4 """
5 GitHub      : Hassan3Afify.
6 Copyright   : MIT License
7 Version     : 0.1
8 Description : This app return the left time to the next pray.
9               uses aladhan API and caching.
10 """
12 import os
13 import re
14 from datetime import datetime
15 from datetime import timedelta
16 import requests
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}\"")
44 class Azan():
45     """Discription."""
47     def __init__(self):
48         """Assign inits."""
50         self.cache_dir = (
51             "/tmp/")
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()
59         else:
60             self.get_from_internet()
62         self.pray_dic = {
63             self.fajr: "Fajr",
64             self.duhr: "Duhr",
65             self.asr: "Asr",
66             self.maghrib: "Maghrib",
67             self.isha: "Isha",
68             self.tomorrow_fajr: "Fajr",
69         }
71         self.print_next_pray()
73         # The icon
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."""
98         try:
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:
117                 file.write(
118                     str(fajr) + "\n" +
119                     str(duhr) + "\n" +
120                     str(asr) + "\n" +
121                     str(maghrib) + "\n" +
122                     str(isha) + "\n" +
123                     str(tomorrow_fajr) + "\n" +
124                     str(hijri_date)
125                     )
126         except requests.ConnectionError:
127             print("No Int")
128             # remove the cache
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:
137                 print(
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)
142                     # "</span>"
143                     )
145                 if (self.remaining_duration(each_pray_time)) == "0:00":
146                     send_notification(
147                         title="Azan",
148                         body=self.pray_dic[each_pray_time])
149                 break
152 if __name__ == "__main__":
153     try:
154         Azan()
156     except KeyboardInterrupt:
157         print("Keyboard Interrupted")