-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.py
More file actions
333 lines (281 loc) · 10.3 KB
/
Copy pathutils.py
File metadata and controls
333 lines (281 loc) · 10.3 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import requests
import json
from bs4 import BeautifulSoup
from datetime import datetime, timezone, timedelta
try:
from zoneinfo import ZoneInfo # Python 3.9+
_HAS_ZONEINFO = True
except Exception: # pragma: no cover - fallback path
ZoneInfo = None # type: ignore
_HAS_ZONEINFO = False
try:
import pytz # Fallback if zoneinfo is unavailable
_HAS_PYTZ = True
except Exception: # pragma: no cover - optional dependency
pytz = None # type: ignore
_HAS_PYTZ = False
from model_helpers import (
create_port,
initialize_db,
create_vessel,
update_port_logs,
PortLog,
PortLogNotification,
)
def _to_datetime(ts):
if ts is None:
return None
if isinstance(ts, datetime):
return ts
try:
# Try ISO-style parsing first
return datetime.fromisoformat(str(ts))
except Exception:
# Give up and return None — callers will handle None
return None
def _seconds_between(new_ts, old_ts):
"""Return seconds between two timestamps or None on failure.
Handles datetime objects and ISO-like strings. If tz-awareness mismatches
(naive vs aware) we fallback to comparing naive times by dropping tzinfo.
"""
n = _to_datetime(new_ts)
o = _to_datetime(old_ts)
if not n or not o:
return None
try:
return (n - o).total_seconds()
except TypeError:
# likely naive vs aware mismatch — try comparing naive datetimes
try:
return (n.replace(tzinfo=None) - o.replace(tzinfo=None)).total_seconds()
except Exception:
return None
except Exception:
return None
def _format_duration(seconds: float | None) -> str | None:
"""Format seconds into 'HhMm' or 'Mm' without seconds (floor to minutes)."""
if seconds is None:
return None
try:
minutes = int(seconds) // 60
except Exception:
return None
hours = minutes // 60
mins = minutes % 60
days = hours // 24
hours = hours % 24
parts: list[str] = []
if days > 0:
parts.append(f"{days}d")
if hours > 0:
parts.append(f"{hours}h")
if mins > 0:
parts.append(f"{mins}m")
# If all units are zero (duration < 60s), show '0m'
if not parts:
return "0m"
return " ".join(parts)
# Maldives time zone (IANA): 'Indian/Maldives' (UTC+05:00)
_MALDIVES_TZNAME = "Indian/Maldives"
def _get_maldives_tz():
"""Return a tzinfo for Maldives, with graceful fallbacks.
Order of preference:
1) zoneinfo.ZoneInfo('Indian/Maldives') if available
2) pytz.timezone('Indian/Maldives') if available
3) Fixed-offset timezone UTC+05:00 as a last-resort
"""
if _HAS_ZONEINFO:
try:
return ZoneInfo(_MALDIVES_TZNAME)
except Exception:
pass
if _HAS_PYTZ:
try:
return pytz.timezone(_MALDIVES_TZNAME)
except Exception:
pass
# Final fallback: fixed offset of +05:00
return timezone(timedelta(hours=5))
def utc_to_maldives_time(ts, fmt: str | None = None) -> datetime | str | None:
"""Convert a UTC timestamp to Maldives local time (UTC+05:00).
- Accepts datetime, ISO-like string (e.g. '2025-11-05T12:34:00Z' or '+00:00'), or epoch seconds.
- If input is naive (no tzinfo), it's assumed to be UTC.
- If `fmt` is provided, returns a formatted string via strftime; otherwise returns
an aware datetime localized to 'Indian/Maldives'.
Returns None if conversion fails.
"""
if ts is None:
return None
dt: datetime | None = None
# Fast-path for datetime
if isinstance(ts, datetime):
dt = ts
# Epoch seconds
elif isinstance(ts, (int, float)):
try:
dt = datetime.fromtimestamp(float(ts), tz=timezone.utc)
except Exception:
return None
# Strings (ISO-ish)
elif isinstance(ts, str):
s = ts.strip()
# Handle trailing 'Z' designator
if s.endswith("Z"):
s = s[:-1] + "+00:00"
try:
dt = datetime.fromisoformat(s)
except Exception:
# Fall back to the module's tolerant parser
dt = _to_datetime(s)
if dt is None:
return None
else:
# Last resort: try the tolerant helper
dt = _to_datetime(ts)
if dt is None:
return None
# Ensure timestamp is in UTC for correct conversion
try:
if dt.tzinfo is None:
dt = dt.replace(tzinfo=timezone.utc)
else:
dt = dt.astimezone(timezone.utc)
except Exception:
# If anything goes wrong, assume UTC
dt = dt.replace(tzinfo=timezone.utc)
local_dt = dt.astimezone(_get_maldives_tz())
if fmt:
try:
return local_dt.strftime(fmt)
except Exception:
return None
return local_dt
def api_request(api_key):
url = f"https://followme.mv/api/v5/public/{api_key}"
api_req = requests.get(url).text
# converts api data to python dictionary
all_vessels_dict = json.loads(
BeautifulSoup(api_req, features="html.parser").find("body").get_text()
)
return all_vessels_dict["data"]
def update_port_info(api_data):
for vessel in api_data:
name = api_data[vessel]["port"]
if name == "":
continue
else:
create_port(name)
def update_vessel_info(api_data):
for vessel in api_data:
name = api_data[vessel]["name"]
vessel_type = api_data[vessel]["type"]
create_vessel(vessel, name, vessel_type)
def all_notify() -> dict:
"""
Inspect the last 50 PortLog entries across all ports and return a dict
of un-notified logs with vessel info.
Returns a dict with 'arrivals' and 'departures' keys. Each value is a dict
keyed by PortLog.id with the same structure as notify():
- portlog_id, vessel_id, name, vessel_type, contact, last_port_name,
event, timestamp, transit_time
For arrivals, also includes:
- departed: the time the vessel left its previous port (if known)
For departures, also includes:
- stay_time: how long the vessel stayed at this port
"""
arrivals: dict = {}
departures: dict = {}
# Get last 50 un-notified logs across all ports
query = (
PortLog.select()
.where(PortLog.notified == False) # noqa: E712
.order_by(PortLog.timestamp.desc())
.limit(50)
)
for log in query:
if not log.notified:
vessel = log.vessel
# Find previous log for this vessel
prev_log = (
PortLog.select()
.where((PortLog.vessel == vessel) & (PortLog.timestamp < log.timestamp))
.order_by(PortLog.id.desc())
.first()
)
if log.event == "arrival":
transit_seconds = None
departed_str = None
if prev_log:
# Calculate transit time from previous log (robust)
transit_seconds = _seconds_between(
log.timestamp, prev_log.timestamp
)
# Format departure time if previous log exists
try:
prev_ts = utc_to_maldives_time(prev_log.timestamp)
now = utc_to_maldives_time(datetime.now())
if prev_ts.date() == now.date():
departed_str = prev_ts.strftime("%H:%M")
else:
departed_str = prev_ts.strftime("%d %b %H:%M")
except Exception:
try:
departed_str = (
utc_to_maldives_time(prev_log.timestamp)
.replace(microsecond=0)
.isoformat()
)
except Exception:
departed_str = utc_to_maldives_time(prev_log.timestamp)
arrivals[log.id] = {
"portlog_id": log.id,
"vessel_id": vessel.id,
"name": vessel.name,
"vessel_type": vessel.vessel_type,
"contact": vessel.contact,
"port_name": log.port.name,
"last_port_name": prev_log.port.name if prev_log else None,
"event": log.event,
"timestamp": log.timestamp,
"transit_time": _format_duration(transit_seconds),
"departed": departed_str,
}
elif log.event == "departure":
# Calculate stay duration if we have a previous arrival at this port
stay_seconds = None
if prev_log and prev_log.event == "arrival":
try:
if prev_log.port.id == log.port.id:
stay_seconds = _seconds_between(
log.timestamp, prev_log.timestamp
)
except Exception:
stay_seconds = None
departures[log.id] = {
"portlog_id": log.id,
"vessel_id": vessel.id,
"name": vessel.name,
"vessel_type": vessel.vessel_type,
"contact": vessel.contact,
"port_name": log.port.name,
"last_port_name": log.port.name,
"event": log.event,
"timestamp": utc_to_maldives_time(log.timestamp),
"stay_time": _format_duration(stay_seconds),
}
return {
"arrivals": arrivals,
"departures": departures,
}
def update_db_with_api(api_key: str, bot_start: bool = False):
initialize_db()
all_vessels = api_request(api_key)
update_port_info(all_vessels)
update_vessel_info(all_vessels)
# Check if this is initial sync by seeing if we have any port logs
is_initial = PortLog.select().count() == 0 or bot_start
if bot_start:
PortLogNotification.update(sent=True).execute()
print("Bot start: Port logs set to sent")
update_port_logs(all_vessels, is_initial_sync=is_initial)
return True