-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrapeAPI.py
More file actions
176 lines (146 loc) · 6.07 KB
/
Copy pathscrapeAPI.py
File metadata and controls
176 lines (146 loc) · 6.07 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
# Scrapes and returns solat times
import re
import pytz
import requests
from datetime import datetime, timedelta
from logs import logger
from text import current_prayertimes, upcoming_prayertimes
sg_timezone = pytz.timezone('Asia/Singapore')
def _fetch_timetable():
"""Fetch the full MUIS timetable JSON (all dates for the year, keyed by YYYY-MM-DD)."""
url = 'https://isomer-user-content.by.gov.sg/muis_prayers_timetable.json'
# url = f'https://www.muis.gov.sg/api/pagecontentapi/GetPrayerTime?v=${str(int(time.time()))}'
headers = {
'Cache-Control': 'no-cache',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36'
}
try:
response = requests.get(url, headers=headers, timeout=10)
except requests.exceptions.Timeout:
logger.error("_fetch_timetable: request timed out after 10s")
return None
except requests.exceptions.ConnectionError as e:
logger.error(f"_fetch_timetable: connection error: {e}")
return None
except requests.exceptions.RequestException as e:
logger.error(f"_fetch_timetable: request failed: {e}")
return None
except Exception as e:
logger.error(f"_fetch_timetable: unexpected error: {e}")
return None
if response.status_code != 200:
logger.error(f"_fetch_timetable: HTTP {response.status_code}")
return None
try:
return response.json()
except ValueError as e: # JSONDecodeError is a ValueError subclass
logger.error(f"_fetch_timetable: invalid JSON response: {e}")
return None
def GetPrayerTime():
data = _fetch_timetable()
if data:
today_key = datetime.now(sg_timezone).strftime("%Y-%m-%d")
prayer_times = data.get(today_key)
if prayer_times:
logger.info("Successfully retrieved prayer times for today")
return prayer_times
logger.error("No prayer times found for today")
return None
def get_tomorrow_subuh():
data = _fetch_timetable()
if data:
tomorrow_key = (datetime.now(sg_timezone) + timedelta(days=1)).strftime("%Y-%m-%d")
tomorrow_times = data.get(tomorrow_key)
if tomorrow_times:
formatted = formatData(tomorrow_times)
if formatted:
return formatted[0].get('subuh')
logger.error("get_tomorrow_subuh: failed to retrieve tomorrow's Subuh time")
return None
def RefreshPrayerTime():
database_prayer_times = GetPrayerTime()
logger.info("Successfully updated Prayer Times")
return database_prayer_times
def formatData(input_dict):
"""Split a raw MUIS day entry into [formatted_times, date_dict].
time_dict keys are normalized to 'HH:MM AM/PM'. date_dict holds
metadata like hijri_date.
"""
if not input_dict:
logger.error("formatData: input_dict is empty")
return None
time_dict = input_dict.copy()
date_dict = {}
if 'hijri_date' in time_dict:
date_dict['hijri_date'] = time_dict.pop('hijri_date')
for prayer, time in time_dict.items():
# Remove extra whitespace
format_time = time.strip()
# Insert a space before am/pm if missing (e.g., "5:43am" -> "5:43 am")
format_time = re.sub(r'(\d)(am|pm)$', r'\1 \2', format_time, flags=re.IGNORECASE)
# Pad the hour with a leading zero if needed (e.g., "5:43 am" -> "05:43 am")
parts = format_time.split(':', 1)
if parts and len(parts[0]) == 1:
format_time = "0" + format_time
# Ensure the am/pm part is uppercase
format_time = re.sub(r'(am|pm)$', lambda m: m.group(0).upper(), format_time, flags=re.IGNORECASE)
time_dict[prayer] = format_time
return [time_dict, date_dict]
async def printTimes():
prayer_times = GetPrayerTime()
formatted = formatData(prayer_times)
if formatted is None:
logger.error("Failed to format prayer time data in printTimes()")
return "Failed to retrieve prayer times."
times = formatted[0]
dates = formatted[1]
prayer_date = datetime.now(sg_timezone).strftime("%d %B %Y")
hijri_date = dates.get('hijri_date', 'N/A')
message = await current_prayertimes(
prayer_date=prayer_date,
hijri_date=hijri_date,
subuh_time=times.get('subuh', 'N/A'),
syuruk_time=times.get('syuruk', 'N/A'),
zohor_time=times.get('zohor', 'N/A'),
asar_time=times.get('asar', 'N/A'),
maghrib_time=times.get('maghrib', 'N/A'),
isyak_time=times.get('isyak', 'N/A'),
)
logger.info("Successfully formatted prayer times")
return message
async def printUpcomingTimes():
"""Prayer times for the next 7 days (strictly future, tomorrow through day 7)."""
raw = _fetch_timetable()
if raw is None:
logger.error("Failed to retrieve prayer times in printUpcomingTimes()")
return "Failed to retrieve prayer times."
today = datetime.now(sg_timezone).date()
days_data = []
for offset in range(1, 8):
target = today + timedelta(days=offset)
key = target.strftime("%Y-%m-%d")
entry = raw.get(key)
if entry is None:
logger.warning(f"No MUIS data for {key}")
continue
formatted = formatData(entry)
if formatted is None:
continue
times = formatted[0]
dates = formatted[1]
days_data.append({
'date': target.strftime("%a, %d %b %Y"),
'hijri': dates.get('hijri_date', 'N/A'),
'subuh': times.get('subuh', 'N/A'),
'syuruk': times.get('syuruk', 'N/A'),
'zohor': times.get('zohor', 'N/A'),
'asar': times.get('asar', 'N/A'),
'maghrib': times.get('maghrib', 'N/A'),
'isyak': times.get('isyak', 'N/A'),
})
if not days_data:
logger.error("No upcoming prayer time data could be assembled")
return "Failed to retrieve upcoming prayer times."
message = await upcoming_prayertimes(days_data)
logger.info(f"Successfully formatted {len(days_data)} upcoming prayer times")
return message