-
Notifications
You must be signed in to change notification settings - Fork 5
/
util.py
145 lines (121 loc) · 4.25 KB
/
util.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
"""Utility functions."""
import json
from datetime import datetime, timedelta
import pytz
from flask import make_response
from common import DailyPrayer
from gmaps_client import GetTimezone
_GEONAMES_URL = 'http://api.geonames.org/timezoneJSON?formatted=true'
def EncodeParameter(param, spaced=False):
"""Returns param encoded to utf-8"""
if spaced:
return ' '.join(param).encode('utf-8')
return ''.join(param).encode('utf-8')
def JsonResponse(response_dict):
"""Constructs a JSON response object."""
response = make_response(json.dumps(response_dict, indent=4))
response.headers['Content-Type'] = 'application/json'
return response
def JsonError(error_text):
"""Constructs a JSON response from an error."""
response = make_response(json.dumps({'error': error_text}, indent=4))
response.headers['Content-Type'] = 'application/json'
return response
_PRAYER_METADATA = {
DailyPrayer.FAJR: {
'key_name': 'fajr',
'display_name': 'Fajr',
'pronunciation': 'Fajer',
},
DailyPrayer.SUNRISE: {
'key_name': 'sunrise',
'display_name': 'Sunrise',
'pronunciation': 'Sunrise',
},
DailyPrayer.DHUHR: {
'key_name': 'dhuhr',
'display_name': 'Dhuhr',
'pronunciation': 'Dhuhr',
},
DailyPrayer.ASR: {
'key_name': 'asr',
'display_name': 'Asr',
'pronunciation': 'Usser',
},
DailyPrayer.MAGHRIB: {
'key_name': 'maghrib',
'display_name': 'Maghrib',
'pronunciation': 'Mugreb',
},
DailyPrayer.ISHA: {
'key_name': 'isha',
'display_name': 'Isha',
'pronunciation': 'Isha',
},
DailyPrayer.QIYAM: {
'key_name': 'qiyam',
'display_name': 'Qiyam',
'pronunciation': 'Qiyam',
},
DailyPrayer.UNSPECIFIED: {
'key_name': 'unpsecified',
'display_name': 'unspecified',
'pronunciation': 'unspecified',
},
}
_KEY_NAME_TO_PRAYER = {
'suhur': DailyPrayer.FAJR,
'fajr': DailyPrayer.FAJR,
'sunrise': DailyPrayer.SUNRISE,
'dhuhr': DailyPrayer.DHUHR,
'asr': DailyPrayer.ASR,
'maghrib': DailyPrayer.MAGHRIB,
'iftar': DailyPrayer.MAGHRIB,
'isha': DailyPrayer.ISHA,
'qiyam': DailyPrayer.QIYAM,
'unspecified': DailyPrayer.UNSPECIFIED,
}
def GetPrayerKeyName(daily_prayer):
"""Gets the name of a daily prayer (ex: "fajr")."""
return _PRAYER_METADATA.get(daily_prayer).get('key_name')
def _StringToEnum(str_value, str_to_enum, default):
"""Converts a string to an enum based on provided dict."""
str_value = str(str_value).lower()
if str_value in str_to_enum:
return str_to_enum[str_value]
return default
def StringToDailyPrayer(prayer_str):
"""Infers a DailyPrayer out of a string."""
prayer_str = str(prayer_str).lower()
if prayer_str in _KEY_NAME_TO_PRAYER:
return _KEY_NAME_TO_PRAYER[prayer_str]
return ''
def GetPronunciation(daily_prayer):
"""Gets TTS for a daily prayer."""
#print 'GetPronunciation: ', _PRAYER_METADATA[daily_prayer]
return _PRAYER_METADATA[daily_prayer].get('pronunciation')
def GetDisplayText(daily_prayer):
"""Gets display text for a daily prayer."""
return _PRAYER_METADATA[daily_prayer].get('display_name')
def GetCurrentUserTime(user_lat, user_lng):
"""Returns the current time in the user's timezone."""
gmaps_timezone_str = GetTimezone(user_lat, user_lng)
if gmaps_timezone_str is None:
return None
user_timezone = pytz.timezone(gmaps_timezone_str)
user_time = datetime.now(user_timezone)
return user_time
def GetTimeDifference(user_time_datetime, prayer_time):
"""Returns the time difference in hours and minutes
between the current user time and the given prayer time."""
prayer_time_format = '%I:%M %p'
prayer_time_datetime = datetime.strptime(prayer_time, prayer_time_format)
start_time = datetime.combine(datetime.min, user_time_datetime.time())
end_time = datetime.combine(datetime.min, prayer_time_datetime.time())
if start_time > end_time:
end_time += timedelta(1)
time_diff = (end_time - start_time).total_seconds()
result = {}
result['HOURS'] = int(time_diff // 3600)
result['MINUTES'] = int((time_diff % 3600) // 60)
return result